#!/bin/bash

# wine-setup -- simple setting script for WINE in Momonga Linux
#
# partially based on tdiary-setup in tdiary-1.5.5-1m of Momonga Linux

#docdir=`find /usr/share/doc -type d -maxdepth 1 | grep -e "wine-[0-9]"`
docdir=/usr/share/config-sample/wine
systmconfdir=/etc/wine
configdir="${HOME}/.wine"
systemwidedir=/var/lib/wine

setup_wineconf () {
    flag=$1
    [ -d $configdir ] || mkdir -p $configdir
    case "$1" in
	local)
	    target=$2
	    cp $docdir/wine.conf /tmp/wine.conf.sed
	    sed -e "s|\"Path\" = \"\${HOME}/wine\"|\"Path\" = \"$target\"|" /tmp/wine.conf.sed | sed -e "s|\"Path\" = \"\${HOME}\"|\"Path\" = \"${HOME}\"|"> $configdir/config
	    rm -rf /tmp/wine.conf.sed
#	    cp -pf $docdir/wine.userreg $configdir/user.reg
#	    cp -pf $docdir/wine.systemreg $configdir/system.reg
	    ;;
	systemwide)    
	    ln -s $systmconfdir/wine.conf $configdir/config
	    ln -s $systmconfdir/wine.userreg $configdir/user.reg
	    ln -s $systmconfdir/wine.systemreg $configdir/system.reg
	    ;;
	*)
	    exit 2
    esac
}


setup_fakewindows () {
# this function refers to make_a_windows_system.sh 
# in wine-20030408-3mdk.src.rpm of Mandrake Linux
    target=${1:-${HOME}/wine}

    echo "made a fake windows system dir to $target."

    [ -d $target ] || mkdir -p $target
    for i in system Desktop Favorites Fonts NetHood \
        "Start Menu/Programs/Startup" \
	Recent SendTo ShellNew system32 Profiles/Administrator; do
      mkdir -p "$target/windows/$i"
    done
    mkdir -p "$target/windows/Program Files/Common Files"
    mkdir -p "$target/windows/My Documents"
    for i in shell.dll shell32.dll winsock.dll wnsock32.dll; do
	touch $target/windows/system/$i
    done
    touch $target/autoexec.bat
    touch $target/config.sys
#    cp -f $docdir/{win.ini,system.ini} $target/windows/
#    cp -f $docdir/system.ini $target/windows/
}

usage () {
    echo "Usage: $0 [local fake-windows_fullpath_name|systemwide] "
    echo ""
    echo "ex) $0 local /home/$USER/wine"
    echo ""
    echo "Warning! Systemwide setting doesn't work correctry now, choose local!!"
    echo "Notice! You MUST point a full path of a writable directory as a fake-windows directory."
    echo ""
}


RETVAL=0

# See how we were called.

case "$1" in
    local)
	fakewindir=${2:-${HOME}/wine}
        echo "Setup WINE to use local fake-windows directory (${fakewindir})"
        setup_fakewindows "$fakewindir"
	setup_wineconf $1 "$fakewindir"
        RETVAL=$?
        ;;
    systemwide)
        echo "Setup WINE to use systemwide fake-windows directory (${systemwidedir})"
	echo "NOTICE!! You MUST have writable access in ${systemwidedir}!!"
	setup_wineconf $1
        RETVAL=$?
        ;;
    *)
        usage
        exit 1
esac

exit $RETVAL
echo "if you have windows partition, you may want to use native windows dlls as follows"
echo "ex) rm -rf $2/windows/{system,system32}; ln -s /mnt/dos/windows/{system,system32} $2/windows/"
