#! /bin/sh
#
### BEGIN INIT INFO
# Provides:          buildd
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:
# Should-Start:      schroot
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Debian package autobuilder daemon
# Description:       Control the buildd daemon.
### END INIT INFO
#
# Copyright © 2006-2009  Roger Leigh <rleigh@debian.org>
# Copyright © 2007       Federico Di Gregorio <fog@debian.org>
#
# sbuild is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# sbuild is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see
# <http://www.gnu.org/licenses/>.

DAEMON=/usr/bin/buildd
PIDFILE=/var/lib/buildd/build/buildd.pid
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=buildd
DESC="Debian package autobuilder"

. /lib/lsb/init-functions

test -x $BUILDD || exit 0
test -x $BUILDD_WATCHER || exit 0

# Include buildd defaults if available
if [ -f "/etc/default/$NAME" ] ; then
    . "/etc/default/$NAME"
fi

set -e

start () {
    log_begin_msg "Starting $DESC: $NAME"

    if [ -e /home/buildd/NO-DAEMON-PLEASE ]; then
        log_failure_msg "NO-DAEMON-PLEASE exists, not starting"
    else
        start-stop-daemon --start --quiet --oknodo --pidfile "$PIDFILE" --chuid buildd:buildd --exec $DAEMON

        log_end_msg $?
    fi
}

stop () {
    log_begin_msg "Stopping $DESC: $NAME"

    start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE --name $NAME

    log_end_msg $?
}

stop () {
    log_begin_msg "Stopping $DESC: $NAME"

    start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE --name $NAME

    log_end_msg $?
}

reload () {
    log_begin_msg "Reloading $DESC: $NAME"
    start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME --signal USR1 && success=1
    log_end_msg $?
}

restart () {
    log_begin_msg "Restarting $DESC: $NAME"
    if start-stop-daemon --stop --quiet --retry 5 --oknodo --pidfile $PIDFILE --name $NAME; then
       if [ -e /home/buildd/NO-DAEMON-PLEASE ]; then
           log_failure_msg "NO-DAEMON-PLEASE exists, not starting"
       else
           start-stop-daemon --start --quiet --pidfile "$PIDFILE"  --chuid buildd:buildd --exec $DAEMON
       fi
    fi
    log_end_msg $?
}

case "$1" in
    start)
	start
	;;
    restart)
	restart
        ;;
    force-reload)
	reload
	;;
    stop)
	stop
	;;
    status)
	echo -n "Status of $DESC: "
	if [ ! -r "$PIDFILE" ]; then
	    echo "$NAME is not running."
	    exit 3
	fi
	if read pid < "$PIDFILE" && ps -p "$pid" > /dev/null 2>&1; then
	    echo "$NAME is running."
	    exit 0
	else
	    echo "$NAME is not running but $PIDFILE exists."
	    exit 1
	fi
	;;
    *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
