#!/bin/sh
#
# pscheduler-ticker  Start/Stop the pScheduler ticker
#
# chkconfig: 2345 90 10
# description: pScheduler ticker daemon


### BEGIN INIT INFO
# Provides: pscheduler-ticker
# Default-Start:  2345
# Default-Stop: 10
# Short-Description: Run pscheduler ticker
# Description: pScheduler ticker daemon
### END INIT INFO



prog=ticker
exec=/usr/libexec/pscheduler/daemons/$prog

config=/etc/pscheduler/database/database-dsn
proguser=pscheduler

pidfile=/var/run/pscheduler-$prog.pid
lockfile=/var/lock/subsys/$proc

# TODO: Should probably rotate this, but should almost always be empty.
logfile=/var/log/pscheduler/$prog.out

# Source function library.
. /etc/rc.d/init.d/functions

retval=0

[ $UID -eq 0 ]

start() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    [ -x $exec ] || exit 5
    [ -f $config ] || exit 6

    optfile="/etc/pscheduler/daemons/ticker.conf"
    if [ -r "${optfile}" ]
    then
	options=$(sed -e 's/#.*$//' -e '/^$/d' "${optfile}" \
	    | tr "\n" " ")

    fi

    echo -n $"Starting pScheduler $prog: "
    touch "$pidfile"
    chown "$proguser.$proguser" "$pidfile"
    if [ ! -s "$logfile" ] ; then
        echo "(This file should be empty unless there's a catastrophic failure.)" >> "$logfile"
    fi
    su $proguser \
       -c "$exec --daemon --pid-file '$pidfile' --dsn '@$config' $options" \
	>> "$logfile" 2>&1 &
    retval=$?
    success
    echo
}


stop() {
    if [ $UID -ne 0 ] ; then
        echo "User has insufficient privilege."
        exit 4
    fi
    echo -n $"Stopping pScheduler $prog: "
        if [ -s "$pidfile" ]; then
	    kill $(cat $pidfile) || failure "Stopping pScheduler $prog"
	    rm -f $pidfile
	fi
    retval=$?
    success
    echo
}

restart()
{
    stop
    start
}


case "$1" in
    start|stop|restart)
        $1
        ;;

    status)
        status -p ${pidfile} $exec
        ;;

    *)
        echo $"Usage: $0 {start|stop|restart|status}"
        exit 2
esac
exit $?
