#!/bin/sh
#
# $Id: tcp,v 4.1 1993/03/23 16:17:12 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

TCPLIB=/etc
HOSTS=$TCPLIB/hosts
CONFIG=`pathof ifconfig $TCPLIB:$PATH`
if [ -z "$CONFIG" ]; then
	CONFIG=`pathof config $TCPLIB:$PATH`
fi


# 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="inetd named syslogd"
fi



case $action in
	start)
		# Determine IP address
		HOSTNAME=`hostname`
		IPADDR=`awk '/^[ \t]*IPADDR/ { print $2 }' /etc/default/tcp`
		if [ "$IPADDR" = "" ]; then
			IPADDR=`awk "/[ \t]$HOSTNAME([ \t]|\$)/ { print \\\$1 }" $HOSTS`
		fi
		if [ "$IPADDR" = "" ]; then
			echo
			echo "No ip address in /etc/default/tcp and $HOSTNAME not in $HOSTS"
		fi

		# Determine address of router (if any)
		ROUTER=`awk '/^[ \t]*ROUTER/ { print $2 }' /etc/default/tcp`
		if [ "$ROUTER" = "" ]; then
			ROUTER=0.0.0.0
		fi

		# Determine the network address
		NET=`awk '/^[ \t]*NET/ { print $2 }' /etc/default/tcp`

		# Configure external interface (if we know enough)
		if [ -n "$NET" -a -n "$IPADDR" ]; then
			$CONFIG -n $NET -r $ROUTER eth0 $IPADDR
		else
			echo "External interface is *not* configured"
		fi

		# Configure loopback interface
		$CONFIG loopback 127.0.0.1
		if [ "$IPADDR" != "" ]; then
			$CONFIG loopback $IPADDR
		fi

		started=
		for p in $PROCS
		do
			it=`pathof $p /etc:/usr/etc:/etc/daemons:$TCPLIB`
			if [ -n "$it" -a "`pidof $p`" = "" ]; then
				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
		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).
		/etc/init.d/nfs stop

		# Stop RPC (if it's running).
		/etc/init.d/rpc stop

		# Kill the service daemons
		stopped=
		for p in $PROCS
		do
			for i in `pidof $p`
			do
				kill $i
				if [ -z "$stopped" ]; then
					stopped=yes
				fi
			done
		done

		# 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
		for p in $UPROCS
		do
			for i in `pidof $p`
			do
				kill $i
				if [ -z "$stopped" ]; then
					stopped=yes
				fi
			done
		done

		if [ -n "$stopped" ]; then
			echo "TCP/IP stopped"
		fi
		;;

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