#!/bin/sh
# Copyright (C) 2005 Etersoft, Vitaly Lipatov <lav@etersoft.ru>
# Redistribution of this file is permitted under the terms of the
# GNU Public License (GPL)

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

if [ -f ${WINEPREFIX}/user.reg ]; then
	exec $WINELOADER "$@"
fi

####### WINE dir preparing =======
mkdir -p ${WINEPREFIX}

# Which root will be used
groups | grep wine
RES=$?
if [ -d /var/lib/wine -a $RES = 0  -a ! -d $CROOT ]
then
	echo "NOTE: /var/lib/wine is exists and we in wine group: WINE-server mode enabled"
	# FIXME: add test for special file
	CROOT=/var/lib/wine
	touch $CROOT/test.tmp
	if [ ! "$?" = "0" ]
	then
		echo "ERROR: $CROOT is not writable, you are not in wine group, but: `groups`"
		echo
		exit 1
	fi
	rm -f $CROOT/test.tmp
	#install -D -m 0644 /usr/share/wine/wine-multiuser.conf $WINEPREFIX/config
else
	echo "Building local WINE tree..."
fi

#test -f $WINEPREFIX/config || install -D -m 0644 /usr/share/wine/wine.conf $WINEPREFIX/config

DEV=${WINEPREFIX}/dosdevices

# Create disks
if [ ! -d ${WINEPREFIX}/dosdevices ]
then
	mkdir -p ${CROOT} ${DEV}
	ln -s /mnt/floppy ${DEV}/a:
	ln -s /dev/fd0 ${DEV}/a::
	ln -s $CROOT ${DEV}/c:
	ln -s ~/Documents ${DEV}/d:
	# FIXME: We need to detect CD drive path
	CDROM=cdrom
	ln -s /mnt/$CDROM ${DEV}/e:
	ln -s /dev/$CDROM ${DEV}/e::

	ln -s /dev/ttyS0 ${DEV}/com1
	#ln -s ~ ~/.wine/dosdevices/f:
	#ln -s / ~/.wine/dosdevices/z:
fi

# WINE apps autostart
mkdir -p ~/.xsession.d
test -f ~/.xsession.d/wineboot || ln -sf /usr/bin/wineboot ~/.xsession.d

# for usability
test -e ${HOME}/wine_c || ln -s ${DEV}/c: ${HOME}/wine_c || :

# Create default WINE tree
/usr/bin/wineprefixcreate

Xdialog --msgbox "WINE has been configured for the first time.\nUse ${HOME}/wine_c as WINE C: disk\nCopy your program into and install it." 0 0
exec $WINELOADER "$@"
