#!/bin/sh
#
# iceccd:   Distributed compiler daemon
#
# chkconfig: - 98 02
# description:  This is a daemon for speeding up builds by \
#		distributing compile jobs to several computers on a network.
#

### BEGIN INIT INFO
# Provides: iceccd
# Required-Start: $local_fs $remote_fs $network $named
# Required-Stop: $local_fs $remote_fs $network $named
# Short-Description: Start/stop Icecream distributed compiler
# Description: Start / stop the Icecream distributed compiler daemon
### END INIT INFO

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

# Source config
if [ -f /etc/sysconfig/icecream ] ; then
	. /etc/sysconfig/icecream
fi

exec=/usr/sbin/iceccd

start() {
	[ -x $exec ] || exit 5
	if [ ! -f /var/lock/subsys/iceccd ]; then
	msg_starting iceccd
	params=""
	if [ -n "$ICECREAM_NETNAME" ] ; then
		params="$params -n $ICECREAM_NETNAME"
	fi
	if [ -n "$ICECREAM_LOG_FILE" ] ; then
		params="$params -l $ICECREAM_LOG_FILE"
	fi
	if [ -n "$ICECREAM_NICE_LEVEL" ] ; then
		params="$params --nice $ICECREAM_NICE_LEVEL"
	fi
	if [ -n "$ICECREAM_SCHEDULER_HOST" ] ; then
		params="$params -s $ICECREAM_SCHEDULER_HOST"
	fi
	if [ "$ICECREAM_ALLOW_REMOTE" = "no" 2> /dev/null ] ; then
		params="$params --no-remote"
	fi
	if [ -n "$ICECREAM_MAX_JOBS" ] ; then
		if [ "$ICECREAM_MAX_JOBS" -eq 0 2> /dev/null ] ; then
			params="$params -m 1"
			params="$params --no-remote"
		else
			params="$params -m $ICECREAM_MAX_JOBS"
		fi
	fi
	verbosity="$ICECREAM_VERBOSITY"
	params="$params -b \"$ICECREAM_BASEDIR\" $verbosity"
	daemon $exec -d -u icecream $params
	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/iceccd
	else
		msg_already_running iceccd
	fi

	return $RETVAL
}

stop() {
	if [ -f /var/lock/subsys/iceccd ]; then
		msg_stopping iceccd
		killproc iceccd 
		rm -f /var/lock/subsys/iceccd
	else
		msg_not_running iceccd
	fi
}

condrestart() {
	if [ -f /var/lock/subsys/iceccd ]; then
		stop
		start
	else
		msg_not_running icecream
		RETVAL=$1
	fi
}

RETVAL=0
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
	condrestart 
	;;
  try-restart)
	condrestart 0
	;;
  force-reload)
	condrestart 7
	;;
  status)
	status icecream
	;;
  *)
	msg_usage "$0 {start|stop|restart|try-restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
