#!/bin/sh
# -*- Mode: sh -*-
# Copyright (C) 2000 by Chmouel Boudjnah <chmouel@mandrakesoft.com>,
# MandrakeSoft
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
# 
# Modified by Danny <obiwan@mailmij.org>
# Aug 11 2002
# - check and create winetmp-${USER}
# - make sure all options get pass to wine, while still
#    cd-ing to correct directory (I had to require -- even when giving options, 
#    in contrast with normal wine.bin commandline)
# - check for number of arguments
#
# Modified by Lav <lav@etersoft.ru>
# Jan 24 2004
# - do launch as in WineX-launcher
# - copy default system tree if does not exist in home
# - copy all registers file
# Feb 15 2004
# - still exist problem with run program throw wine services (  )
# - reg placed in to WINE_C
# Apr 10 2004
# - sync with wineprefixcreate from new wine
# Jun 20 2004
# - use wineprefixcreate for creating needs environment

export WINELOADER=/usr/bin/wine-glibc
export WINEPREFIX=${HOME}/.wine
CROOT=${WINEPREFIX}/drive_c

if [ ! -d ${WINEPREFIX} ]; then
    mkdir -p ${WINEPREFIX}
    cp /usr/share/wine/wine.conf $WINEPREFIX/config
	# Create disks
	if [ ! -d ${WINEPREFIX}/dosdevices ]
	then
		#mkdir -p "$CROOT/temp"
		mkdir -p "$CROOT/My Documents"
	    mkdir -p ${WINEPREFIX}/dosdevices
		#ln -s /mnt/floppy/auto ~/.wine/dosdevices/a:
		ln -s $CROOT ${WINEPREFIX}/dosdevices/c:
		CDROM=/mnt/cdrom
		test -d /mnt/auto && CDROM=/mnt/auto/cdrom
		ln -s $CDROM ${WINEPREFIX}/dosdevices/d:
		#ln -s ~ ~/.wine/dosdevices/f:
		#ln -s / ~/.wine/dosdevices/z:
	fi

	# for usability
	test -e ${HOME}/wine_c || ln -s ${CROOT} ${HOME}/wine_c || :
	if [ -d $CROOT ]; then
		/usr/bin/wineprefixcreate --update
	else
		/usr/bin/wineprefixcreate
	fi
fi

WORKDIR=""

while [ $# -gt 0 ]; do
    case "$1" in
    "--workdir") WORKDIR="$2"; shift 2;;
    *) break;;
    esac
done

test -z "$WORKDIR" || cd "$WORKDIR"

exec $WINELOADER "$@"
