#!/bin/sh
#
# svscan	svscan (scan and run services)
#
# chkconfig:	345 14 89
#
# description:	svscan (scan and run services) is a server part of the daemontools suite.
#
# id:		$Id$
#

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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/svscan ] && . /etc/sysconfig/svscan

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down svscan
		exit 1
	fi
else
	exit 0
fi

RETVAL=0
# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/svscan ]; then
		msg_starting svscan;
		busy
		# we can't get status report from background job
		# but we can only do simple file check
		if [ -w $SVSCAN_DIR ]; then
			setsid svscan $SVSCAN_DIR </dev/null >> /var/log/svscan 2>&1 &
			ok
		else
			fail
			echo $(nls "Missing service directory")'!'
			nls "Create %s first." "$SVSCAN_DIR"
			RETVAL=1
		fi
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/svscan
	else
		msg_already_running svscan
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/svscan ]; then
		msg_stopping svscan
		killproc svscan
		svc -d -x $SVSCAN_DIR/* \
		    $(find $SVSCAN_DIR/* -maxdepth 0 -follow -perm -1000 2> /dev/null |sed s-\$-/log-)
		rm -f /var/run/svscan.pid /var/lock/subsys/svscan >/dev/null 2>&1
	else
		msg_not_running svscan
	fi
	;;
  restart|force-reload)
	$0 stop
	$0 start
	exit $?
	;;
  status)
	status svscan
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|status}"
	exit 3
esac

exit $RETVAL
