#!/bin/sh
### BEGIN INIT INFO
# Provides:          FA_persistd
# 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 persistd.
# Short-Description:	Runs persistd.
### END INIT INFO


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


. /lib/lsb/init-functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
PERSISTD=/6100/bin/persistd
persistd_pid_file=/dev/shm/persistd.pid
SCRIPTNAME=FA_persistd

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

stop()
{
    # If PID file exists, kill the program and remove its pid file.
    if [[ -f ${persistd_pid_file} ]]; then
        kill `cat $persistd_pid_file` 2>&1 >/dev/null && rm $persistd_pid_file
        log_action_end_msg 0
    else
        log_action_end_msg 1
    fi
}

restart()
{
    # If PID file exists, kill the program and remove its pid file.
    if [[ -f ${persistd_pid_file} ]]; then
        kill `cat $persistd_pid_file` 2>&1 >/dev/null && rm $persistd_pid_file
    else
	echo "$PERSISTD is not running"
    fi

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


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