#!/bin/sh
#
# $NetBSD: distccd.sh,v 1.7 2005/02/18 07:50:49 xtraeme Exp $
#

# PROVIDE: distccd
# BEFORE:  DAEMON

if [ -f /etc/rc.subr ]; then
	. /etc/rc.subr
fi

name="distccd"
rcvar="${name}"
command="/usr/pkg/bin/${name}"
pidfile="/var/run/distccd/${name}.pid"
command_args="--daemon --pid-file ${pidfile}"
distccd_user="distcc"
distccd_group="distcc"
start_precmd="distcc_precmd"

distcc_precmd()
{
    if [ ! -d /var/run/distccd ]; then
	/bin/mkdir -p /var/run/distccd
	/bin/chmod 0700 /var/run/distccd
	/usr/sbin/chown distcc /var/run/distccd
    fi
}

if [ -f /etc/rc.subr -a -f /etc/rc.conf -a -f /etc/rc.d/DAEMON ]; then
	load_rc_config $name
	run_rc_command "$1"
else
	case ${1:-start} in
	start)
		distcc_precmd
		if [ -x ${command} ]; then
			echo "Starting ${name}."
			eval ${command} ${distccd_flags} ${command_args}
		fi
		;;
	stop)
		if [ -f ${pidfile} ]; then
			pid=`/bin/head -1 ${pidfile}`
			echo "Stopping ${name}."
			kill -TERM ${pid}
		else
			echo "${name} not running?"
		fi
		;;
	restart)
		( $0 stop )
		sleep 1
		$0 start
		;;
	status)
		if [ -f ${pidfile} ]; then
			pid=`/bin/head -1 ${pidfile}`
			echo "${name} is running as pid ${pid}."
		else
			echo "${name} is not running."
		fi
		;;
		esac
fi
