#!/bin/bash

# wine-setup2 -- simple setting script for WINE in Momonga Linux
#
# This setup script is greatly inspired by wine-config-sidenet by Sidenet 
# (http://sidenet.ddo.jp/winetips/)
# 
#                nosanosa <nosanosa at momonga-linux.org>

winelibdir=/usr/lib/wine
templatedir=/usr/share/wine
configfile=config.template
w32codecdir=/usr/lib/win32
fontsdir=/usr/share/fonts/truetype/japanese
configdir="${HOME}/.wine"

today=`date +20%y%m%d%H%M`

confirm_oldsettings () {

    unset USEOLDFAKEDIR
    USEOLDFAKEDIR=n
    echo ""
    echo "--- Checking Old Settings..."
    echo ""
    if [ -n "$fakewindir" ] && [ -e $fakewindir ]
	then
	echo "Existing $fakewindir is found."
	echo "Do you want to use the existing $fakewindir as a fake windows directory?[y/N]"
    while true
    do
      read useold
      case "$useold" in 
	  y | yes | Y | Yes | YES )
	      echo "Existing $fakewindir is used as a fake windows directory"
	      USEOLDFAKEDIR=y
	      return 0
	      ;;
	  * ) 
	      echo "Existing $fakewindir will be renamed to $fakewindir.bak.$today"
	      USEOLDFAKEDIR=n
	      return 0
	      ;;
      esac
    done
    fi
}

backup_oldsettings () {

    echo "*** Checking old configs..."
    echo ""
    if [ -e ~/.wine ]
	then
	echo "~/.wine has already existed and will be renamed to  ~/.wine.bak.$today"
	mv ~/.wine ~/.wine.bak.$today
    fi

    if [ x"$USEOLDFAKEDIR" = xn -a -d $fakewindir ]; then
	mv $fakewindir $fakewindir.bak.$today
    fi
}

get_confirm () {
    echo -e "$1"
    while true
    do
      read x
      case "$x" in 
	  y | yes | Y | Yes | YES )
	      return 0
	      ;;
	  n | no | N | No | NO )
	      return 1
	      ;;
	  * ) return 1
	      ;;
      esac
    done
}

confirm_fakewindir () {
    echo "--- Setting up a fake Windows directory..."
    echo ""
    echo "Wine need a fake Windows directory which has the same structure as C:\\windows of Windows. This setup program will make it."
    echo "Which directory do you want to asign as a fake Windows directory?"
    echo "Enter the fullpath[${HOME}/wine]."
    unset fakewindir
    while read fakewindir && [ x`expr substr "$fakewindir" 1 1` != x"/" ]
    do
      if [ x"$fakewindir" = x"" ]; then
	  fakewindir=${HOME}/wine
	  break;
      fi
      echo "The path of direcotries should be fullpath and must begin with \"/\"."
      echo "Please input again."
    done
}

print_entries () {

    itr=0
#      echo "Now, entries are: "
    while [ $itr -le $num ]
    do
      echo "         ${letter[$itr]}: -> ${drives[$itr]}"
      itr=`expr $itr + 1`
    done

}

confirm_dirs () {

    echo ""
    echo "--- Entry the mapping of directories used by WINE"
    echo ""

    num=0
    letter[$num]=a
    drives[$num]=/mnt/floppy
    num=1
    letter[$num]=b
    drives[$num]=/tmp
    num=2
    letter[$num]=c
    drives[$num]=$fakewindir
    num=3
    letter[$num]=d
    drives[$num]=$HOME
    num=4
    letter[$num]=e
    drives[$num]=/mnt/cdrom

    echo "The directories you want to use with Wine should be entried and mapped to a certain drive letter. This is done by making symbolic links in $configdir/dosdevices as \"ln -s ${HOME}/windows c:\". This setup will make these links."
    echo "Following links will be made by default."
    while (print_entries; get_confirm "Do you want to entry more directories as windows drives?[y/N]")
    do
      num=`expr $num + 1`
      echo "Which directory do you want to entry? (It must be fullpath)"
#      read drives[$num]
      while read drives[$num] && [ x`expr substr "${drives[$num]}" 1 1` != x"/" ]
	do
	echo "The path of the direcotory should be fullpath and must begin with \"/\"."
	echo "Please input again."
      done
    echo "What drive letter do you want to assign to ${drives[$num]}"
      read letter[$num]
      while [ `expr length "${letter[$num]}"` -ne 1 ] || [ `expr "${letter[$num]}" : [a-z]` -ne 1 ]
	do
	echo "Drive letter must be one lower-case letter."
	echo "What drive letter do you want to assign to ${drives[$num]}"
	read letter[$num]
      done
    done
}

