#!/bin/sh
#
# tyrion-      init file for starting Tyrion
#
# chkconfig:   - 20 80
# description: Starts and stops the Tyrion daemon that handles all \
#              service requests.

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

exec="/usr/sbin/tyrion"
config="/etc/tyrion/tyrion.conf"
logfile="/var/log/tyrion/stdout.log"
pidfile="/var/run/tyrion.pid"

[ -e /etc/sysconfig/tyrion ] && . /etc/sysconfig/tyrion

lockfile=/var/lock/subsys/tyrion

start() {
    [ -x $exec ] || exit 5
    echo -n $"Starting tyrion: "
    daemon --user ${TYRION_USER-root} --pidfile $pidfile \
      "$exec -c $config >> $logfile 2>&1 & echo \$! > $pidfile"
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

stop() {
    echo -n $"Stopping tyrion: "
    killproc -p $pidfile tyrion
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading tyrion: "
    killproc -p $pidfile tyrion -HUP
    retval=$?
    echo
    return $retval
}

rh_status() {
    status -p $pidfile tyrion
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}


case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
        restart
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload}"
        exit 2
esac
exit $?
