#!/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:


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
	echo "$DATALOGGER is not executable."
	return 1
    else
        # Otherwise...
	$DATALOGGER 
    fi
}

stop()
{
    local ISRUNNING=`ps | grep -v grep | grep $DATALOGGER`
    # If the application is running, kill it.
    if [[ -n $ISRUNNING ]]; then
	# Kill both the keepalive script and the binary.
	ps | grep -v grep | grep "$DATALOGGER" | awk '{print $1}' | xargs kill 
    else
	echo "$DATALOGGER is not running."
    fi
}

restart()
{
    local ISRUNNING=`ps | grep -v grep | grep $DATALOGGER`
    # If the application is running, kill it.
    if [[ -n $ISRUNNING ]]; then
	# Kill both the keepalive script and the binary.
	ps | grep -v grep | grep "$DATALOGGER" | awk '{print $1}' | xargs kill 
    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."
    else
        # Otherwise...
	$DATALOGGER
    fi
}


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