#!/bin/bash
#       interact
#   Simple script to launch Interact
#   The working wine directory is ~/.interact
#   If ~/.interact is not found, it will be created.

find_ia_path()
{
        if [ -z $IA_PATH ]
        then
                mypath=`which $0`
                if [ -L $mypath ] # called through a sym-link
                then
                        mypath=`ls -l $mypath | sed -e 's/^.*-> //'`
                fi
                IA_PATH=`dirname $mypath | sed -e 's/\/bin//'`
        fi
        # quick check
        if [ ! -x $IA_PATH/bin/ebeam-server -a ! -x wine ]
        then
                echo Error in installation
                return 1
        fi
        export IA_PATH
        return 0
}

find_ia_path
if [ ! $? = 0 ]
then
        exit 1
fi

# setup wine variables - use the wine that is bundled with interact.
export WINELOADER=$IA_PATH/wine/bin/wine
export WINESERVER=$IA_PATH/wine/bin/wineserver
export WINEDLLPATH=$IA_PATH/wine/lib/wine
export WINEBINPATH=$IA_PATH/wine/bin
export WINEPREFIX=~/.interact
export LD_LIBRARY_PATH=$IA_PATH/wine/lib

SERV_ONLY=1
SKIP_SERV=1
SCRAPBOOK=1

if [ $# -gt 0 ]; then
	if [ $1 = "--server-only" ]; then
		SERV_ONLY=0
        elif [ $1 = "--scrapbook" ]; then
                SCRAPBOOK=0
	fi
fi

if [ ! -d $WINEPREFIX ]
then
	# need to create interact dir structure on home (first time running?)
	$WINEBINPATH/wineboot -u
	i=0
	while [ ! -r $WINEPREFIX/system.reg ]
	do
		((i++))
		if (($i>10))
		then
			echo Timeout waiting for system.reg
			exit 1
		fi
		sleep 1
	done
fi

# get the path to program files
cd $WINEPREFIX
PROG_FILES=`fgrep '"ProgramFiles"' system.reg | sed s/\"ProgramFiles\"=\"C...// | sed s/\"//`

if [ "$PROG_FILES"x = x ]; then 
        echo unable to find ProgramFiles in ~/.interact/system.reg
        exit 1
fi

# Add registry key for the gallery
cat > gallery.reg <<EOF
REGEDIT4

[HKEY_CURRENT_USER\Software\Luidia\eBeam Interactive\Interactive Clip-Art Gallery\GalleriesDirectory]
"n1"="Sample"
"p1"="Z:\\\\opt\\\\interact\\\\SampleGallery"
EOF

$WINEBINPATH/regedit gallery.reg
rm gallery.reg

if [ ! -d "$WINEPREFIX/drive_c/$PROG_FILES/interact" ]
then
	# Create interact dir under c:\Program Files and populate it with
	# sym-links
	mkdir "$WINEPREFIX/drive_c/$PROG_FILES/interact"
	if [ ! $? = 0 ]; then
		echo problem creating interact directory
		exit 1
	fi

	cd "$WINEPREFIX/drive_c/$PROG_FILES/interact"

	for f in $IA_PATH/bin/*
	do
		ln -s $f `basename $f`
	done
	# WORKAROUND: use hhctrl.ocx to fix help bug.
	cp $IA_PATH/wine/share/wine/hhctrl/hhctrl.ocx $WINEPREFIX/drive_c/windows/system32/

    # Install the flash player
    mkdir "$WINEPREFIX/drive_c/windows/system32/Macromed"
    mkdir "$WINEPREFIX/drive_c/windows/system32/Macromed/Flash"
	if [ ! $? = 0 ]; then
		echo problem creating flash directory
		exit 1
	fi
	cp $IA_PATH/wine/share/wine/flash/Flash10c.ocx $WINEPREFIX/drive_c/windows/system32/Macromed/Flash
    $WINEBINPATH/regsvr32 $WINEPREFIX/drive_c/windows/system32/Macromed/Flash/Flash10c.ocx

fi

cd "$WINEPREFIX/drive_c/$PROG_FILES/interact"

# todo: we could validate the version
$WINELOADER --version
if [ ! $? = 0 ]; then
	echo wine not found
	exit 1
fi

# check to see which applications are already running
# check for ebeam-server
ps -e | fgrep -v fgrep | fgrep -q ebeam-server
ebserv=$?
# check for eBeamInteractive
ps -e | fgrep -v fgrep | fgrep -q eBeamInteractiv
ebia=$?

#
# Start ebeam-server
#
if [ $ebserv = 0 -o $SKIP_SERV = 0 ]; then
	echo eBeam Server is already running
        # make sure is_core is set
        $IA_PATH/bin/ebeam-setcore 1
elif [ `readlink /dev/fd/0` = /dev/null ]; then
	# There is no tty, restart in an xterm (need to enter root pw)
	xterm -T "Launch eBeam Server" -e "'$0' --server-only ; echo Done. Press Enter to exit. ; read JUNK"
	# Verify that eBeam Server started
	ps -e | fgrep -v fgrep | fgrep -q ebeam-server
	ebserv=$?
	if [ $ebserv = 1 ]; then
		exit 1
	fi
	SKIP_SERV=0
elif [ $SKIP_SERV = 1 ]; then
	if [ $ebia = 0 ]; then
		# Do clean up
		killall -9 -Ivr 'ebeam'
		ebia=1
	fi
	echo === Starting eBeam Server
	if [ $UID != 0 ]; then
		echo eBeam Server must be launched by root. You may type the root
		echo password below.
                # make sure root can still access X
                xhost + >/dev/null 2>&1
	fi
	su -c 'exec $IA_PATH/bin/ebeam-server --is_core=1 >/dev/null 2>&1 &'
	if [ $? != 0 ]; then
		echo Problem starting eBeam Server
		exit 1;
	fi
	sleep 5 # wait until the server is ready...
fi
if [ $SERV_ONLY = 0 ]; then
	exit 0
fi

#
# Start Interact
#
if [ $ebia = 0 ]; then
	echo eBeam Interact is already running
else
	echo === Starting Interact
	exec $WINELOADER "c:\\$PROG_FILES\\interact\\eBeamInteractive" -linux >/dev/null 2>&1 &
	if [ $SCRAPBOOK = 0 ]; then
		sleep 5 # wait until interact is up before starting scrapbook.
	fi
fi

#
# Start Scrapbook
#
if [ $SCRAPBOOK = 0 ]; then
        echo === Starting Scrapbook
	exec $WINELOADER "c:\\$PROG_FILES\\interact\\eBeamScrapbook" -linux >/dev/null 2>&1 &
fi
