#!/bin/sh
#
# generic_wrapper - wrapper script for daemons
#
# This goes in /usr/local/sbin, renamed or linked as <whatever>_wrapper.
# It backgrounds itself, and then runs the real <whatever> daemon in a loop.
# If the daemon exits then the script restarts it automatically.
#
# The -X flag tells the daemon to *not* put itself into the background.
# The other flags and arguments are copied from this script's command
# line to the daemon's.

daemon=`echo "$0" | sed -e 's/_wrapper//'`
dae=`echo "$daemon" | sed -e 's,.*/,,'`
(
    while true ; do
	"$daemon" -X "$@"
	if [ -f /var/run/nologin ] ; then
	    exit
	fi
	sleep 10
	(
	    egrep " $dae[:\[]" /var/log/messages |
	      tail -20
	    egrep " $dae[:\[]" /var/log/maillog |
	      tail -20
	) |
	  mail -s "$dae on `hostname` restarted" root
    done
) &
