#!/bin/sh
### BEGIN INIT INFO
# Provides:          FA_DataLogger
# Required-Start:    $local_fs $remote_fs $FA_dbus_daemon $FA_coredatad
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Description:       This script launches DataLogger.
# Short-Description:	Runs DataLogger.
### END INIT INFO


# Author: Jon Polfer <jpolfer@forceamerica.com>
#         Based on other FA_ scripts by Andrew Dorney <adorney@forceamerica.com>
# Project: P1131 - 6100 Spreader Control
# Created: 08/28/2009
# Modified:


. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DATALOGGER=/6100/bin/DataLogger
SCRIPTNAME=FA_DataLogger

start()
{
    # If eventtd isn't executable, quit.
    if [[ ! -x "$DATALOGGER" ]]; then
	log_action_end_msg 1
	echo "$DATALOGGER is not executable."
	return 1
    else
        # Otherwise...
	$DATALOGGER 
        log_action_end_msg 0
    fi
}

stop()
{
    local ISRUNNING=`ps ax | grep -v grep | grep $DATALOGGER`
    # If the application is running, kill it 
    if [[ -n $ISRUNNING ]]; then
        killall $DATALOGGER 
        log_action_end_msg 0
    else
        log_action_end_msg 1
    fi
}

restart()
{
    local ISRUNNING=`ps ax | grep -v grep | grep $DATALOGGER`
    # If the application is running, kill it 
    if [[ -n $ISRUNNING ]]; then
        killall $DATALOGGER 
    else
	echo "$DATALOGGER is not running"
    fi

    # Then, restart it.
    # If the application isn't executable, quit.
    if [[ ! -x "$DATALOGGER" ]]; then
	echo "$DATALOGGER is not executable."
	log_action_end_msg 1
    else
        # Otherwise...
	$DATALOGGER
        log_action_end_msg 0
    fi
}


case "$1" in
    start)
	log_action_msg "Starting DataLogger"
	start
	;;
    stop)
	log_action_msg "Stopping DataLogger"
	stop
	;;
    restart)
	log_action_msg "Restarting DataLogger"
	restart
	;;
    *)
	# echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart}" >&2
	exit 3
	;;
esac
