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


# Author: Andrew Dorney <adorney@forceamerica.com>
# Project: P1131 - 6100 Spreader Control
# Created: 05/11/2009
# Modified:


PATH=/sbin:/bin:/usr/sbin:/usr/bin
COREDATAD=/6100/bin/coredatad
SCRIPTNAME=FA_coredatad

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

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

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

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

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