#!/bin/bash

# wine-config-sidenet installer script
# Licenced under GPL

today=`date +%y%m%d%H%M`
username=$USER
homedir=$HOME

# Some variables
# Distributers can customise installer using these variables.

# Location for wine base directory structure and config file.
# Ex. "/usr/share/wine-config-sidenet"
# Default is "."
setupdir="."

# Where to place temporary files.
# Ex. "/tmp/wine-config-sidenet-$username"
# Default is "."
tempdir="."

# Where to place virtual c drive.
# Ex. "$homedir/c"
# If nothing is set, setup will ask users.
cdrivedir=""

# Where to place log file
logfile="$homedir/.wine/sidenet/wcs.log"

# Distributers can customise install option

# Set "99" for custom install
installtype=""

# Custom install options
silentinst=""
ie6install=""
mp6install=""
mp71install=""
dcominstall=""
msiinstall=""
w32codecs=""
dotfont=""

# Some functions for user input

function yn_answer {
 unset refstr
 while [ "$refstr" != 'y' ] && [ "$refstr" != 'n' ]
 do {
  echo -n "$1"
  read refstr
 }
 done
}

function strinput {
 unset refstr
 echo -n "$1"
 read refstr
}

echo
echo "wine-config-sidenet version 1.9.1 installer"
echo "THIS SOFTWARE COMES WITH ABSOLUTELY NO WARRANTY."
echo "USE AT YOUR OWN RISK."
echo
echo "Read readme.en or readme.ja.euc or readme.br carefully before running setup."
echo
echo "This script will configure your wine environment properly."
echo -n "Continue(y/n)?"
yn_answer
if [ "$refstr" = "y" ]
then
 echo
 echo "Setup started on 20$today."
else
 echo
 echo "Setup aborted."
 exit 1
fi

# Let's check if the environment is sane

if [ ! `which wine` ]
then
 echo
 echo "ERROR: Could not find wine installation."
 echo "       Setup wine first."
 echo "Setup aborted."
 exit 1
fi

if [ `whoami` = 'root' ]
then
 echo
 echo "ERROR: wine-config-sidenet cannot be installed as root."
 echo "Setup aborted."
 exit 1
fi

if [ ! `which wineserver` ] && [ ! -e /usr/lib/wine/wineserver ]
then
 echo "ERROR: Could not find \"wineserver\"."
 echo "       Please check your wine installation."
 echo "Setup aborted."
 exit 1
fi

if [ ! -e $setupdir/wineconfig ] || [ ! -e $setupdir/inf ]
then
 echo
 echo "ERROR: Please run setup in wine-config-sidenet directory."
 echo "Setup aborted."
 exit 1
fi

if [ "$DISPLAY" = "" ]
then
 echo
 echo "ERROR: Can't open display."
 echo "       This script requires the X window system."
 echo "Setup aborted."
 exit 1
fi


# Configure language
# FIXME :Get them from LANG envirnoment variable.

echo
echo "Please spicify your language."
echo "Supported language :"
echo "Arabic(ar) Blazilian Portuguese(br) Czech(cs)"
echo "Simplified Chinese(cn) Traditional Chinese(tw)"
echo "Danish(da) Dutch(nl) English(en) Finnish(fi) French(fr)"
echo "German(de) Greek(el) Hebrew(he) Hungarian(hu) Itarian(it)"
echo "Nihongo(ja) Korean(ko) Norwegian(no) Polish(pl)"
echo "Portuguese(pt) Russian(ru) Spanish(es) Swedish(sv) Turkish(tr)"
echo
echo "If unsure, just hit enter and english version will be installed."
echo -n "Language code ? : "
strinput

if [ "$refstr" = "ar" ]
then
 mslang="AR"
 wcslang="en"
 
elif [ "$refstr" = "br" ]
then
 mslang="PT-BR"
 wcslang="en"
 
elif [ "$refstr" = "cs" ]
then
 mslang="CS"
 wcslang="en"

elif [ "$refstr" = "cn" ]
then
 mslang="CN"
 wcslang="cn"

elif [ "$refstr" = "tw" ]
then
 mslang="TW"
 wcslang="cn"

elif [ "$refstr" = "da" ]
then
 mslang="DA"
 wcslang="en"

elif [ "$refstr" = "nl" ]
then
 mslang="NL"
 wcslang="en"

