#!/bin/sh
# Script to demonstrate installing Medic Aid Demo
# Dan Kegel Feb '07
set -ex

# Edit these if you're not building from git
#WINESRC=$HOME/wine-git/
WINESRC=
#WINEPREFIXCREATE=${WINESRC}tools/wineprefixcreate
WINEPREFIXCREATE=wineprefixcreate

WINE=${WINESRC}wine
WINESERVER=wineserver

# Kill the existing wine installation just to be clean
$WINESERVER -k || true
rm -rf $HOME/.wine

# Download the app and its prerequisites
# I think mdac 2.6 will do; I used mdac 2.7 here because it's easier to find
test -f demo9.zip || wget http://www.amlamed.com/demo9.zip
test -f mdac_typ.exe || wget http://download.microsoft.com/download/3/b/f/3bf74b01-16ba-472d-9a8c-42b2b4fa0d76/mdac_typ.exe
test -f vc6redistsetup_enu.exe || wget http://download.microsoft.com/download/vc60pro/update/1/w9xnt4/en-us/vc6redistsetup_enu.exe

# Grab msvcirt.dll (should have been bundled with app)
if ! test -f vcredist.exe; then
   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
   cp $HOME/.wine/drive_c/foo/vcredist.exe .
fi
rm -rf tmp
mkdir tmp
cd tmp
cabextract ../vcredist.exe
cp msvcirt.dll ..
cd ..
$WINESERVER -k || true
rm -rf $HOME/.wine

# Fake IE per workaround in http://bugs.winehq.org/show_bug.cgi?id=3453
# needed to install mdac 2.6
$WINEPREFIXCREATE
$WINESERVER -k || true
cat >> $HOME/.wine/system.reg <<"_EOF_"

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

# Install MDAC 
WINEDLLOVERRIDES="odbccp32,odbc32=n" $WINE mdac_typ.exe

# Unpack app, install while using native ODBC so its data source gets recorded
rm -rf tmp
mkdir tmp
cd tmp
unzip ../demo9.zip
# (Say, why does the app's installer return nonzero status?)
WINEDLLOVERRIDES="odbccp32,odbc32=n" $WINE setup.exe || true

# copy msvcirt.dll from the vc6 runtime package into the app's dir
cp ../msvcirt.dll ~/.wine/drive_c/meddemo

# start the app with native ODBC, let user answer first-run questions
cd ~/.wine/drive_c/meddemo
WINEDLLOVERRIDES="odbccp32,odbc32=n" $WINE medicdemo9.exe || true

# Start it again.  Here's where the crashing starts...
WINEDLLOVERRIDES="odbccp32,odbc32=n" $WINE medicdemo9.exe

