#!/bin/csh -f -b
#**********************************************************************
#         Copyright IBM Corporation 1988,1989 - All Rights Reserved
#         For full copyright information see: 'andrew/config/COPYRITE'
#**********************************************************************
%include pobbconfig.defs
#
# ?[POBBRunWarning]
# ?[POBBRunNotice]

%ifdef pobb_RunMachines

setenv PATH=/bin:/etc
rehash
# Check to see that everything in this machine's configuration is stopped,
#   so that we can reboot safely.
# The startup scripts leave tag files in the config dir (?[BigLocalDir]/config).
# The possibilities for files there:
# queuemail daemons: 'qm.TAG' containing name of queue being serviced or O option for the outgoing daemon
# nntppoll:	'nntppoll' containing 'nntppoll'
# undigest:	'undigest' containing 'undigest'
# cuin:		'cuin.TAG' containing name of script being run
# oldsendmail:	'oldsendmail' containing 'oldsendmail'
# reauth:		'reauth' containing 'reauth'
# Always:	Venus (or afsd) and the queuemail servicing ?[LocalQueue].
echo "##############################"
%ifdef pobb_RunAMSDelivery
set pid=`ps awwwwx | grep queuemail | grep ?[LocalQueue] | grep -v grep`
if ( "$$pid" == "" ) then
	sleep 4
	set pid=`ps awwwwx | grep queuemail | grep ?[LocalQueue] | grep -v grep`
endif
if ( "$$pid" != "" ) then
	echo "## Shutting down ?[LocalQueue] queuemail daemon"
	kill -?[ShutdownSignal] $$pid[1]
endif
%endif pobb_RunAMSDelivery

cd ?[BigLocalDir]/config
if ( $$status != 0 ) exit
foreach FN (*)
	set cont=`cat $$FN`
	set pid=`ps awwwwx | grep -e "$$cont" | grep -v grep`
	if ( "$$pid" == "" ) then
		sleep 4
		set pid=`ps awwwwx | grep -e "$$cont" | grep -v grep`
	endif
	if ( "$$pid" != "" ) then
		if ($$FN =~ cuin.*) then
			echo "## Terminating CUI daemon running $$cont"
			kill -TERM $$pid[1]
		else
			if ($$FN =~ qm.*) then
				echo "## Shutting down $$cont queuemail daemon"
			else
				echo "## Shutting down $$FN ($$cont)"
			endif
			kill -?[ShutdownSignal] $$pid[1]
		endif
	endif
end
echo "##############################"
# Send mail, logging the fact that the machine was being gracefully shut down

echo "Post office/bboard machine `/bin/hostname` being shut down" >> ?[TempLog]/reboot.$$$$
echo "	at `date`" >> ?[TempLog]/reboot.$$$$
echo "	by $$user" >> ?[TempLog]/reboot.$$$$

echo "Shutdown: Completed -- reboot when desired" 

%endif pobb_RunMachines
