#!/bin/bash
### BEGIN INIT INFO
# Provides:          svnpubsub
# Required-Start:    $remote_fs
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: SvnPubSub
# Description:       start SvnPubSub daemon
#### END INIT INFO

. /lib/init/vars.sh
. /lib/lsb/init-functions

svnpubsub_user=${svnpubsub_user-"daemon"}
svnpubsub_group=${svnpubsub_group-"daemon"}
svnpubsub_reactor=${svnpubsub_reactor-"poll"}
svnpubsub_pidfile=${svnpubsub_pidfile-"/var/run/svnpubsub.pid"}
pidfile="${svnpubsub_pidfile}"

TWSITD_CMD="/usr/bin/twistd -y /opt/svnpubsub/svnpubsub.tac \
            --logfile=/var/log/svnpubsub/svnpubsub.log \
            --pidfile=${pidfile} \
            --uid=${svnpubsub_user} --gid=${svnpubsub_user} \
            -r${svnpubsub_reactor}"

RETVAL=0
 
start() {
    echo "Starting SvnPubSub Server: "
    $TWSITD_CMD
    RETVAL=$?
    [ $RETVAL -eq 0 ] && echo "ok" || echo "failed"
    return $RETVAL
}
 
stop() {
    echo "Stopping SvnPubSub Server: "
    THE_PID=`cat ${pidfile}`
    kill $THE_PID
    RETVAL=$?
    [ $RETVAL -eq 0 ] && echo "ok" || echo "failed"
    return $RETVAL
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        start
        ;;
    *)
        echo "Usage: $0 {start|stop|restart}"
        exit 1
esac

exit $RETVAL
