#!/bin/sh
########################################################################
# Begin $/opt/midas/bin/check-setup
#
# Description : Launch the application
#
# Authors     : Joshua Harley
#
# Version     : 00.00
#
# Notes       : Provide housekeeping functionality for launch scripts
#
#
########################################################################

checksetup()
{
	RETVAL=0
	PLATFORM=$1

	if [ "${PLATFORM}" == "DL3" ]; then
		MIDAS_VAR=/dev/hda3
		CF_DOC=/dev/hda4
		NV_DOC=/dev/sda1
	elif [ "${PLATFORM}" == "DL1" ] || \
	     [ "${PLATFORM}" == "DL2" ] || \
	     [ "${PLATFORM}" == "PROX" ]; then
		MIDAS_VAR=/dev/midas_var
		CF_DOC=/dev/disc-on-chip
	elif [ "${PLATFORM}" == "XL8" ]; then
		MIDAS_VAR=/dev/hda3
		NV_DOC=/dev/sda1
	else
		echo "Not a valid platform!!"
		RETVAL=4
		return
	fi

	# redirect the hotplug event to our handler
	cd /opt/midas/bin
	echo "/opt/midas/bin/hotplug-script" > /proc/sys/kernel/hotplug

	# probe for cold plugged USB keys
	#  (those already present on boot)
	./usbhotplug -probe

	# Mount /opt/midas/var if not already mounted
	if [ -z "`mount | grep /opt/midas/var`" ]; then
		# Attempt to mount the device if it is present
		if [ -e ${MIDAS_VAR} ]; then
			mount ${MIDAS_VAR} /opt/midas/var
		else
			RETVAL=1
		fi
	fi

	# Mount the disc-on-chip device if not already mounted
	if [ ! -e /mnt/disc-on-chip ]; then
		cd /mnt
		
		if [ "${PLATFORM}" == "DL3" ]; then
			if [ -z "`mount | grep /mnt/nv_doc`" ]; then
				if [ -z "`mount | grep /mnt/cf_doc`" ]; then
					mount ${CF_DOC} /mnt/cf_doc
					if [ $? -eq 0 ]; then
						ln -s cf_doc disc-on-chip
					else
						echo "Error: No disc-on-chip!"
						RETVAL=2
						return
					fi
				else
					ln -s cf_doc disc-on-chip
				fi
			else
				ln -s nv_doc disc-on-chip
			fi
		elif [ "${PLATFORM}" == "XL8" ]; then
			if [ -z "`mount | grep /mnt/disc-on-chip`" ]; then
				mount ${NV_DOC} /mnt/disc-on-chip
				if [ $? -eq 0 ]; then
					echo "Error: No disc-on-chip!"
					RETVAL=2
					return
				fi
			fi
		else #Must be DL1/2/ProX
			if [ -z "`mount | grep /mnt/disc-on-chip`" ]; then
				mount ${NV_DOC} /mnt/disc-on-chip
				if [ $? -eq 0 ]; then
					echo "Error: No disc-on-chip!"
					RETVAL=2
					return
				fi
			fi
		fi
	fi
	
	# 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
}
