#!/bin/sh

###########################################################################
#
# System V style init script.
# Relies on runit utilities to do the real work.
# It is assumed that init scripts will be linked to this script.
#
###########################################################################

# Determine the service name and its service directory from $0

SERVICE=$(/bin/basename $0 | sed -e 's/^[SK][0-9][0-9]*//')
SERVICE_DIR=/service/$SERVICE

###########################################################################

# Source in the RedHat initscripts functions

. /etc/rc.d/init.d/functions

# The maximum amount of time to wait for a process to shut down, in seconds.
WAITMAX=60

start()
{
    /bin/echo -n "Starting $SERVICE:"
    dirs=$1
    if [ -d $1/log ]; then
        dirs="$1/log $1"
    fi
    /usr/bin/runsvctrl u $dirs
    if [ $? -ne 0 ]; then
        failure "Starting $SERVICE"
    else
        success "Starting $SERVICE"
    fi
    /bin/echo
}

status()
{
    /usr/bin/runsvstat $1
}

stop()
{
    /bin/echo -n "Stopping $SERVICE:"
    /usr/bin/svwaitdown -t $WAITMAX $1
    if [ $? -ne 0 ]; then
        failure "Stopping $SERVICE"
    else
        success "Stopping $SERVICE"
    fi
    /bin/echo
}

# This function not only shuts the service down, but removes the /service
# symlink and shuts down the logger. This should only be used during an
# uninstall of the service in question.
svdisable()
{
    /bin/echo -n "Disabling $SERVICE:"
    stop $1
    cd $1 && rm -f $1
    dirs=.
    if [ -e log ]; then
        dirs="$dirs ./log"
    fi
    /usr/bin/runsvctrl d $dirs
    /usr/bin/runsvctrl x $dirs
    if [ $? -ne 0 ]; then
        failure "Disabling $SERVICE"
    else
        success "Disabling $SERVICE"
    fi
    /bin/echo
}

case $1 in

    restart)
	action "Restarting $SERVICE" /usr/bin/runsvctrl t $SERVICE_DIR
	/usr/bin/runsvctrl u $SERVICE_DIR
            ;;

    sigalrm)
	action "Sending ALRM signal to $SERVICE" /usr/bin/runsvctrl a $SERVICE_DIR
            ;;

    sigcont)
	action "Sending CONT signal to $SERVICE" /usr/bin/runsvctrl c $SERVICE_DIR
            ;;

    sighup)
	action "Sending HUP signal to $SERVICE" /usr/bin/runsvctrl h $SERVICE_DIR
            ;;

    sigusr1)
	action "Sending USR1 signal to $SERVICE" /usr/bin/runsvctrl 1 $SERVICE_DIR
            ;;

    sigusr2)
	action "Sending USR2 signal to $SERVICE" /usr/bin/runsvctrl 2 $SERVICE_DIR
            ;;

    sigint)
	action "Sending INT signal to $SERVICE" /usr/bin/runsvctrl i $SERVICE_DIR
            ;;

    sigkill)
	action "Sending KILL signal to $SERVICE" /usr/bin/runsvctrl k $SERVICE_DIR
            ;;

    sigstop)
	action "Sending STOP signal to $SERVICE" /usr/bin/runsvctrl p $SERVICE_DIR
            ;;

    sigterm|condrestart)
	action "Sending TERM signal to $SERVICE" /usr/bin/runsvctrl t $SERVICE_DIR
            ;;

    status)
	/usr/bin/runsvstat $SERVICE_DIR
            ;;

    start)
        start $SERVICE_DIR
            ;;

    stop)
        stop $SERVICE_DIR
            ;;

    svdisable)
        svdisable $SERVICE_DIR
            ;;

    *)
        echo "usage: $0 {start|stop|restart|status|sigalrm|sigcont|sighup|sigint|sigkill|sigstop|sigterm|condrestart|sigusr1|sigusr2|svdisable}"
        ;;

esac
