#!/bin/sh
#
# $Id: tcp,v 3.4 1993/03/19 14:23:53 mike Exp $
#
# Linux networking startup script.
# Configures the interfaces and starts the daemons.
# It assumes hostname has been set.
#

action=$1

# See if we have tcp at all
if [ ! -f /etc/default/tcp ]; then
	echo "TCP/IP is either not installed or not configured."
	exit
fi

# Load the library and find various programs.
. /etc/init.d/LIB
killall=`pathof killall /bin:/usr/bin`
umount=`pathof umount`

TCPLIB=/etc
HOSTS=$TCPLIB/hosts

# Find out what service daemons are needed. If we don't have any
# listed in /etc/default/tcp we'll use a fairly minimal list of
# our own.
> /tmp/tcp.$$
awk '/^[ \t]*SERVICES/ { sub("^[ \t]*SERVICES", ""); print $0 }' /etc/default/tcp | while read i
do
	echo $i >> /tmp/tcp.$$
done
PROCS="`cat /tmp/tcp.$$`"
rm -f /tmp/tcp.$$
if [ "$PROCS" = "" ]; then
	PROCS="in.named inetd"
fi


case $action in
	start)
		# Sanity checks...
		if [ -r /etc/named.boot ]; then
			use_bind=`awk '/127.0.0.1/' /etc/resolv.conf 2> /dev/null`
			if [ -z "$use_bind" ]; then
				echo "NOTICE: Nameserver boot file exists but resolver not using local nameserver"
				echo "NOTICE:         (add 'nameserver 127.0.0.1' to /etc/resolv.conf"
				echo "NOTICE:          or remove /etc/named.boot)"
			fi
		fi

		started=
		for p in $PROCS
		do
			# The service program could have in. or rpc. prefixed
			# depending on the local religion.
			progname=$p
			it=`pathof $p /etc:/usr/etc:/etc/daemons:$TCPLIB`
			if [ -z "$it" ]; then
				progname=in.$p
				it=`pathof in.$p /etc:/usr/etc:/etc/daemons:$TCPLIB`
			fi
			if [ -z "$it" ]; then
				progname=rpc.$p
				it=`pathof rpc.$p /etc:/usr/etc:/etc/daemons:$TCPLIB`
			fi

			if [ -n "$it" ]; then
				if $killall -0 $progname 2> /dev/null; then
					: already running
				else
					if [ -z "$started" ]; then
						echo $use_escapes "Starting TCP services: \c"
						started=yes
					fi
					$it > /dev/null 2>&1
					echo $use_escapes " $p\c"
				fi
			fi
		done
		if [ -n "$started" ]; then
			echo " ... done"
		fi
		;;

	stop)
		# Unmount any NFS filesystems we have mounted
		$umount -avt nfs

		# Stop NFS (if it's running).
		if [ -x /etc/init.d/nfs ]; then
			/etc/init.d/nfs stop
		fi

		# Kill the service daemons
		if wipeout $PROCS; then
			echo "TCP/IP services stopped"
		else
			echo "TCP/IP services weren't running"
		fi

		# Find out what user daemons may be present. If we don't have
		# any listed in /etc/default/tcp we stick with an empty list.
		> /tmp/tcp.$$
		awk '/^[ \t]*USER/ { sub("^[ \t]*USER", ""); print $0 }' /etc/default/tcp | while read i
		do
			echo $i >> /tmp/tcp.$$
		done
		UPROCS="`cat /tmp/tcp.$$`"
		rm -f /tmp/tcp.$$

		# Kill the user daemons
		if wipeout $UPROCS; then
			echo "TCP/IP user activity stopped"
		else
			: no user activity to kill
		fi
		;;

	*)
		echo "usage: tcp start|stop"
		exit 1
		;;
esac
