
# netrc
#
#	usage: $0 kill [inetd] [nfs] [named] [sendmail|sm] [timed]
#	       $0 [inetd] [nfs] [named] [sendmail|sm] [timed]
#
#	Options to: named [-d debuglevel] [-p port#] [[-b] bootfile]
#	            sendmail|sm [flags]
#	            timed [-t] [-M] [-n network] [-i network]
#
#	kill		Kills specified daemons (default: TCP and NFS)
#
#	init		Initialize kernel with Internet address
#
#	inetd		Starts Internet daemon
#
#	nfs		Network File System daemons
#
#	named		Domain name server
#
#	sendmail	Internet mail router
#
#	timed		Time daemon
#
	
TCPIP_PARAMS="/etc/init.d/tcp_ip.params"

NAMEDCMD="/usr/lib/named"
NAMED_DEFAULT="/etc/named.boot"

SENDMAILCMD="/usr/lib/sendmail"
SM_CONFIG="/usr/lib/sendmail.cf"
SM_DEFAULT="-bd -q30m"

TIMEDCMD="/etc/timed"
TIMED_DEFAULT="-M"

usage()
{
	echo "usage: $0 kill [inetd] [nfs] [named] [sendmail|sm] [timed]"
	echo "       $0 [inetd] [nfs] [named] [sendmail|sm] [timed]\n"
	echo "Options to: named [-d debuglevel] [-p port#] [[-b] bootfile]"
	echo "            sendmail|sm [flags]"
	echo "            timed [-t] [-M] [-n network] [-i network]"
	exit 1
}

do_cmd()
{
	DO_CMD=$1
	pid="`/bin/ps -e 2>/dev/null | /bin/grep $DO_CMD | \
		/bin/grep -v grep | line`"

 	if [ "$pid" ]; then
		set $pid
		while [ "$1" ]; do
			PID=$1
			/bin/kill -9 $PID
			if [ $? -eq 0 ]; then
				KILLED=1
			else
				KILLED=""
			fi
			shift; shift; shift; shift
		done
	elif [ "$KILL" ]; then
		KILLED=""
 	fi

	if [ "$INIT" -o "$DEFAULT" ]; then
		case $DO_CMD in
			named)
				echo $NAMEDCMD
				$NAMEDCMD &
				;;
			sendmail|sm)
				NAMESERVER="`/bin/ps -e 2>/dev/null | /bin/grep named | grep -v grep`"
				if [ ! "$NAMESERVER" ]; then
					echo "$0: /usr/lib/named must be initialized in order to use sendmail"
					exit 1
				else
					if [ -f /usr/lib/sendmail.rel -a ! -f /usr/lib/sendmail ]; then

						mv /usr/lib/sendmail.rel /usr/lib/sendmail
						chmod +xs /usr/lib/sendmail
						rm -f /usr/spool/mqueue/[lnx]f*
					fi
					echo $SENDMAILCMD
					$SENDMAILCMD &
				fi
				;;
			timed)
				echo $TIMEDCMD
				$TIMEDCMD &
				;;
			*)
				if [ -x /etc/$DO_CMD ]; then
					/etc/$DO_CMD
					echo $DO_CMD started
				else
					echo "$0: /etc/$DO_CMD not found or not executable"
					exit 1
				fi
				;;
		esac
	fi
}

exec_cmd()
{
	CMD=$1
	if [ "`eval echo \\$$CMD`" ]; then
		if [ "$INIT" -o "$DEFAULT" ]; then
			echo "*** Initializing $CMD"
		else
			echo "*** Stopping $CMD"
		fi
		shift

		for i in $*; do
			do_cmd $i
		done

		if [ "$INIT" -o "$DEFAULT" ]; then
			echo "*** $CMD initialized"
		elif [ "$KILLED" ]; then
			echo "*** $CMD stopped"
		else
			echo "*** $CMD not running"
		fi
	fi
}

