#
# SCCS_ID - "@(#)S86wintcp (TWG)       1.6     89/07/18 "
#
# @(#) Copyright 1986.  The Wollongong Group, Inc.  All Rights Reserved.
#

#
# Start the network. 
#

USAGE="Usage: /etc/init.d/wintcp {start | stop}"
ETC=/usr/etc

case "$1" in
'start')

#
# Set up the hostname.
#
/usr/bin/hostname `uname -n` > /dev/null 2>&1

# 
# Initialize the network and build the streams.
#
# If processes are already executing, don't re-execute them.
# 
echo "WIN/TCP starting network services"

#
# Inetinit reads information from the file /usr/etc/net.config
# which specifies how streams is configured.
#
pids=`/bin/ps -e | /usr/bin/egrep "inetinit" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
	if ( $ETC/inetinit -l1 )
	then
		:
	else
		exit 1
	fi
fi

NONAMESERVER=1; export NONAMESERVER

#IFCONFIG
# This line is used by the install script to install ifconfig lines.
# DON'T REMOVE
#
# 
# Start up all the daemons we would like to
# have. It looks for the daemons in several
# directories. They should have the name
# end with a 'd' so that it can identify them
# from the name of the service specifed.
#
pids=`/bin/ps -e | /usr/bin/egrep "tcpliste" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
	$ETC/tcplisten ftp telnet finger rlogin remsh rexec
fi

pids=`/bin/ps -e | /usr/bin/egrep "syslogd" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
	$ETC/syslogd
fi

pids=`/bin/ps -e | /usr/bin/egrep "rwhod" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
#$ETC/rwhod
:
fi

pids=`/bin/ps -e | /usr/bin/egrep "named" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
#$ETC/named
:
fi

pids=`/bin/ps -e | /usr/bin/egrep "sendmail" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
( cd /usr/spool/mqueue ; rm -f [lnxf]* ; /usr/lib/sendmail -bd -q30m )
fi
# Initialize kernel hook
echo < /dev/win-net

#
# Dont start tftpd & gated unless you need it.
# tftpd is considered a potential security problem

pids=`/bin/ps -e | /usr/bin/egrep "tftpd" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
:
#$ETC/tftpd
fi

pids=`/bin/ps -e | /usr/bin/egrep "gated" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
:
#$ETC/gated
fi

pids=`/bin/ps -e | /usr/bin/egrep "uucpd" | /usr/bin/cut -c1-6`
if [ "${pids}" = "" ]
then
:
#$ETC/uucpd
fi

#
# Make FTPD log file to have FTPD logging enabled
#
#/bin/echo "" > /tmp/ftpdlog

#
# Start up RFS
#
if [  -s /usr/options/rfs.name ]	# RFS installed ?
then
  if [  -s /usr/net/nls/tcp ]		# Initialized ?
  then
    pids=`grep 105 /usr/net/nls/tcp/dbf`
    if [ "${pids}" != "" ]
    then
	pids=`/bin/ps -e | /usr/bin/egrep "listen" | /usr/bin/cut -c1-6`
	if [ "${pids}" = "" ]
	then
	/usr/bin/nlsadmin -q tcp || /usr/bin/nlsadmin -s tcp
	fi
    fi 
  fi
fi
	;;
'stop')

#
# If /usr isn't mounted, the daemons cannot be running.
#
if [ `/etc/mount | grep -c "^/usr on "` -eq 0 ]
then
	exit 0
fi

echo "WIN/TCP stopping network services"
#
# send death of child signal in case anyone is remotely logged on
#
pids=`ps -e | /usr/bin/egrep "telnetd|rlogind|ftpd" | /usr/bin/cut -c1-6`
if [ "$pids" != "" ]
then
	kill -18 $pids
fi

#
# Stop All Network Daemons.
#
pids=`/bin/ps -e | /usr/bin/egrep "tcpliste|tftpd|rwhod|gated|named|sendmail|syslogd" | /usr/bin/cut -c1-6`
if [ "${pids}" != "" ]
then
	/bin/kill  ${pids}
fi

#
# Make sure we've nuked all the daemons
#
sleep 2
pids=`/bin/ps -e | /usr/bin/egrep "ftpd|telnetd|fingerd|rlogind|remshd|rexecd|syslogd" | /usr/bin/cut -c1-6`
if [ "${pids}" != "" ]
then
	/bin/kill  ${pids}
fi

pids=`/bin/ps -e | /usr/bin/egrep "uucpd|ulogin" | /usr/bin/cut -c1-6`
if [ "${pids}" != "" ]
then
	/bin/kill  ${pids}
fi

# 
# Kill the listener process associated with the netspec tcp
#
pids=`/bin/ps -e | /usr/bin/egrep "listen" | /usr/bin/cut -c1-6`
if [ "${pids}" != "" ]
then
	/usr/bin/nlsadmin -q tcp && /usr/bin/nlsadmin -k tcp
fi

#
# Kill inetinit now.
#
pids=`/bin/ps -e | /usr/bin/egrep "inetinit" | /usr/bin/cut -c1-6`
if [ "${pids}" != "" ]
then
	/bin/kill  ${pids}
fi

#
# wait for things to die
#
cnt=0
sleep 2
pids=`/bin/ps -e | /usr/bin/egrep "inetinit" | /usr/bin/cut -c1-6`
while  [ "$pids" != "" -a $cnt -le 10 ]
do
	sleep 2
	cnt=`/bin/expr $cnt + 1`
	pids=`/bin/ps -e | /usr/bin/egrep "inetinit" | /usr/bin/cut -c1-6`
done

# if inetinit is still alive then warn user
if [ "$pids" != "" ]
then
	echo "\n\t    ***  WARNING - couldn't kill inetinit  ***"
	exit 1
fi

	;;
*)
	echo ${USAGE}
	exit 1
	;;
esac
