#!/bin/sh
#
# ZMailer startup and maintenance commands
#
# Do not edit this file, instead edit the source in proto/zmailer.sh
#
#	Copyright 1990 by Rayan S. Zachariassen, all rights reserved.
#	This will be free software, but only when it is finished.
#
#	Some hacking by Matti Aarnio, Copyright 1990-1995
#
PATH=/bin:/usr/bin:/usr/ucb
ZCONFIG=/etc/zmailer.conf

export PATH

# Sense how the  echo  works, it is either, or..
case "`echo "foo\c"`" in
foo)	eopt='' ; eeol='\c' ;;
*)	eopt='-n' ; eeol='' ;;
esac

. $ZCONFIG || exit 1

case $POSTOFFICE in
/*)	;;
*)	echo "$0: panic!! can't initialize from $ZCONFIG"
	exit 1
	;;
esac

cd $POSTOFFICE	# So that possible cores are there..

if [ ! -d $MAILBOX ]; then
	echo "$0: panic!!  MAILBOX-variable does not point to a directory!  Verify $ZCONFIG!"
	exit 1
fi

# set up the default arguments
case $# in
0)	set -$- router scheduler smtpserver ;;
esac

KILL=

for op in $@
do
	shift
	case "$op" in
	kill|nuke|kills|killr)	# print nothing
		;;
	*)	case $KILL in
		-TERM)	echo $eopt "kill $eeol" ;;
		-KILL)	echo $eopt "nuke $eeol" ;;
		*)	echo $eopt "start $eeol" ;;
		esac
		echo $eopt "${op}... $eeol"
		;;
	esac
	case "$op" in
	router|outer)
			case $KILL in
			-*)	if [ -f $POSTOFFICE/.pid.$op ]; then
					$MAILBIN/router -k > /dev/null
#				else
#					echo $eopt "(warning: no .pid.$op file) $eeol"
				fi ;;
			*)
				if [ -f $POSTOFFICE/.freeze ]; then
					echo "Sorry, Zmailer is frozen, won't start anything until thawed!"
					exit 0
				fi
				if [ "x$NROUTERS" != x ]; then
					$MAILBIN/router -dkn $NROUTERS
				else
					$MAILBIN/router -dk
				fi
				;;
			esac
			;;
#	oscheduler)
	osched*)
			case $KILL in
			-*)	kill $KILL `cat $POSTOFFICE/.pid.$op` ;;
			*)
				if [ -f $POSTOFFICE/.freeze ]; then
					echo "Sorry, Zmailer is frozen, won't start anything until thawed!"
					exit 0
				fi
				cd $POSTOFFICE/transport
				# must send signal to scheduler to make it
				# stop reading the directory for new files
				PIDFILE=.pid.scheduler
				if [ -f ../$PIDFILE ]; then
					kill -TERM `cat ../$PIDFILE` 2>/dev/null
#				else
#					echo $eopt "(warning: no $PIDFILE file) $eeol"
				fi
				$MAILBIN/scheduler-old
				;;
			esac
			;;
	sched*|ched*)
			case $KILL in
			-*)	kill $KILL `cat $POSTOFFICE/.pid.$op` ;;
			*)
				if [ -f $POSTOFFICE/.freeze ]; then
					echo "Sorry, Zmailer is frozen, won't start anything until thawed!"
					exit 0
				fi
				cd $POSTOFFICE/transport
				# must send signal to scheduler to make it
				# stop reading the directory for new files
				PIDFILE=.pid.scheduler
				if [ -f ../$PIDFILE ]; then
					kill -TERM `cat ../$PIDFILE` 2>/dev/null
#				else
#					echo $eopt "(warning: no $PIDFILE file) $eeol"
				fi
				$MAILBIN/scheduler $SCHEDULEROPTIONS
				;;
			esac
			;;
	smtp*|mtp*)
			case $KILL in
			-*)	if [ -f $POSTOFFICE/.pid.$op ]; then
					kill $KILL `cat $POSTOFFICE/.pid.$op`
#				else
#					echo $eopt "(warning: no .pid.$op file) $eeol"
				fi ;;
			*)
				if [ -f $POSTOFFICE/.freeze ]; then
					echo "Sorry, Zmailer is frozen, won't start anything until thawed!"
					exit 0
				fi
				cd / ; $MAILBIN/smtpserver $SMTPOPTIONS
				;;
			esac
			;;
	new|newaliases)     $MAILBIN/router -f $MAILBIN/newaliases
			;;
	kill|nuke|kills|killr)
			case $# in
			0)	exec $0 $op router scheduler smtpserver ;;
			esac
			case $op in
			kill)	KILL="-TERM" ;;
			nuke)	KILL="-KILL" ;;
			esac
			;;
	resubmit)	(cd $POSTOFFICE/deferred &&
			 mv -i [0-9]* ../router) 2> /dev/null
			;;
	bootclean)	rm -f $POSTOFFICE/.pid.* 2> /dev/null
			;;
	cleanup)	(cd $POSTOFFICE ;
			 find public -type f -mtime +2 -print |
				xargs rm -f
			 find postman -type f -mtime +7 -name '[0-9]*' -print |
				xargs rm -f
			)
			;;
	freeze)		touch $POSTOFFICE/.freeze
			;;
	unfr*|thaw)	rm -f $POSTOFFICE/.freeze
			;;
	*)		echo $0: unknown option: $op
			errflg=1
			;;
	esac
done
echo
case $errflg in
1)	echo Usage: $0 "[ router | scheduler | smtpserver | kill | resubmit | bootclean | cleanup | freeze | thaw ]"
	exit 1
	;;
esac
exit 0
