#!/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

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

# If application didn't stop respawn
if [ ! -e /var/norespawn ]; then

    # paranoid deletion of old-style marmaserver logs (BIR-488)
    find /opt/midas/var/logs/marmaserver -name "*.log" -type f -delete || true &> /dev/null

	# find out what to launch
	whatami TYPE ID

	# and launch it
	if [ "${ID}" != "?" ]; then
		if [ "${TYPE}" = "GUI" ]; then
			# redirect the hotplug event to our handler
			echo "/opt/midas/bin/hotplug-script" > /proc/sys/kernel/hotplug
			
			cd /opt/midas/bin
			
			# probe for cold plugged USB keys
			#  (those already present on boot)
			./usbhotplug -probe
			
			# start the GUI
			#stty sane 57600 < /dev/ttyS1
			# 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.
				./marmaset enable-signal-handler --dfb:depth=32 > /dev/console 2>&1
			else
				# Normal startup
				./marmaset --dfb:depth=32 > /dev/console 2>&1
			fi
			# Create /opt/midas/var/system folder if it does not exist
			if [ ! -d /opt/midas/var/system ]; then
				mkdir /opt/midas/var/system
			fi
			# Create symlink to UK-localtime file if localtime doesn't exist 
			if [ ! -e /opt/midas/var/system/localtime ]; then
				ln -s /etc/UK-localtime /opt/midas/var/system/localtime
			fi
		else
			if [ "${TYPE}" = "MC" ]; then
				# redirect the hotplug event to our handler
				echo "/opt/midas/bin/hotplug-script" > /proc/sys/kernel/hotplug
				
				cd /opt/midas/bin
				
				# probe for cold plugged USB keys
				#  (those already present on boot)
				./usbhotplug -probe
				
				# Create preset library template if it doesn't exist
				PRESET_LIBRARY_FILE=/opt/midas/var/templates/default.presets
				if [ ! -e ${PRESET_LIBRARY_FILE} ]; then
					./mastercontroller create-preset-library-template ${PRESET_LIBRARY_FILE}
				fi
				
				# Create show template if it doesn't exist
				SHOW_FILE=/opt/midas/var/templates/default.show
				if [ ! -e ${SHOW_FILE} ]; then
					./mastercontroller create-show-template ${SHOW_FILE}
				fi
				
				# Mount the disc-on-chip device if not already mounted
				if [ -z "`mount | grep /mnt/disc-on-chip`" ]; then
					# Attempt to mount the device if it is present
					if [ -e /dev/hda1 ]; then
						mount /dev/hda1 /mnt/disc-on-chip
					fi
				fi
				
				# Create the 'system' and 'projects' directories on the disc-on-chip if it
				# is present.
				if [ -n "`mount | grep /mnt/disc-on-chip`" ]; then
					# If the system directory doenn't exist then attempt to
					# create it.
					if [ ! -d /mnt/disc-on-chip/system ]; then
						mkdir /mnt/disc-on-chip/system
					fi
					# If the projects directory doesn't exist then attempt to
					# create it.
					if [ ! -d /mnt/disc-on-chip/projects ]; then
						mkdir /mnt/disc-on-chip/projects
					fi
					# And extract the dsp projects file to this folder for the mastercontroller
					tar -zxf /opt/midas/bin/projects/*.tar.gz -C /mnt/disc-on-chip/projects
	
				fi
				# Create /opt/midas/var/system folder if it does not exist
				if [ ! -d /opt/midas/var/system ]; then
					mkdir /opt/midas/var/system
				fi
				# Create symlink to UK-localtime file if localtime doesn't exist 
				if [ ! -e /opt/midas/var/system/localtime ]; then
					ln -s /etc/UK-localtime /opt/midas/var/system/localtime
				fi
				
				# If DSP discovery agent not already started
				if [ -z "`ps -AH | grep pandad`" ]; then
					# start it
					./pandad -d &
				fi
				
				# Start the MC
				# 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 MC with signal handling enabled.
					./mastercontroller enable-signal-handler enable-cache
				else
					# Normal startup
					./mastercontroller enable-cache
				fi
			fi
		fi
	fi
fi

# this works, because /var/runupdater should never exist on a MC
if [ -e /var/runupdater ]; then
	if [ -e /opt/midas/var/DL3Update ]; then
		# clear out any crap that might be remaining
		rm -r -f /opt/midas/var/DL3Update/*
	else
		mkdir /opt/midas/var/DL3Update
	fi
	if [ ! -e /opt/midas/var/keycopy ]; then
		# make the replacement temp directory
		mkdir /opt/midas/var/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 /opt/midas/var/Updater
	cd /opt/midas/var/;  tar --no-same-owner -xf $(cat /var/runupdater) Updater
	/opt/midas/var/Updater /opt/midas/var/DL3Update > /dev/tty8 </dev/tty9
	# now clear our temporary files, leaving the keycopy directory
	rm -r -f /opt/midas/var/keycopy/*
	rm -r -f /opt/midas/var/Updater
 	chvt 1
fi

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

