#!/bin/csh -f -b
#**********************************************************************
#         Copyright IBM Corporation 1988,1991 - All Rights Reserved
#         For full copyright information see: 'andrew/config/COPYRITE'
#**********************************************************************
# IBM, CARNEGIE MELLON UNIVERSITY, 
# AND THE OTHER COPYRIGHT HOLDERS
#  DISCLAIM ALL WARRANTIES WITH 
# REGARD TO THIS SOFTWARE, INCLUDING 
# ALL IMPLIED WARRANTIES OF MERCHANT-
# ABILITY AND FITNESS. IN 
# NO EVENT SHALL  IBM, CARNEGIE 
# MELLON UNIVERSITY, OR ANY OTHER 
# COPYRIGHT HOLDER BE LIABLE FOR 
# ANY SPECIAL, INDIRECT OR CONSE-
# QUENTIAL DAMAGES OR ANY DAMAGES 
# WHATSOEVER RESULTING FROM LOSS OF
# USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT 
# OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
#  $
%include pobbconfig.defs
#
# ?[POBBRunWarning]
# ?[POBBRunNotice]

%ifdef pobb_RunMachines

setenv PATH /bin:/usr/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