netinit()
{
	if [ ! -r /etc/ifinit -o ! -r /etc/hosts ]; then
		echo $0: cannot find /etc/ifinit /etc/hosts
		exit 1
	fi
	pid=`/bin/ps -e 2>/dev/null | /bin/grep inetd | \
		/bin/grep -v grep | line`
	if [ ! "$pid" ]; then
		/etc/ifinit
		if [ $? -ne 0 ]; then
			echo $0: network initialization failed
			exit 1
		fi
		echo "*** Network initialized"
	else
		echo "*** Network already initialized"
	fi
}

# MAIN

if [ $# -eq 0 ]; then
	ARG=init
	SHIFTED=1
else
	ARG=$1
fi

case $ARG in
	init)
		INIT=1
		netinit
		;;
	kill)
		KILL=1
		;;
	inetd|tcp|nfs|sendmail|sm|named|timed)
		DEFAULT=init
		netinit
		continue
		;;
	*)
		echo "$0: unrecognized argument: $ARG"
		usage
		;;
esac

if [ "$INIT" -a ! "$SHIFTED" -o "$KILL" ]; then
	shift
fi

while [ "$1" ]; do
	ARG=$1
	case $ARG in
		inetd|tcp)
			INETD=1
			;;
		nfs)
			NFS=1
			;;
		sendmail|sm)
			SENDMAIL=1
			OPTIONS=`echo $2 | cut -c1 | grep '-'`
			if [ "$OPTIONS" ]; then
				shift
				while [ -n "$1" ]; do
					SHIFTED=1
					OPTIONS=$1
					case $OPTIONS in
						inetd|tcp|nfs|sendmail|sm|named|timed)
							break
							;;
						-C*)
							CF="`echo $OPTIONS | cut -dC -f2`"
							if [ ! -r $CF ]; then
		   						echo "$0: $CF not found or not readable"
								exit 1
							else
								SM_OPTIONS="$SM_OPTIONS $OPTIONS"
							fi
							;;
						*)
							SM_OPTIONS="$SM_OPTIONS $OPTIONS"
							;;
					esac
					shift
				done
			fi
			;;
		named)
			NAMED=1
			OPTIONS="`echo $2 | cut -c1 | egrep '\/|-'`"
			if [ "$OPTIONS" ]; then
				shift
				while [ -n "$1" ]; do
					SHIFTED=1
					OPTIONS=$1
					case $OPTIONS in
						inetd|tcp|nfs|sendmail|sm|named|timed)
							break
							;;
						/*)
							BOOTFILE=$OPTIONS
							if [ ! -r $BOOTFILE ]; then
								echo "$0: $BOOTFILE not found or not readable"
								exit 1
							else
								NAMED_OPTIONS="$NAMED_OPTIONS $BOOTFILE"
							fi
							;;
						-b)
							BOOTFILE="$2"
							if [ ! "$BOOTFILE" ]; then
								echo "usage: $0 named -b bootfile"
								exit 1
							elif [ ! -r $BOOTFILE ]; then
		   						echo "$0: $BOOTFILE not readable"
								exit 1
							else
								shift
								NAMED_OPTIONS="$NAMED_OPTIONS $OPTIONS $BOOTFILE"
							fi
							;;
						-d)
							DEBUG_LEVEL="$2"
							case "$DEBUG_LEVEL" in
								"")
									echo "$0: a debuglevel must be specified with the -d option"
									exit 1
									;;
								[0-9]*)
									shift
									NAMED_OPTIONS="$NAMED_OPTIONS $OPTIONS $DEBUG_LEVEL"
									;;
								*)
									echo "$0: the debuglevel must be a number."
									exit 1
									;;
							esac
							;;
						-p)
							PORT_NUM="$2"
							case "$PORT_NUM" in
								"")
									echo "$0: a port # must be specified with the -d option"
									exit 1
									;;
								[0-9]*)
									shift
									NAMED_OPTIONS="$NAMED_OPTIONS $OPTIONS $PORT_NUM"
									;;
								*)
									echo "$0: the port # must be a number."
									exit 1
									;;
							esac
							;;
						*)
							echo "$0: unrecognized argument: $NAMEDCMD $OPTIONS"
							usage
							;;
					esac
					shift
				done
			fi
			;;
		timed)
			TIMED=1
			OPTIONS=`echo $2 | cut -c1 | grep '-'`
			if [ "$OPTIONS" ]; then
				shift
				while [ -n "$1" ]; do
					SHIFTED=1
					OPTIONS=$1
					case $OPTIONS in
						inetd|tcp|nfs|sendmail|sm|named|timed)
							break
							;;
						-M|-t)
							TIMED_OPTIONS="$TIMED_OPTIONS $OPTIONS"
							;;
						-i|-n)
							NETWORK="$2"
							case "$NETWORK" in
								"")
		   							echo "$0: a network must be specified with the $OPTIONS option"
									exit 1
									;;
								*)
									shift
									TIMED_OPTIONS="$TIMED_OPTIONS $OPTIONS $NETWORK"
									;;
							esac
							;;
						*)
							echo "$0: unrecognized argument: $TIMEDCMD $OPTIONS"
							usage
							;;
					esac
					shift
				done
			fi
			;;
		init|kill)
			echo "$0: init and kill cannot be used together"
			usage
			;;
		*)
			echo "$0: unrecognized argument: $ARG"
			usage
			;;
	esac
	CMD_SET=1
	if [ ! "$SHIFTED" ]; then
		shift
	fi
	SHIFTED=""
done

if [ "$INIT" -a ! "$CMD_SET" ]; then
	INETD=1
fi

if [ "$KILL" -a ! "$CMD_SET" ]; then
	INETD=1
	NFS=1
fi

if [ "$KILL" -a "$INETD" ]; then
	NAMED=1
	SENDMAIL=1
	TIMED=1
fi

if [ "$NAMED" ]; then
	if [ ! "$NAMED_OPTIONS" -a -r $TCPIP_PARAMS ]; then
		NAMED_OPTIONS="`grep named_ARG $TCPIP_PARAMS | cut -d'\"' -f2`"
	fi
	if [ ! "$NAMED_OPTIONS" ]; then
		NAMED_OPTIONS="$NAMED_DEFAULT"
	fi
	NAMEDCMD="$NAMEDCMD $NAMED_OPTIONS"
fi

if [ "$SENDMAIL" ]; then
	if [ ! "$SM_OPTIONS" -a -r $TCPIP_PARAMS ]; then
		SM_OPTIONS="`grep smtp_ARG $TCPIP_PARAMS | cut -d'\"' -f2`"
	fi
	if [ ! "$SM_OPTIONS" ]; then
		SM_OPTIONS="$SM_DEFAULT"
	fi
	SENDMAILCMD="$SENDMAILCMD $SM_OPTIONS"
	SMTP="`echo $SENDMAILCMD | grep '\-bd'`"
	if [ ! "$SMTP" ]; then
		SENDMAILCMD="$SENDMAILCMD -bd"
	fi
	SMTP="`echo $SENDMAILCMD | grep '\-q'`"
	if [ ! "$SMTP" ]; then
		SENDMAILCMD="$SENDMAILCMD -q30m"
	fi
fi

if [ "$TIMED" ]; then
	if [ ! "$TIMED_OPTIONS" -a -r $TCPIP_PARAMS ]; then
		TIMED_OPTIONS="`grep timed_ARG $TCPIP_PARAMS | cut -d'\"' -f2`"
	fi
	if [ ! "$TIMED_OPTIONS" ]; then
		TIMED_OPTIONS="$TIMED_DEFAULT"
	fi
	TIMEDCMD="$TIMEDCMD $TIMED_OPTIONS"
fi

exec_cmd INETD inetd
exec_cmd NFS portmap mountd nfsd
exec_cmd NAMED named
exec_cmd SENDMAIL sendmail
exec_cmd TIMED timed
