#! /bin/sh

usage () {
cat <<EOF

lkst_watch_daemon: 
    watch specified daemon and when the daemon is down then
    let LKST logging daemon to preserve the logs.

USAGE: 
  set following envionments:
	DAEMON_TOWATCH as the name of daemon to watch.
	INTERVAL as interval to watch [in seconds].
  then invoke:
       lkst_watch_daemon &

EOF
}

DAEMON_TOWATCH=${DAEMON_TOWATCH:="sendmail[:]"}
INTERVAL=${INTERVAL:="10"}

LKSTLOGD_PID=/var/run/lkstlogd.pid
EXISTS=

if [ ! -f "$LKSTLOGD_PID" ]
then
	echo $0: LKST logging daemon was not found.
	usage
	exit 1
fi

while sleep $INTERVAL
do
	SENDMAIL=`ps auxww | grep "$DAEMON_TOWATCH"`
	if [ -z "$SENDMAIL" ]
	then
		if [ "$EXISTS" ]
		then
			kill -USR2 `cat $LKSTLOGD_PID`
			break
		fi
		EXISTS=
	else
		EXISTS=1
	fi
done
