#!/bin/sh
#
# hookd:   hook events notifier
#
# chkconfig: 345 40 60
# # Source function library.
# . /etc/rc.d/init.d/functions

# so we can rearrange this easily
servicename=hookd
processname=/usr/bin/$servicename
pidfile=/var/run/$servicename.pid
lockfile=/var/lock/subsys/$servicename
STATUS=/usr/sbin/upgd_status

RETVAL=0


start() {
    # do no start if upgrade is running
    if [[ -x $STATUS ]]; then
        $STATUS >/dev/null 2>&1
        if [ $? -eq 1 ]; then
          echo "upgrade in progress : will not start hookd"
          return
        fi
    fi
    pidof $servicename > $pidfile
    RETVAL=$?
    if [ $RETVAL -ne 0 ]; then
        rm $pidfile
    fi

    [[ -f $pidfile ]] && [[ -e /proc/$(cat $pidfile) ]] && return

    $processname 
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        touch $lockfile
    fi
    return $RETVAL
}

stop() {
    ## we don't want to kill all the per-user $processname, we want
    ## to use the pid file *only*; because we use the fake nonexistent
    ## program name "$servicename" that should be safe-ish
    # killproc $servicename -TERM
    pidof $servicename > $pidfile
    kill -TERM `cat $pidfile`
    RETVAL=$?
    if [ $RETVAL -eq 0 ]; then
        rm -f $pidfile
        rm -f $lockfile
    fi
    return $RETVAL
}

# See how we were called.
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    condrestart)
        if [ -f $lockfile ]; then
            stop
            start
        fi
		;;
    *)
        echo $"Usage: $0 {start|stop|restart|condrestart}"
        ;;
esac

exit $RETVAL