elif [ "$refstr" = "en" ]
then
 mslang="EN-US"
 wcslang="en"

elif [ "$refstr" = "fi" ]
then
 mslang="FI"
 wcslang="en"

elif [ "$refstr" = "fr" ]
then
 mslang="FR"
 wcslang="en"

elif [ "$refstr" = "de" ]
then
 mslang="DE"
 wcslang="en"

elif [ "$refstr" = "el" ]
then
 mslang="EL"
 wcslang="en"

elif [ "$refstr" = "he" ]
then
 mslang="HE"
 wcslang="en"

elif [ "$refstr" = "hu" ]
then
 mslang="HU"
 wcslang="en"

elif [ "$refstr" = "it" ]
then
 mslang="IT"
 wcslang="en"

elif [ "$refstr" = "ja" ]
then
 mslang="JA"
 wcslang="ja"

elif [ "$refstr" = "ko" ]
then
 mslang="KO"
 wcslang="cn"

elif [ "$refstr" = "no" ]
then
 mslang="NO"
 wcslang="en"

elif [ "$refstr" = "pl" ]
then
 mslang="PL"
 wcslang="en"

elif [ "$refstr" = "pt" ]
then
 mslang="PT"
 wcslang="en"

elif [ "$refstr" = "ru" ]
then
 mslang="RU"
 wcslang="en"

elif [ "$refstr" = "es" ]
then
 mslang="ES"
 wcslang="en"

elif [ "$refstr" = "sv" ]
then
 mslang="SV"
 wcslang="en"

elif [ "$refstr" = "tr" ]
then
 mslang="TR"
 wcslang="en"

# Default is EN-US

else
 mslang="EN-US"
 wcslang="en"
fi

echo "Selected language for downloading : $mslang"
echo "Selected language for language pack : $wcslang"

# Configure install option

echo
echo "Please select install option."
echo "Install options are :"
echo
echo "0 :Configuration only (default)"
echo "   Configure wine properly. 100% MS-free installation."
echo "   Setup will run silently."
echo "   If unsure, choose thise one."
echo
echo "1 :IE6 only installation (Optional)"
echo "   IE6 only installation. Setup will run silently."
echo "   This option requires windows licence: Don't choose this option unless needed."
echo
echo "2 :IE6 with WMP6 installation (Optional)"
echo "   Install IE6 and WMP6. Setup will run silently."
echo "   This option requires windows licence: Don't choose this option unless needed."
echo
echo "3 :Manual installation"
echo "   Setup will ask you all available options."
echo "   (For expert users)"
echo

if [ "$installtype" = "" ]
then
 echo "If unsure,just hit enter."
 echo -n "Install option (0-4)? : "
 strinput
else
 refstr=$installtype
fi

if [ "$refstr" = "1" ]
then
 echo "IE6 only installation selected."
 installtype="1"
 silentinst="y"
 ie6install="y"
 mp6install="n"
 dcominstall="n"
 msiinstall="n"
 w32codecs="y"
 dotfont="y"

elif [ "$refstr" = "2" ]
then
 echo "IE6 and WMP6 installation selected."
 installtype="2"
 silentinst="y"
 ie6install="y"
 mp6install="y"
 dcominstall="n"
 msiinstall="n"
 w32codecs="y"
 dotfont="y"

elif [ "$refstr" = "3" ]
then
 echo "Manual installation selected."
 installtype="3"
 silentinst="n"
 ie6install=""
 mp6install=""
 dcominstall=""
 msiinstall=""
 w32codecs=""
 dotfont=""

elif [ "$refstr" = "99" ]
then
 echo "Distributer custom installation."
 installtype="99"

elif [ "$refstr" = "0" ]
then
 echo "Wine configuration only."
 installtype="0"
 silentinst="y"
 ie6install="n"
 mp6install="n"
 dcominstall="n"
 msiinstall="n"
 w32codecs="y"
 dotfont="y"

else
 echo "Wine configuration only."
 installtype="0"
 silentinst="y"
 ie6install="n"
 mp6install="n"
 dcominstall="n"
 msiinstall="n"
 w32codecs="y"
 dotfont="y"
fi

# Ask users about packages to install

if [ "$ie6install" = "" ]
then
 echo -n "Install IE6 (y/n)?"
 yn_answer
 ie6install=$refstr
