#!/usr/bin/pagsh -e
### BEGIN INIT INFO
# Provides:          invirt-svn-server
# Required-Start:    $local_fs $remote_fs $network $syslog openafs-client
# Required-Stop:     $local_fs $remote_fs $network $syslog openafs-client
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/stop subversion server
### END INIT INFO
#
# Apache kstart wrapper by anders.
#

SCRIPT=/etc/init.d/apache2.invirt-orig
KSTART_PIDFILE=/var/run/k5start-invirt-svn.pid
KSTART_KEYTAB=/etc/invirt/keytab

export AFSCELL=sipb.mit.edu

kstart_start () {
    TMP_PIDFILE=$(mktemp /tmp/k5start-invirt-svn-server.pid-XXXXXX)
    chown invirt-svn "$TMP_PIDFILE"
    export KRB5CCNAME=$(mktemp /tmp/krb5cc_33.kstart-XXXXXX)
    chown invirt-svn $KRB5CCNAME
    export KRB5_KTNAME=/etc/invirt/svn.keytab
    /sbin/start-stop-daemon --start --pidfile "$TMP_PIDFILE" -c invirt-svn \
	--exec /usr/bin/k5start -- -b -p "$TMP_PIDFILE" \
	-U -f "$KSTART_KEYTAB" -K10 -S afs -I "$AFSCELL" -t -b \
	sudo -u invirt-svn svnserve -d --foreground -r "$(invirt-getconf svn.repopath)"
    cat "$TMP_PIDFILE" >|"$KSTART_PIDFILE"
    rm -f "$TMP_PIDFILE"
}
kstart_stop () {
    if [ -e $KSTART_PIDFILE ]; then
	/sbin/start-stop-daemon --stop --pidfile $KSTART_PIDFILE
	rm -f $KSTART_PIDFILE
    else
	echo -n " ... no kstart pidfile"
    fi
}

case "$1" in
  start)
    echo -n "Starting kstart for Subversion: k5start"
    kstart_start
    echo "."
    ;;
  stop)
    echo -n "Stopping kstart for Subversion: k5start"
    kstart_stop
    echo "."
    ;;
  restart|force-reload)
    "$0" stop
    sleep 1
    "$0" start
    ;;
  *)
    exec "$SCRIPT" "$@"
    ;;
esac
