#!/bin/sh
# 
# chkconfig: 345 99 01
# description: Netsaint network monitor
#
# File : netsaint
#
# Author : Jorge Sanchez Aymar (jsanchez@lanchile.cl)
# 
# Changelog :
#
# 1999-07-09 Karl DeBisschop <kdebisschop@infoplease.com>
#  - setup for autoconf
#  - add reload function
# 1999-08-06 Ethan Galstad <netsaint@linuxbox.com>
#  - Added configuration info for use with RedHat's chkconfig tool
#    per Fran Boon's suggestion
# 1999-08-13 Jim Popovitch <jimpop@rocketship.com>
#  - added variable for netsaint/var directory
#  - cd into netsaint/var directory before creating tmp files on startup
# 1999-08-16 Ethan Galstad <netsaint@linuxbox.com>
#  - Added test for rc.d directory as suggested by Karl DeBisschop
# 2000-07-23 Karl DeBisschop <kdebisschop@users.sourceforge.net>
#  - Clean out redhat macros and other dependencies
#
# Description: Starts and stops the Netsaint monitor
#              used to provide network services status.
#
  
status ()
{

	if test ! -f $NetsaintRun; then
		echo "No lock file found in $NetsaintRun"
		return 1
	fi

	NetsaintPID=`head -n 1 $NetsaintRun`
	if test -x $NetsaintCGI/daemonchk.cgi; then
		if $NetsaintCGI/daemonchk.cgi -l $NetsaintRun; then
		        return 0
		else
			return 1
		fi
	else
		if ps -p $NetsaintPID; then
		        return 0
		else
			return 1
		fi
	fi

	return 1
}


killproc ()
{

	if test ! -f $NetsaintRun; then
		echo "No lock file found in $NetsaintRun"
		return 1
	fi

	NetsaintPID=`head -n 1 $NetsaintRun`
	kill $2 $NetsaintPID
}


# Source function library
if [ -f /usr/local/etc/netsaint/functions ]; then
. /usr/local/etc/netsaint/functions
fi

prefix=/usr/local
exec_prefix=${prefix}
NetsaintBin=${exec_prefix}/bin/netsaint
NetsaintCfg=/usr/local/etc/netsaint/netsaint.cfg
NetsaintLog=/usr/local/var/netsaint/status.log
NetsaintTmp=/usr/local/var/netsaint/netsaint.tmp
NetsaintSav=/usr/local/var/netsaint/status.sav
NetsaintCmd=/usr/local/var/netsaint/rw/netsaint.cmd
NetsaintVar=/usr/local/var/netsaint
NetsaintRun=/usr/local/var/netsaint/netsaint.lock
NetsaintLckDir=/var/lock/subsys
NetsaintLckFile=netsaint
NetsaintCGI=/usr/local/share/netsaint/sbin
Netsaint=netsaint
          

# Check that netsaint exists.
test -f $NetsaintBin || exit 0

# Check that netsaint.cfg exists.
test -f $NetsaintCfg || exit 0
          
# See how we were called.
case "$1" in

	start)
		echo "Starting network monitor: netsaint"
		su -m $Netsaint -c "touch $NetsaintVar/netsaint.log $NetsaintSav"
		rm -f $NetsaintCmd
		$NetsaintBin -d $NetsaintCfg
		if [ -d $NetsaintLckDir ]; then touch $NetsaintLckDir/$NetsaintLckFile; fi
		sleep 1
		status netsaint
		;;

	stop)
		echo "Stopping network monitor: netsaint"
		killproc netsaint
		rm -f $NetsaintLog $NetsaintTmp $NetsaintRun $NetsaintLckDir/$NetsaintLckFile $NetsaintCmd
		;;

	status)
		status netsaint
		;;

	restart)
		printf "Running configuration check..."
		$NetsaintBin -v $NetsaintCfg > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo "done"
			$0 stop
			$0 start
		else
			$NetsaintBin -v $NetsaintCfg
			echo "failed - aborting restart."
			exit 1
		fi
		;;

	reload|force-reload)
		printf "Running configuration check..."
		$NetsaintBin -v $NetsaintCfg > /dev/null 2>&1;
		if [ $? -eq 0 ]; then
			echo "done"
			if test ! -f $NetsaintRun; then
				$0 start
			else
				NetsaintPID=`head -n 1 $NetsaintRun`
				if status netsaint > /dev/null; then
					$0 stop
					$0 start
				else
					printf "Reloading netsaint configuration..."
					killproc netsaint -HUP
					echo "done"
				fi
			fi
		else
			$NetsaintBin -v $NetsaintCfg
			echo "failed - aborting reload."
			exit 1
		fi
		;;

	*)
		echo "Usage: netsaint {start|stop|restart|reload|force-reload|status}"
		exit 1
		;;

esac
  
# End of this script
