#!/bin/sh
#
# iceccd Debian init.d script contributed by Jason Thomas.  (Debian #161136)
#
# skeleton      example file to build /etc/init.d/ scripts.
#               This file should be used to construct scripts for /etc/init.d.
#
#               Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#               Modified for Debian GNU/Linux
#               by Ian Murdock <imurdock@gnu.ai.mit.edu>
#               by Carsten Wolff <carsten@wolffcarsten.de>.
#
# Version:      @(#)skeleton  1.9.1  08-Apr-2002  miquels@cistron.nl
#

DAEMONNAME=iceccd
DAEMON=/usr/sbin/$DAEMONNAME
SCHEDULERNAME=icecc-scheduler
SCHEDULER=/usr/sbin/$SCHEDULERNAME
DEFAULT=/etc/default/icecc
DESC="Distributed Compiler Daemon"

# Reads config file
[ -r $DEFAULT ] && . $DEFAULT

test -x $DAEMON || exit 0

set -e

netname=
if test -n "$ICECC_NETNAME"; then
    netname="-n $ICECC_NETNAME"
fi



start_daemon() {
    logfile=""
    if test -n "$ICECC_LOG_FILE"; then
        logfile="-l $ICECC_LOG_FILE"
    fi
    nice= 
    if test -n "$ICECC_NICE_LEVEL"; then
       nice="--nice $ICECC_NICE_LEVEL"
    fi
    scheduler=
    if test -n "$ICECC_SCHEDULER_HOST"; then
        scheduler="-s $ICECC_SCHEDULER_HOST"
    fi
    basedir=
    if test -n "$ICECC_BASEDIR"; then
        basedir="-b $ICECC_BASEDIR"
    fi
    maxjobs=
    if test -n "$ICECC_MAX_JOBS"; then
        maxjobs="-m $ICECC_MAX_JOBS"
    fi
    start-stop-daemon --start --quiet --exec $DAEMON -- \
    -d "$logfile" $nice $scheduler $netname -u icecc $basedir $maxjobs
}



stop_daemon() {
    start-stop-daemon --stop --quiet --signal TERM --oknodo --exec $DAEMON
}



start_scheduler() {
    if test -z "$ICECC_SCHEDULER_LOG_FILE"; then
        ICECC_SCHEDULER_LOG_FILE="/var/log/icecc_scheduler"
    fi
    logfile="-l $ICECC_SCHEDULER_LOG_FILE"
    : > $ICECC_SCHEDULER_LOG_FILE
    chown icecc $ICECC_SCHEDULER_LOG_FILE
    start-stop-daemon --start --quiet --chuid icecc \
    --exec $SCHEDULER -- -d $logfile $netname
}



stop_scheduler() {
    start-stop-daemon --stop --quiet --signal TERM --oknodo --exec $SCHEDULER
}



case "$1" in
    start)
        if [ "$START_ICECC" == "true" ]; then
            echo -n "Starting $DESC: $DAEMONNAME"
            start_daemon
            echo "."
        else
            echo "START_ICECC is set to false in $DEFAULT"
            echo "$DAEMON not starting"
        fi

        if [ "$START_ICECC_SCHEDULER" == "true" ]; then
            echo -n "Starting $DESC: $SCHEDULERNAME"
            start_scheduler
            echo "."
        else
            echo "START_ICECC_SCHEDULER is set to false in $DEFAULT"
            echo "$SCHEDULER not starting"
        fi
        ;;
  stop)
        echo -n "Stopping $DESC: $DAEMONNAME"
        stop_daemon
        echo "."
        echo -n "Stopping $DESC: $SCHEDULERNAME"
        stop_scheduler
        echo "."
        ;;
  restart|force-reload)
        #
        #       If the "reload" option is implemented, move the "force-reload"
        #       option to the "reload" entry above. If not, "force-reload" is
        #       just the same as "restart".
        #
        echo -n "Restarting $DESC: $DAEMONNAME"
        stop_daemon
        sleep 1
        if [ "$START_ICECC" == "true" ]; then
            start_daemon
        fi
        echo "."

        echo -n "Restarting $DESC: $SCHEDULERNAME"
        stop_scheduler
        sleep 1
        if [ "$START_ICECC_SCHEDULER" == "true" ]; then
            start_scheduler
        fi
        echo "."
        ;;
  *)
        N=/etc/init.d/icecc
        echo "Usage: $N {start|stop|restart|force-reload}" >&2
        exit 1
        ;;
esac

exit 0