fi

if [ "$dcominstall" = "" ]
then
 if [ -e $tempdir/dcom98.exe ] || [ -e $tempdir/DCOM98.EXE ]
 then
  echo "Found DCOM98."
  echo -n "Install DCOM98 (y/n)?"
  yn_answer
  dcominstall=$refstr
 else
  dcominstall="n"
 fi
fi

if [ "$msiinstall" = "" ]
then
 if [ -e $tempdir/InstMsiA.exe ]  || [ -e $tempdir/instmsia.exe ] || [ -e $tempdir/INSTMSIA.EXE ]
 then
  echo "Found Windows Installer."
  echo -n "Install Windows Installer (y/n)?"
  yn_answer
  msiinstall=$refstr
 else
  msiinstall="n"
 fi
fi

if [ "$dotfont" = "" ]
then
 echo -n "Link ~/.font -> c:\\windows\\fonts (y/n)?"
 yn_answer
 dotfont=$refstr
fi

if [ "$w32codecs" = "" ]
then
 if [ -e /usr/lib/win32 ]
 then
  echo "Found MPlayer codec package in /usr/lib/win32"
  echo -n "Install MPlayer codec package (y/n)?"
  yn_answer
  w32codecs=$refstr
  w32codecsdir="/usr/lib/win32"
 elif [ -e /usr/local/lib/win32 ]
 then
  echo "Found MPlayer codec package in /usr/local/lib/win32"
  echo -n "Install MPlayer codec package (y/n)?"
  yn_answer
  w32codecs=$refstr
  w32codecsdir="/usr/local/lib/win32"
 else
  w32codecs="n"
 fi
else
 if [ -e /usr/lib/win32 ]
 then
  echo "Found MPlayer codec package in /usr/lib/win32"
  w32codecsdir="/usr/lib/win32"
 elif [ -e /usr/local/lib/win32 ]
 then
  echo "Found MPlayer codec package in /usr/local/lib/win32"
  w32codecsdir="/usr/local/lib/win32"
 fi
fi

# Check if we have wget and can download IE6 and WMP7

if [ "$ie6install" = "y" ]
then
 if [ -e $tempdir/ie6setup.exe ] || [ -e $tempdir/IE6SETUP.EXE ] || [ -e $tempdir/ie6setup_$mslang.exe ]
 then
  echo "Found Internet Explorer 6 setup."
  ie6download="n"
 else
  if [ `which wget` ]
  then
   echo "Internet Explorer 6 will be downloaded during installation."
   ie6download="y"
  else
   echo "Internet Explorer 6 not found."
   echo "ERROR: wget not found."
   echo "       Setup cannot download ie6setup.exe."
   echo "       Please download ie6setup.exe manually and try again."
   echo "Setup aborted."
   exit 1
  fi
 fi
fi

if [ "$cdrivedir" = "" ]
then
 if [ "$silentinst" = "n" ]
 then
  echo
  echo "Virtual C drive cofiguration :"
  echo "Please spicify where to place virtual C drive."
  echo "DO NOT SELECT DIRECTORY WHICH CONTAINS REAL WINDOWS"
  echo
  echo "If unsure, just hit enter and ~/c will be used."
  echo -n "Virtual C drive path ? :"
  strinput
  if [ "$refstr" = "" ]
  then
   cdrivedir="$homedir/c"
  else
   cdrivedir="`eval echo $refstr`"
  fi
  
 else
  cdrivedir="$homedir/c"
 fi
fi

# Setup

echo
echo "SETUP STAGE 1"

echo "Stopping wine process ..."
if [ -e ~/.wine ]
 then
 if [ `which wineserver` ]
 then
  wineserver -k
 else
  /usr/lib/wine/wineserver -k
 fi
else
 # Sometimes, user has erased ~/.wine while wine is still running.
 # Let wineserver quit safely.
 mkdir ~/.wine
 if [ `which wineserver` ]
 then
  wineserver -k
 else
  /usr/lib/wine/wineserver -k
 fi
 rm -rf ~/.wine
fi

