#!/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
#		This function returns:
#		0 - If all setup/mounts are correct
#		1 - If /opt/midas/var is missing, this should stop the MC from
#		    starting but GUI will run to allow updating/downgrading
#		2 - If /mnt/disc-on-chip is missing, as this would cause
#		    updating to fail both MC and GUI should refuse to launch
#		4 - If this script is run on an unsupported platform
#
#
########################################################################

checksetup()
{
	RETVAL=0
	PLATFORM=$1

	if [ "${PLATFORM}" == "DL1" ] || [ "${PLATFORM}" == "DL2" ]; then
		echo "DL1/2 not currently supported"
		RETVAL=4
		return
	elif [ "${PLATFORM}" == "XL8" ]; then
		echo "XL8 not currently supported"
		RETVAL=4
		return
	elif [ "${PLATFORM}" == "" ]; then
		echo "No platform specified"
		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

	# Check /opt/midas/var is mounted
	if [ -z "`mount | grep /opt/midas/var`" ]; then
		RETVAL=1
	fi

	# Check /mnt/disc-on-chip is mounted
	if [ -z "`mount | grep /mnt/disc-on-chip`" ]; then
		RETVAL=2
		return
	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
}