confirm_settings () {
    
    echo ""
    echo "--- Settings of the Font Substituion..."
    echo ""
    echo "Windows often demand MS fonts to display as screen fonts."
    echo "If you do not have MS fonts, Font Substitution is recommended."
    echo "Do you want to substitute MS fonts by X11 fonts?[Y/n]"
    unset USE_FONT_SUBSTITUTION
    while [ -z "$USE_FONT_SUBSTITUTION" ]
    do
      read x
      case "$x" in 
	  y | yes | Y | Yes | YES )
	      USE_FONT_SUBSTITUTION=y
	      ;;
	  n | no | N | No | NO )
	      USE_FONT_SUBSTITUTION=n
	      ;;
	  * ) USE_FONT_SUBSTITUTION=y
	      ;;
      esac
    done
    if [ "$USE_FONT_SUBSTITUTION" = "y" ]; then
	echo "Which font do you want to use to substitute MS fonts?"
	echo "It must be accessible by FontConfig [Sazanami Gothic]"
	unset SUBFONT
	read SUBFONT
	if [ -z "$SUBFONT" ]; then
	    SUBFONT="Sazanami Gothic"
	fi
	echo "Locate the fontfile of the substitute fonts[/usr/share/fonts/truetype/japanese/sazanami-gothic.ttf]"
	unset FONTFILE
	read FONTFILE
	if [ -z "$FONTFILE" ]; then
	    FONTFILE="/usr/share/fonts/truetype/japanese/sazanami-gothic.ttf"
	fi	
    else
	echo "You need MS fonts in C://windows/Fonts"
    fi

    echo ""
    echo "--- Setting an antialiasing of fonts"
    echo ""
    echo "Do you want to use the antialiasing fonts if available?[Y/n]"
    unset USEAA
    while [ -z "$USEAA" ]
    do
      read x
      case "$x" in 
	  y | yes | Y | Yes | YES )
	      USEAA=y
	      ;;
	  n | no | N | No | NO )
	      USEAA=n
	      ;;
	  * ) USEAA=y
	      ;;
      esac
    done

    echo ""
    echo "--- Setting of ODBC"
    echo ""
    echo "Do you want to use UNIX ODBC?[Y/n]"
    unset UNIXODBC
    while [ -z "$UNIXODBC" ]
    do
      read x
      case "$x" in 
	  y | yes | Y | Yes | YES )
	      UNIXODBC=y
	      ;;
	  n | no | N | No | NO )
	      UNIXODBC=n
	      ;;
	  * ) UNIXODBC=y
	      ;;
      esac
    done
}

confirm_all () {
    echo "===================================================="
    echo "Configulation completed and Your configulations are:"
    echo ""
    if [ x"$USEOLDFAKEDIR" = xn ]; then    
	echo "    Fake Windows Directory:   $fakewindir (Make NEW)"
	echo ""
    else
	echo "    Fake Windows Directory:   $fakewindir (USE EXISTING)"
	echo ""
    fi	
    echo "    Windows Drive Assignations:"
    print_entries
    echo ""
    if [ $USE_FONT_SUBSTITUTION == y ]; then
    echo "    Use Fonts Substitution:      Yes"
    echo "        Substitution Fonts:      $SUBFONT "
    echo "        Location of Substitution Fonts: $FONTFILE"
    else
    echo "    Use Fonts Substitution:      No"
    fi    
    echo ""
    if [ $USEAA == y ]; then
    echo "    Use Antialias Fonts:      Yes"
    else
    echo "    Use Antialias Fonts:      No"
    fi
    echo ""
    if [ $UNIXODBC = y ]; then
    echo "    Use UNIX ODBC:            Yes"
    else
    echo "    Use UNIX ODBC:            No"
    fi
    echo ""

    echo "Continue?[Y/n]"
    while true
      do
      read x
      case "$x" in 
	  y | yes | Y | Yes | YES )
	      return 1
	      ;;
	  n | no | N | No | NO )
	      echo "========= Setup aborted! =========" 
	      echo "--- Nothing was changed."
	      exit 0
	      ;;
	  * ) return 1
	      ;;
      esac
    done
}

entry_drives () {
    let=`echo -n $2 | tr a-z A-Z`
    perl -p -i -e "s/;;==trigerline==/;;==trigerline==\n[Drive ${let}]\n\"codepage\" = \"20932\"\n/g" $configdir/config
    ln -s $1 $configdir/dosdevices/$2:

}

