#!/bin/sh
### BEGIN INIT INFO
# Provides:          FA_dbus_daemon
# Required-Start:    $local_fs $remote_fs
# Required-Stop:     $local_fs $remote_fs
# Default-Start:     S 2 3 4 5
# Default-Stop:      0 1 6
# Description:       This script launches the Force America DBUS Daemon.
# Short-Description:	Runs FA's dbus daemon.
### 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
DBUS_DAEMON=/usr/bin/dbus-daemon
CONFIGFILE=/6100/etc/coredata-dbus.conf
SCRIPTNAME=FA_dbus_daemon

start()
{
    # If the application isn't executable, quit.
    if [[ ! -x "$DBUS_DAEMON" ]]; then
	echo "$DBUS_DAEMON is not executable."
	return 1
    else
        # Otherwise...
        nice -n -20 $DBUS_DAEMON --config-file=${CONFIGFILE} 
    fi
}

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

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

    if [[ ! -x "$DBUS_DAEMON" ]]; then
	echo "$DBUS_DAEMON is not executable."

    else
        # Otherwise...
        $DBUS_DAEMON --config-file=${CONFIGFILE}
    fi
}

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