#!/bin/sh
########################################################################
# Begin $/opt/midas/bin/launch-script
#
# Description : Launch the application
#
# Authors     : Stefan Cook
#
# Version     : 00.00
#
# Notes       : Do necessary house keeping and launch the application
#
########################################################################

. /etc/rc.d/init.d/functions
. /opt/midas/bin/check-setup

# Launch the shutdown monitor daemon.
#/opt/midas/bin/shutdown-monitor &

# If application didn't stop respawn
if [ ! -e /var/norespawn ]; then
	
	#
	# Start the GUI on a GUI/MC platform; i.e. for now a DL3.
	#
	
	# find out what to launch
	whatami TYPE ID
	
	# and launch it
	if [ "${ID}" != "?" ]; then

		checksetup DL3 GUI
		if [ ${RETVAL} -gt 1 ]; then
			echo "Both /mnt/disc-on-chip and /opt/midas/var failed to mount correctly, refusing to start GUI"
			exit 1
		fi

		# start the GUI
		if [ ! -e /var/cache-sod-check-complete ]; then
			# pause on initial startup to reduce contension.
			sleep 5
		fi
		
		# If configured for diagnostic execution then enable signal handling
		grep --silent /opt/midas/var/logs /etc/syslog.conf
		if [ $? -eq 0 ]; then
			# Ensure the crash directory exists
			if [ ! -d /opt/midas/var/logs/crash ]; then
				mkdir /opt/midas/var/logs/crash
			fi
			
			# Start GUI with signal handling enabled.
			taskset 0x00000001 /opt/midas/bin/marmaset enable-signal-handler --dfb:depth=32 > /dev/console 2>&1
		else
			# Normal startup
			taskset 0x00000001 /opt/midas/bin/marmaset --dfb:depth=32
		fi
	fi
fi

if [ -e /var/runupdater ]; then
	if [ -e /mnt/disc-on-chip/DL3Update ]; then
		# clear out any crap that might be remaining
		rm -r -f /mnt/disc-on-chip/DL3Update/*
	else
		mkdir /mnt/disc-on-chip/DL3Update
	fi
	if [ ! -e /mnt/disc-on-chip/keycopy ]; then
		# make the replacement temp directory
		mkdir /mnt/disc-on-chip/keycopy
	fi
	chvt 9
	echo -n -e "\033[2J\033[?25l\033[?1c\033[9;0]\n\n      Launching the Updater. Please wait..." >> /dev/tty9
	rm -r -f /mnt/disc-on-chip/Updater
	cd /mnt/disc-on-chip/;  tar --no-same-owner --occurrence -xf $(cat /var/runupdater) Updater
	/mnt/disc-on-chip/Updater /mnt/disc-on-chip/DL3Update > /dev/tty8 </dev/tty9
	# now clear our temporary files, leaving the keycopy directory
	rm -r -f /mnt/disc-on-chip/keycopy/*
	rm -r -f /mnt/disc-on-chip/Updater
 	chvt 1
fi 

# If not respawning
if [ -e /var/norespawn ]; then
	# drop to bash
#	taskset 0x00000002 /sbin/agetty -I '\033(K' ttyS2 9600
	/sbin/agetty -I '\033(K' ttyS2 9600
fi

