#!/bin/sh
# Script to install Church Windows in a clean Wine directory
# This is rediculously hard, because Church Windows thinks it requires IE6,
# and because it needs but isn't distributed with mfc40.dll, mfc42.dll, and
# the VB 6 runtime
# (C) Dan Kegel 2005

# Verify no existing wine
if test -d $HOME/.wine; then
   echo Please blow away $HOME/.wine first
   exit 1
fi

# Initialize .wine
wcmd /c echo foo > /dev/null

# Get MFC binaries
if ! test -f mfc40.tgz; then
   # Make a fake 'start' command to work around
   # http://bugs.winehq.org/show_bug.cgi?id=1370
   # when running mfc40 installer
   # Really, start.exe should be a symlink to start.exe.so like wcmd.exe, 
   # not sure why it isn't
   cat > $HOME/.wine/drive_c/windows/system32/start.bat <<_EOF_
%2
_EOF_
   test -f MFC40i.exe || wget http://download.microsoft.com/download/sql65/Patch/6.5/WIN98/EN-US/MFC40i.exe
   clear
   echo "** When the gui comes up, have it install to c:\\windows\\system32 !"
   sleep 2
   wine MFC40i.exe

   while ! test -f $HOME/.wine/drive_c/windows/system32/mfc40.dll; do
     sleep 1
   done
   tar -C $HOME -czvf mfc40.tgz .wine/drive_c/windows/system32/mfc40.dll
fi


if ! test -f mfc42.tgz; then
   test -f vc6redistsetup_enu.exe || wget http://download.microsoft.com/download/vc60pro/update/1/w9xnt4/en-us/vc6redistsetup_enu.exe
   clear
   echo "** When the GUI comes up, tell it to install to c:\\foo !"
   sleep 2
   wine vc6redistsetup_enu.exe
   if ! test -f $HOME/.wine/drive_c/foo/vcredist.exe; then
      echo vcredist.exe not found
      exit 1
   fi
   wine $HOME/.wine/drive_c/foo/vcredist.exe

   while ! test -f $HOME/.wine/drive_c/windows/system32/mfc42.dll; do
     sleep 1
   done
   tar -C $HOME -czvf mfc42.tgz .wine/drive_c/windows/system32/mfc42.dll
fi

# OK, blow away the .wine that has been modified by the
# above installs, initialize a new one, then restore just the DLLs we need
# (I'm being overly clean here - the extra files probably don't hurt anything)
wineserver -k

rm -rf ~/.wine
wcmd /c echo foo > /dev/null


# Install MFC40 to work around following error in CW setup:
#err:module:import_dll Library MFC40.DLL (which is needed by L"C:\\windows\\system32\\THREED32.OCX") not found
tar -C $HOME -xzvf mfc40.tgz

# Install MFC42 to work around following error in CW setup:
#err:module:import_dll Library MFC42.DLL (which is needed by "C:\\windows\\system32\\CSText32.ocx") not found
tar -C $HOME -xzvf mfc42.tgz

# Kill the wineserver again so we can modify the registry
# (Yeah, I should use regedit instead...)
wineserver -k

# Fake IE per workaround in http://bugs.winehq.org/show_bug.cgi?id=3453
cat >> $HOME/.wine/system.reg <<"_EOF_"

[Software\\Microsoft\\Internet Explorer]
"Version"="6.0.2900.2180"
_EOF_

# Install VB runtime, else you get
# err:module:import_dll Library MSVBVM60.DLL (which is needed by L"C:\\Program Files\\Common Files\\Computer Helper\\Cwbup.dll") not found
test -f vbrun60.exe || wget http://download.microsoft.com/download/vb60pro/install/6/win98me/en-us/vbrun60.exe
wine vbrun60.exe

# Download and install Church Windows trial
test -f CW2005Trial.exe || wget -O CW2005Trial.exe http://209.190.80.19/trial/CW%202005%20Trial.exe

wine CW2005Trial.exe > cw.log 2>&1

# Install should succeed!

# Wait for install to finish
# (Should have used start /w to start installer, maybe...)
while ! test -f $HOME/.wine/drive_c/windows/fonts/USPSBar.ttf ; do
  sleep 1
done

# Now move away all fonts installed by CW to work around horrible font
# bug described in http://bugs.winehq.org/show_bug.cgi?id=3624

mkdir $HOME/.wine/drive_c/windows/badfonts
mv $HOME/.wine/drive_c/windows/fonts/*.* $HOME/.wine/drive_c/windows/badfonts


