#!/bin/bash
#
# Hemisphere GPS startup script for Celestia software.
#
### BEGIN INIT INFO
# Provides:          celestia_controller
# Required-Start:    $remote_fs $syslog can_server
# Required-Stop:     $remote_fs $syslog can_server
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start celestia controller at boot time
# Description:       Enable service provided by celestia_controller.
### END INIT INFO

#set -x

. /etc/ags-celestia/can_messaging_functions
. /etc/ags-celestia/can_messaging_ids
. /etc/ags-celestia/ad_dmu_functions

DAEMON=/bin/run_celestia
EXEC=Controller
NAME=celestia
USER=celestia

SETUIDRUNNER_DAEMON=/bin/SetUIDRunner
SETUIDRUNNER_EXEC=/bin/SetUIDRunner
SETUIDRUNNER_NAME=SetUIDRunner
SETUIDRUNNER_ARGUMENTS="Proxy"

CAN_DAEMON=/bin/run_can_server
CAN_EXEC=/bin/can_server
CAN_NAME=can_server

SHUTDOWN_DAEMON=/usr/bin/edx_powerd
SHUTDOWN_EXEC=/usr/bin/edx_powerd
SHUTDOWN_NAME=edx_powerd

test -x $DAEMON || exit 0
test -x $SETUIDRUNNER_DAEMON || exit 0
test -x $CAN_DAEMON || exit 0
test -x $SHUTDOWN_DAEMON || exit 0

# check current IO scheduler.
IO_SCHEDULER=$(cat /sys/block/sda/queue/scheduler | sed -e "s/^.*\[\(.*\)\].*/\1/")

# If we are using "completely fair queing" scheduler, change to something else,
# as cfq is the one with the theoretical bad behaviour. If it is already not
# "cfq" either we've already changed it or somebody cleverer than us had a
# reason to change it, so leave it as is.
if [ "cfq" = $IO_SCHEDULER ] ; then
# change to noop -> deadline would also be a good choice, but it doesn't
# actually matter much when using CF devices (as long as it's not cfq).
    echo "noop" > /sys/block/sda/queue/scheduler
fi

if [ ! -f ~celestia/.backup_core_pattern ]; then
  if [ -f /proc/sys/kernel/core_pattern ]; then
    cp /proc/sys/kernel/core_pattern /home/celestia/.backup_core_pattern
  fi
fi
echo /home/celestia/.HEMISPHERE/core_%e_%t_%p_%u_%g_%s_%h > /proc/sys/kernel/core_pattern

result1=0
result2=0
result3=0
result4=0

case $1 in
	start)
        # Check the DMU firmware and update if required
        do_dmu_firmware_update_loop

		echo -n "Starting $SHUTDOWN_NAME: "
		start-stop-daemon --oknodo --start --background --exec $SHUTDOWN_EXEC --startas $SHUTDOWN_DAEMON
                result4=$?
		echo "$SHUTDOWN_NAME."

		echo -n "Starting $SETUIDRUNNER_NAME: "
		start-stop-daemon --oknodo --start --chuid $USER:$USER --background --exec $SETUIDRUNNER_EXEC --startas $SETUIDRUNNER_DAEMON -- $SETUIDRUNNER_ARGUMENTS
                result3=$?
		echo "$SETUIDRUNNER_NAME."

		echo -n "Starting $CAN_NAME: "
		start-stop-daemon --oknodo --start --chuid $USER:$USER --background --exec $CAN_EXEC --startas $CAN_DAEMON
                result2=$?
		echo "$CAN_NAME."
		sleep 10

		echo -n "Starting $NAME: "
		start-stop-daemon --start --chuid $USER:$USER --background --user $USER --name $EXEC --startas $DAEMON
                result1=$?
		echo "$NAME."
	;;

	stop)
		echo -n "Stopping $NAME: "
		start-stop-daemon --stop --user $USER --name $EXEC
                result1=$?
		echo "$NAME."
                sleep 2

		echo -n "Stopping $CAN_NAME: "
		start-stop-daemon --stop --exec $CAN_EXEC
                result2=$?
		echo "$CAN_NAME."

		echo -n "Stopping $SETUIDRUNNER_NAME: "
		start-stop-daemon --stop --exec $SETUIDRUNNER_EXEC
                result3=$?
		echo "$SETUIDRUNNER_NAME."

		echo -n "Stopping $SHUTDOWN_NAME: "
		start-stop-daemon --stop --exec $SHUTDOWN_EXEC
                result4=$?
		echo "$SHUTDOWN_NAME."
	;;

	force-reload)
		start-stop-daemon --stop --test --user $USER --name $EXEC \
		&& $0 restart \
		|| exit 0
	;;

	restart)
		$0 stop
		$0 start
	;;


	*)
		echo "Usage: /etc/init.d/$0 {start|stop|restart|force-reload}" >&2
		exit 1
	;;

esac

if [ $result1 -ne 0 ]; then
  exit $result1
fi

if [ $result2 -ne 0 ]; then
  exit $result2
fi

if [ $result3 -ne 0 ]; then
  exit $result3
fi

if [ $result4 -ne 0 ]; then
  exit $result4
fi

exit 0