if [ -e ~/.wine ]
then
 if [ "$silentinst" = "n" ]
 then
  echo
  echo "WARN: Found wine configuration."
  echo "This setup program will backup ~/.wine to ~/.wine.$today"
  echo -n "Continue(y/n)?"
  yn_answer
 else
  refstr=y
 fi
 
 if [ "$refstr" = "y" ]
 then
  mv ~/.wine ~/.wine.$today
  echo "renamed ~/.wine to ~/.wine.$today"
 else
  echo "Setup aborted."
  exit 0
 fi
fi

if [ -e $cdrivedir ]
then
 if [ "$silentinst" = "n" ]
 then
  echo
  echo "WARN: Found $cdrivedir directory."
  echo "This setup program will backup $setupdir to $setupdir.$today"
  echo -n "Continue(y/n)?"
  yn_answer
 else
  refstr=y
 fi

 if [ "$refstr" = "y" ]
 then
  mv $cdrivedir $cdrivedir.$today
  echo "renamed $cdrivedir to $cdrivedir.$today"
 else
  echo "Setup aborted."
  exit 0
 fi
fi

echo "Creating ~/.wine ..."
mkdir ~/.wine
mkdir ~/.wine/dosdevices
mkdir ~/.wine/sidenet

echo "Setting up install-time configuration ..."
cp $setupdir/wineconfig/config.safe ~/.wine/config

echo "Setting up wine base directory structure ..."

# This part is from wineprefixcreate script

CROOT=$cdrivedir
mkdir $cdrivedir

# Create the directory tree

for i in \
 "$CROOT/windows" \
 "$CROOT/windows/command" \
 "$CROOT/windows/fonts" \
 "$CROOT/windows/inf" \
 "$CROOT/windows/system" \
 "$CROOT/windows/system32" \
 "$CROOT/windows/system32/drivers" \
 "$CROOT/windows/temp"
do

    [ -d "$i" ] || mkdir "$i"
done

if [ ! -e $cdrivedir/windows ]
then
 echo "ERROR: C drive installation failed."
 echo "       Please check if you have a correct permission."
 exit 1
fi

if [ "$wcslang" = "de" ]
then
 ln -s $cdrivedir/Program\ Files $cdrivedir/Programme
fi


echo "Setting up drives ..."
ln -s $cdrivedir ~/.wine/dosdevices/c\:
ln -s ~/ ~/.wine/dosdevices/d\:
ln -s / ~/.wine/dosdevices/z\:

if [ ! -e $tempdir ]
then
 echo "Creating temporary directory ($tempdir) ..."
 mkdir -p $tempdir
fi

if [ -e /mnt/cdrom ]
then
 echo "Linked /mnt/cdrom to e drive."
 ln -s /mnt/cdrom ~/.wine/dosdevices/e\:
elif [ -e /media/cdrom ]
then
 echo "Linked /media/cdrom to e drive."
 ln -s /media/cdrom ~/.wine/dosdevices/e\:
else [ -e /cdrom ]
 echo "Linked /cdrom to e drive."
 ln -s /cdrom ~/.wine/dosdevices/e\:
fi

if [ "$dotfont" = "y" ]
then
 if [ ! -e ~/.fonts ]
 then
  echo "Creating ~/.fonts ..."
  mkdir ~/.fonts
 fi
 rm -rf $cdrivedir/windows/fonts
 echo "Linked $cdrivedir/windows/fonts -> ~/.fonts"
 ln -s ~/.fonts $cdrivedir/windows/fonts
fi

