#!/bin/sh
echo
echo "Installing Jaws 2.4 and JawsService for Linux."
echo "=============================================="
echo

if [ "`whoami`" != "root" ]; then
        echo "You need to be root to run this script. You can become root"
        echo "by using the command su. Please do this, then restart the"
        echo "installation with ./install_s6"
        exit 1
fi

if [ ! -d /usr/local/SpeedSix/dl/bin ]; then
	echo "SpeedSix Monsters for Autodesk Discreet do not appear to have"
	echo "been installed on this system. That is an essential pre-requisite."
	echo "Exiting now."
	exit 1
fi

if [ -f /etc/init.d/JawsService ]; then
	echo "JawsService seems to have been installed already."
	echo "Attempting to stop JawsService if it is running."
	/etc/init.d/JawsService stop
	sleep 5
	echo "Attempting to delete previous JawsService components."
	/sbin/chkconfig JawsService off
	if [ -d /usr/local/SpeedSix/jaws2.4 ]; then
		rm -rf /usr/local/SpeedSix/jaws2.4
	fi
	rm -f /usr/local/SpeedSix/dl/bin/JawsService
	rm -f /etc/init.d/JawsService
	rm -f /etc/sysconfig/JawsService
fi

echo "Extracting new Jaws 2.4 installation ..."
gzip -d s6_jaws_linux.tgz
tar xf s6_jaws_linux.tar

echo "Installing new Jaws 2.4 subsystem ..."
./s6_jaws_linux_install
#
# The following ensures that we have a ONE JOB SERVER on Unix ... Gulp!
#
sed -e 's/onejobonly false/onejobonly true/g' /usr/local/SpeedSix/jaws2.4/lib/init.ps > ./XXXinit.ps
rm -f /usr/local/SpeedSix/jaws2.4/lib/init.ps
cp ./XXXinit.ps /usr/local/SpeedSix/jaws2.4/lib/init.ps

echo "Installing new JawsService subsystem ..."
cp JawsService /usr/local/SpeedSix/dl/bin
cp init.d/JawsService /etc/init.d
# The following command marks the service for start on reboot.
/sbin/chkconfig JawsService on

echo "Cleaning up ..."
rm -f s6_jaws_linux.tgz
rm -f s6_jaws_linux.tar
rm -rf init.d
rm -rf sysconfig
rm -f JawsService s6_jaws2.4_linux.tar.gz s6_jaws_linux_install XXXinit.ps readme-jaws-linux.txt s6_jaws2.4_linux.tar.gz README-LinuxJawsServiceKit.txt

echo "Starting JawsService ..."
/etc/init.d/JawsService start

echo "Done."
echo "====="
echo

