#!/bin/sh
# /etc/init.d/jetstream
#
### BEGIN INIT INFO
# Provides:          jetstream
# Required-Start:    $local_fs
# Required-Stop:     $local_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: jetstream
# Description:       jetstream daemon
### END INIT INFO

set -x
set -e

DAEMON=/usr/bin/raven/fishspear/jetstream
WRAPPER=/etc/init.d/jetstream_wrapper
DAEMONOPTS=
NAME=jetstream
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin
LIBDIR=/usr/bin/raven/fishspear/
ROSVERFILE=${LIBDIR}softwareversion.txt

test -x $DAEMON || exit 2

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$LIBDIR:$LIBDIR/plugins/be/:$LIBDIR/plugins/:$LIBDIR/plugins/ui/jobConfirmation/:$LIBDIR/plugins/ui/tractors_carts/:$LIBDIR/plugins/ui/:$LIBDIR/plugins/dispatchers/:$LIBDIR/plugins/can/:$LIBDIR/plugins/can/isobus/:$LIBDIR/plugins/serial/
 
export DISPLAY=:0.0
export LANG=en_US.UTF-8

if [ -f ${ROSVERFILE} ]; then
	export ROSVERSION=$(cat ${ROSVERFILE})
fi

# Remove .lock files left over from power loss
find /usr/bin/raven/fishspear/ -name "*ini.lock" -exec rm {} \;

PID=""

IsRunning() {

	kill -s 0 $PID &> /dev/null
	if [ "$?" = "0" ]; then
			return 1  #process is running
	else
			return 0  #process not running
	fi

}

case "$1" in
  start)
    echo -n "Starting app: $NAME  "
    [ -d /var/run/jetstream ] || mkdir -p /var/run/jetstream
	
	cd /usr/bin/raven
    start-stop-daemon --start --background -m --pidfile /var/run/jetstream/jetstream.pid --exec $WRAPPER -- $DAEMONOPTS
	
	sleep 5 #kinda hokey, but we need to make sure the start-stop-daemon has time to generate a pid file before we try to read it, 1 second should be more than enough. 5 wont hurt anything
	
	PID=`cat /var/run/jetstream/jetstream.pid`
	
	until IsRunning
	do
		sleep 3
	done
	
	RETURN_VALUE=`cat /tmp/jetstream.return.code`
	exit $RETURN_VALUE
	
    ;;
  stop)
    echo -n "Stopping app: $NAME  "
    start-stop-daemon --stop --pidfile /var/run/jetstream/jetstream.pid --oknodo --exec $DAEMON -- $DAEMONOPTS
    rm -f /var/run/jetstream/jetstream.pid
    echo "."
    ;;
  restart)
    echo -n "Restarting app: $NAME  "
	
	set +e #enable error checking incase process wasn't running
	
    start-stop-daemon --stop --pidfile /var/run/jetstream/jetstream.pid -q --exec $DAEMON

	if [ "$?" = "1" ]; then
		sleep 1 #if process wasn't running give it one second to see if it comes up (incase this script gets called twice really fast) and try again
		start-stop-daemon --stop --pidfile /var/run/jetstream/jetstream.pid --oknodo --exec $DAEMON
	fi
	set -e
    rm -f /var/run/jetstream/jetstream.pid
    start-stop-daemon --start --background -m --pidfile /var/run/jetstream/jetstream.pid --exec $DAEMON -- $DAEMONOPTS
	
	PID=`cat /var/run/jetstream/jetstream.pid`
	
	until IsRunning
	do
		sleep 3
	done
 
 	RETURN_VALUE=`cat /tmp/jetstream.return.code`
	exit $RETURN_VALUE
    ;; 
  debug)
    echo "Debug Start: $NAME "
    $DAEMON
    echo "."
    ;;
  debug-log)
    echo "$NAME Run /w Logging to /home/root/jetstream.run.log"
	mkdir -p /home/root/
    $DAEMON &> /home/root/jetstream.run.log
    echo "."
    ;;
   strace-log)
    echo "$NAME strace /w Logging to /home/root/jetstream.strace.log"
	mkdir -p /home/root/
    strace $DAEMON &> /home/root/jetstream.strace.log
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|debug|debug-log|strace-log}"
    exit 1
    ;;
esac

exit 0