echo "Installing base font ..."
cp $setupdir/fonts/*.ttf $cdrivedir/windows/fonts

echo
echo "SETUP STAGE 2"
echo "Setting up default registry ..."
echo >> $logfile
echo "-- Default registry --">> $logfile
cp $setupdir/inf/wine.inf $cdrivedir/windows/inf/wine.inf
wine rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 wine.inf 2>&1 |cat >> $logfile

echo
echo "Setting up sidenet-spicific configuration ..."
echo >> $logfile
echo "--  wcs.inf --">> $logfile
cp $setupdir/inf/wcs.inf $cdrivedir/windows/inf/wcs.inf
wine rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 wcs.inf 2>&1 |cat >> $logfile

echo
echo "Setting up language pack for Language : $wcslang ..."
echo >> $logfile
echo "--  Language Pack --">> $logfile
cp $setupdir/inf/wcs$wcslang.inf $cdrivedir/windows/inf/wcs$wcslang.inf
wine rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 wcs$wcslang.inf 2>&1 |cat >> $logfile

if [ -e /usr/local/lib/wine ]
then
 echo "Install prefix is /usr/local/lib/wine"
 echo >> $logfile
 echo "Install prefix is /usr/local/lib/wine">> $logfile
 instprefix="/usr/local/lib/wine"
elif [ -e /usr/lib/wine ]
then
 echo "Install prefix is /usr/lib/wine"
 echo >> $logfile
 echo "Install prefix is /usr/lib/wine">> $logfile
 instprefix="/usr/lib/wine"
else
 echo "WARN: Wine installation not found."
 echo "      Some application will not work propery."
 instprefix=""
fi

dlldir=$instprefix

# This part is from wineprefixcreate script
# Create the application symlinks

link_app()
{
   rm -f "$2" && ln -s "$dlldir/$1.exe.so" "$2" || echo "Warning: failed to create $2"
}

#don't link regsvr32 and msiexec

if [ ! "$instprefix" = "" ]
then
 link_app start		"$CROOT/windows/command/start.exe"
 link_app notepad	"$CROOT/windows/notepad.exe"
 link_app regedit	"$CROOT/windows/regedit.exe"
 link_app rundll32	"$CROOT/windows/rundll32.exe"
 link_app wcmd		"$CROOT/windows/system32/wcmd.exe"
 link_app control	"$CROOT/windows/system32/control.exe"
 link_app winefile	"$CROOT/windows/system32/fileman.exe"
 #link_app winhelp	"$CROOT/windows/system32/help.exe"
 #link_app msiexec	"$CROOT/windows/system32/msiexec.exe"
 link_app notepad	"$CROOT/windows/system32/notepad.exe"
 link_app progman	"$CROOT/windows/system32/progman.exe"
 #link_app regsvr32	"$CROOT/windows/system32/regsvr32.exe"
 link_app taskmgr	"$CROOT/windows/system32/taskman.exe"
 link_app winemine	"$CROOT/windows/system32/winmine.exe"
 link_app winver	"$CROOT/windows/system32/winver.exe"
 link_app uninstaller	"$CROOT/windows/uninstall.exe"
 #link_app winhelp	"$CROOT/windows/winhelp.exe"
 #link_app winhelp	"$CROOT/windows/winhlp32.exe"
 link_app winebrowser	"$CROOT/windows/winebrowser.exe"
 link_app winecfg	"$CROOT/windows/winecfg.exe"
fi

link_dll()
{
   rm -f "$2" && ln -s "$dlldir/$1.dll.so" "$2" || echo "Warning: failed to create $2"
}

if [ ! "$instprefix" = "" ]
then
 link_dll d3d8		"$CROOT/windows/system/d3d8.dll"
 link_dll d3d9		"$CROOT/windows/system/d3d9.dll"
 link_dll d3d8		"$CROOT/windows/system32/d3d8.dll"
 link_dll d3d9		"$CROOT/windows/system32/d3d9.dll"
fi

if [ "$w32codecs" = "y" ]
then
 echo "Installing MPlayer codec package ..."
 echo >> $logfile
 echo "--  w32codec install --">> $logfile
 ln -s $w32codecsdir/ir41_32.dll $cdrivedir/windows/system/ir41_32.dll
 ln -s $w32codecsdir/iccvid.dll $cdrivedir/windows/system/iccvid.dll
 ln -s $w32codecsdir/ir32_32.dll $cdrivedir/windows/system/ir32_32.dll
 ln -s $w32codecsdir/ir50_32.dll $cdrivedir/windows/system/ir50_32.dll
 ln -s $w32codecsdir/msvidc32.dll $cdrivedir/windows/system/msvidc32.dll
 ln -s $w32codecsdir/msrle32.dll $cdrivedir/windows/system/msrle32.dll
 ln -s $w32codecsdir/tsd32.dll $cdrivedir/windows/system/tsd32.dll
 ln -s $w32codecsdir/imaadp32.acm $cdrivedir/windows/system/imaadp32.acm
 ln -s $w32codecsdir/lhacm.acm $cdrivedir/windows/system/lhacm.acm
 ln -s $w32codecsdir/msadp32.acm $cdrivedir/windows/system/msadp32.acm
 ln -s $w32codecsdir/msgsm32.acm $cdrivedir/windows/system/msgsm32.acm
 ln -s $w32codecsdir/tssoft32.acm $cdrivedir/windows/system/tssoft32.acm

 echo "Registering MPlayer codec package ..."
 cp $setupdir/inf/w32codecs.inf $cdrivedir/windows/inf/w32codecs.inf
 wine rundll32.exe setupapi.dll,InstallHinfSection DefaultInstall 128 w32codecs.inf 2>&1 |cat >> $logfile
fi

echo "Installing true.exe dummy application ..."
cp $setupdir/true/true.exe $cdrivedir/windows/explorer.exe

echo "Installing ShellLinker ..."
cp $setupdir/shelllink/*.exe $cdrivedir/windows

echo "Creating shortcut: Start -> Programs -> File Magager"
wine shelllink mkdir __startmenu__\\Programs\\
wine shelllink c:\\windows\\system32\\fileman.exe __startmenu__\\Programs\\File\ Manager.lnk
echo "Creating shortcut: Start -> Programs -> Program Magager"
wine shelllink c:\\windows\\system32\\progman.exe __startmenu__\\Programs\\Program\ Manager.lnk

echo "Creating shortcut: Start -> Control Panel"
wine shelllink c:\\windows\\system32\\control.exe __startmenu__\\Programs\\Control\ Panel.lnk
echo "Creating shortcut: Start -> Application Uninstaller"
wine shelllink c:\\windows\\uninstall.exe __startmenu__\\Programs\\Application\ Uninstaller.lnk
echo "Creating shortcut: Start -> Wine Configuration"
wine shelllink c:\\windows\\winecfg.exe __startmenu__\\Programs\\Wine\ Configuration.lnk
echo "Creating shortcut: Start -> Task Manager"
wine shelllink c:\\windows\\system32\\taskman.exe __startmenu__\\Programs\\Task\ Manager.lnk

wine shelllink mkdir __startmenu__\\Programs\\Accesories\\
echo "Creating shortcut: Start -> Programs -> Accesories -> Notepad"
wine shelllink c:\\windows\\notepad.exe __startmenu__\\Programs\\Accesories\\Notepad.lnk
echo "Creating shortcut: Start -> Programs -> Accesories -> Registry Editor"
wine shelllink c:\\windows\\regedit.exe __startmenu__\\Programs\\Accesories\\Registry\ Editor.lnk
echo "Creating shortcut: Start -> Programs -> Accesories -> Mine Sweeper"
wine shelllink c:\\windows\\system32\\winemine.exe __startmenu__\\Programs\\Accesories\\Mine\ Sweeper.lnk

if [ `which wineserver` ]
then
 wineserver -w
else
 /usr/lib/wine/wineserver -w
fi

echo
echo "SETUP STAGE 3"

echo "Setting up wine configuration ..."
if [ -e $setupdir/wineconfig/config.sidenet.$wcslang ]
then
 cp $setupdir/wineconfig/config.sidenet.$wcslang ~/.wine/config
else
 cp $setupdir/wineconfig/config.sidenet ~/.wine/config
fi

if [ "$dcominstall" = "y" ]
then
 echo "Setting up DCOM98 ..."
 echo >> $logfile
 echo "--  DCOM98 installation --">> $logfile
 WINEDLLOVERRIDES="ole32=n" wine $tempdir/dcom98.exe 2>&1 | cat >> $logfile
 echo "DCOM98 setup completed."
 if [ `which wineserver` ]
 then
  wineserver -w
 else
  /usr/lib/wine/wineserver -w
 fi
fi

if [ -e $tempdir/mfc40.dll ] || [ -e $tempdir/MFC40.DLL ]
then
 echo "Installing mfc40.dll ..."
 echo >> $logfile
 echo "--  MFC 4.0 installation --">> $logfile
 if [ -e $tempdir/mfc40.dll ]
 then
  cp $tempdir/mfc40.dll $cdrivedir/windows/system/mfc40.dll
 else
  cp $tempdir/MFC40.DLL $cdrivedir/windows/system/mfc40.dll
 fi

fi

if [ "$ie6download" = "y" ]
then
 echo "Downloading Internet Explorer 6 setup ..."
 echo >> $logfile
 echo "--  IE6 download --">> $logfile
 wget http://download.microsoft.com/download/ie6sp1/finrel/6_sp1/W98NT42KMeXP/$mslang/ie6setup.exe -O $tempdir/ie6setup_$mslang.exe
 if [ ! -e $tempdir/ie6setup_$mslang.exe ]
 then
  echo "ERROR: Download failed, Please download ie6setup.exe manually and try again."
  echo "Setup aborted."
  exit 1
 fi
fi

if [ "$ie6install" = "y" ]
then

echo >> $logfile
echo "--  IE6 installation --">> $logfile

echo "Removing old iebatch.txt ..."
rm -f $setupdir/iebatch.txt

if [ "$silentinst" = "y" ]
then
 echo "Enabling batch install ..."
 echo >> $logfile
 echo "--  Batch install --">> $logfile
 if [ "$mp6install" = "y" ]
 then
  cp $setupdir/iebatch/iebatch_wmp6.txt $tempdir/iebatch.txt
 else
  cp $setupdir/iebatch/iebatch.txt $tempdir/iebatch.txt
 fi
fi

echo "Setting up Internet Explorer 6 ..."

declare -i counter
counter=0

while [ ! -e $cdrivedir/Program\ Files/Internet\ Explorer/IEXPLORE.EXE ]
 do {
 if [ -e $tempdir/ie6setup.exe ] || [ -e $tempdir/IE6SETUP.EXE ]
 then
  wine $tempdir/ie6setup.exe 2>&1 |cat >> $logfile
 else
  wine $tempdir/ie6setup_$mslang.exe 2>&1 |cat >> $logfile
 fi
  
  if [ `which wineserver` ]
  then
   wineserver -w
  else
   /usr/lib/wine/wineserver -w
  fi
  echo "Checking if ie6setup needs to be started again ..."
 counter=$counter+1
 if [ "$counter" = "5" ]
 then
  echo "ERROR: ie6setup respawned too many times."
  echo "Setup aborted."
  exit 1
 fi
 }
done

echo "Internet Explorer 6 setup completed."

if [ ! -e $cdrivedir/windows/system/mfc40.dll ]
then
 echo "Disabling ieinfo5.ocx ..."
 mv $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX \
 $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX.bak
 cp $setupdir/true/trueocx.ocx $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX
fi

echo "Simulating reboot ... This may take some time."
wineboot 2>&1 |cat >> $logfile
wineboot

if [ -e $cdrivedir/Program\ Files/Internet\ Explorer/IEXPLORE.EXE ]
then
 echo "Creating shortcut: Start -> Programs -> Internet Explorer"
 wine shelllink c:\\Program\ Files\\Internet\ Explorer\\IEXPLORE.EXE __startmenu__\\Internet\ Explorer.lnk
 echo "Creating shortcut: Desktop -> Internet Explorer"
 wine shelllink c:\\Program\ Files\\Internet\ Explorer\\IEXPLORE.EXE __desktop__\\Internet\ Explorer.lnk
fi

if [ -e $cdrivedir/Program\ Files/Windows\ Media\ Player/mplayer2.exe ]
then
 echo "Creating shortcut: Start -> Programs -> Windows Media Player 6.4"
 wine shelllink c:\\Program\ Files\\Windows\ Media\ Player\\mplayer2.exe __startmenu__\\Programs\\Windows\ Media\ Player\ 6.4.lnk
fi

if [ `which fc-cache` ]
then
 echo "Updating Font Cache ..."
 fc-cache
fi

if [ -e $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX.bak ]
then
 echo "Restoring ieinfo5.ocx ..."
 rm -f $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX
 mv $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX.bak \
 $cdrivedir/Program\ Files/Common\ Files/Microsoft\ Shared/MSInfo/IEINFO5.OCX
fi

echo "Internet Explorer setup completed."

fi

if [ "$msiinstall" = "y" ]
then
 echo >> $logfile
 echo "-- InstMsiA installation --">> $logfile
 WINEDLLOVERRIDES="msi=n" wine $tempdir/instmsia.exe 2>&1 |cat >> $logfile
fi

echo "Opening: http://sidenet.ddo.jp/winetips/wcsstart190.html"
echo "This command fails if no browser installed."
echo >> $logfile
echo "--  Show start page --">> $logfile
wine start http://sidenet.ddo.jp/winetips/wcsstart190.html 2>&1 |cat >> $logfile

echo
echo "All setup completed."