setup_wineconf () {

    echo ""
    echo "*** Setting ~/.wine/config..."

    [ -d $configdir ] || mkdir -p $configdir
    cp $templatedir/$configfile $configdir/config

    mkdir -p $configdir/dosdevices
    itr=0    
    while [ $itr -le $num ]
    do
      entry_drives ${drives[$itr]} ${letter[$itr]}
      itr=`expr $itr + 1`
    done    

    if [ "$USEAA" == "y" ]; then
	perl -p -i -e "s/\"ClientSideAntiAliasWithRender\" = \"N\"/;;\"ClientSideAntiAliasWithRender\" = \"N\"/g" $configdir/config
    fi

    if [ "$UNIXODBC" == "y" ]; then
	perl -p -i -e "s/\"odbc32\"       = \"native, builtin\"/;;\"odbc32\"       = \"native, builtin\"/g" $configdir/config
    fi	

}

setup_fakewindows () {
    target=${fakewindir}

    echo ""
    echo "Making a fake Windows directory...."
    echo ""

    [ -d $target ] || mkdir -p $target
    for i in inf temp History command Recent Fonts SendTo "Start Menu/Programs/Startup" Cookies Desktop Favorites "Temporary Internet Files" "Application Data" system32 "All Users" system ShellNew; do
#    for i in inf temp History command Recent SendTo "Start Menu/Programs/Startup" Cookies Desktop Favorites "Temporary Internet Files" "Application Data" system32 "All Users" system ShellNew; do
      mkdir -p "$target/windows/$i"
    done
    mkdir -p "$target/Program Files/Common Files"
    mkdir -p "$target/My Documents"

    for i in regedit expand rundll32 start; do
	ln -s ${winelibdir}/$i.exe.so $target/windows/$i.exe
    done

    if [ -e $w32codecdir ]
	then
#	echo "MPlayer codec package found."
#	echo "Linking some codecs for wine ..."
	for i in ir41_32 iccvid ir32_32 ir50_32 msvidc32 msrle32 tsd32; do
	    ln -s $w32codecdir/$i.dll $target/windows/system
	done
	for i in imaadp32 lhacm msadp32 msgsm32 tssoft32; do
	    ln -s $w32codecdir/$i.acm $target/windows/system
	done
    fi

    ln -s $FONTFILE $target/windows/Fonts/
#    ln -s $HOME/.fonts $target/windows/Fonts

    cp -f $templatedir/wine.inf $target/windows/inf/wine.inf
    if [ "$USE_FONT_SUBSTITUTION" == "y" ]; then
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS UI Gothic\",,\"Sazanami Gothic\"$/HKLM,\%FontRepStr\%,\"MS UI Gothic\",,\"${SUBFONT}\"/g" $target/windows/inf/wine.inf
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS Gothic\",,\"Sazanami Gothic\"$/HKLM,\%FontRepStr\%,\"MS Gothic\",,\"${SUBFONT}\"/g" $target/windows/inf/wine.inf
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS PGothic\",,\"Sazanami Gothic\"$/HKLM,\%FontRepStr\%,\"MS PGothic\",,\"${SUBFONT}\"/g" $target/windows/inf/wine.inf
    else
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS UI Gothic\",,\"Sazanami Gothic\"$//g" $target/windows/inf/wine.inf
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS Gothic\",,\"Sazanami Gothic\"$//g" $target/windows/inf/wine.inf
	perl -p -i -e "s/^HKLM,\%FontRepStr\%,\"MS PGothic\",,\"Sazanami Gothic\"$//g" $target/windows/inf/wine.inf	
    fi
	
}

setup_wineconf_wineinf () {
    echo ""
    echo "*** Setting a default registry ..."
    echo ""
    wineserver -k
    wine rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 wine.inf 2>&1
    wineserver -k
}

RETVAL=0

if [ ! -z "$@" ]; then
    echo "$0 -- A setup script of WINE for Momonga Linux"
    echo ""
    echo "No option needed, simply type $0 and follow an instruction."
    exit 1
fi

echo ""
echo "This is a setup script of WINE for Momonga Linux"
echo ""
confirm_fakewindir
confirm_oldsettings
confirm_dirs
confirm_settings
confirm_all
echo ""
echo "=========== Setup procedure started... ==========="
echo ""
backup_oldsettings
if [ x"$USEOLDFAKEDIR" = xn ]; then
    setup_fakewindows
fi
setup_wineconf
if [ x"$USEOLDFAKEDIR" = xn ]; then
    setup_wineconf_wineinf
fi
echo ""
echo "=========== Setup procedure completed! ==========="
echo "You can change settings by editing ~/.wine/config"
echo "You can run Windows applications by \"wine PATH_TO_BINARY &\"."
echo "If you want to install Internet Explore or Windows Media Player, some tools in /usr/share/wine/misctools may help you."

RETVAL=$?

exit $RETVAL

