#! /bin/sh
# tox-bootstrapd	Starts the Tox DHT bootstrapping server daemon
#
# chkconfig:		2345 90 49
# description:		Starts the Tox DHT bootstrapping server daemon
# processname:		tox-bootstrapd
# config:		/etc/tox-bootstrapd.conf
# pidfile:		/var/run/tox-bootstrapd.pid
#
### BEGIN INIT INFO
# Provides:		tox-bootstrapd
# Required-Start:	$remote_fs $syslog $network
# Required-Stop:	$remote_fs $syslog $network
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Starts the Tox DHT bootstrapping server daemon
# Description:		Starts the Tox DHT bootstrapping server daemon
### END INIT INFO

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

NAME=tox-bootstrapd
DAEMON=/usr/bin/$NAME
CFGFILE=/etc/$NAME.conf
DAEMON_ARGS="--config $CFGFILE"
PIDDIR=/var/run/$NAME
PIDFILE=$PIDDIR/$NAME.pid
USER=tox-bootstrapd
GROUP=tox-bootstrapd

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

exec="$DAEMON"
prog=${exec##*/}
[ -e /etc/sysconfig/$prog ] && . /etc/sysconfig/$prog
lockfile=/var/lock/subsys/$prog

start() {
    echo -n $"Starting $prog: "
    [ ! -d $PIDDIR ] && install -o $USER -g $GROUP -d $PIDDIR
    daemon --user $USER --pidfile $PIDFILE $exec $DAEMON_ARGS
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}

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

restart() {
    stop
    start
}

case "$1" in
    start|stop|restart)
        $1
        ;;
    force-reload)
        restart
        ;;
    status)
        status $prog
        ;;
    try-restart|condrestart)
        if status $prog >/dev/null ; then
            restart
        fi
	;;
    reload)
        # If config can be reloaded without restarting, implement it here,
        # remove the "exit", and add "reload" to the usage message below.
        # For example:
        # status $prog >/dev/null || exit 7
        # killproc $prog -HUP
        action $"Service ${0##*/} does not support the reload action: " /bin/false
        exit 3
        ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
        exit 2
esac

exit 0
