#! /bin/bsh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# tcpip720 src/tcpip/etc/rc.tcpip 1.82.1.4 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1985,1995 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)95        1.82.1.4  src/tcpip/etc/rc.tcpip, tcp_apps, tcpip720, 1510A_720 2/3/15 15:46:16
# 
# COMPONENT_NAME: TCPIP rc.tcpip
# 
# FUNCTIONS: 
#
# ORIGINS: 26  27 
#
# (C) COPYRIGHT International Business Machines Corp. 1985, 1996
# All Rights Reserved
# Licensed Materials - Property of IBM
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
####################################################################
# rc.tcpip - 
#	assumes interfaces are brought up by /etc/rc.net
#	starts TCP/IP daemons (sendmail, inetd, etc.)
####################################################################
# start -
#	starts daemons using either src or command-line method
# args:
#	$1: pathname of daemon
#	$2: non-null if we should use src to start the daemon
#	$3: any arguments to pass it
#

PATH=/usr/bin:/usr/sbin:$PATH
export PATH

start()
{
	# just return if the daemon doesn't exist
	#
	[ -x $1 ] || return 0

	# start the daemon using either src or command-line method
	#
	cmd=`basename $1`
	if [ -n "$2" ] ; then
		startsrc -s $cmd -a "$3" &
	else
		if [ $cmd = "portmap" ] ; then
			$1 $3 &	 # portmap must start in background
		else
			$1 $3
		fi
		echo "\t$cmd"
	fi
}

# check the bootup_option flag in the configuration database
option=`lsattr -E -l inet0 -a bootup_option -F value`
if [ "$option" = "no"  ]
then
####################################################################
#
# Check to see if srcmstr is running; if so, we try to use it;
# otherwise, we start the daemons without src
#
i=3  # make sure init has time to start it
while [ $i != 0 ] ; do
	if [ -n "`ps -e | awk '$NF == "srcmstr" { print $1; exit }'`" ] ; then
		src_running=1  # set flag
		break
	fi
	sleep 1		# otherwise wait a second and try again
	i=`expr $i - 1`
done

# If srcmstr is running, ensure that it is active before issuing the
# startsrc commands
#
if [ -n "$src_running" ] ; then
	echo "Checking for srcmstr active...\c"
	i=10  # try ten times to contact it
	while [ $i != 0 ] ; do
		lssrc -s inetd >/dev/null 2>&1 && break  # break out on success
		echo ".\c"
		sleep 1		# otherwise wait a second and try again
		i=`expr $i - 1`
	done
	if [ $i = 0 ] ; then
		echo "\n\nERROR: srcmstr is not accepting connections.\n"
		exit 1
	fi
	echo "complete"
fi

else
	src_running=""
fi
# Begin: dhcpcd_IF_check
dhcpcd_IF_check()
{
	 loop=11
	 interval=5
	 count=0
	 dhcp_active=5
         num_IF=0
         ip_line=0


	 # If srcmstr is available.
	 if [ -n "$1" ] ; then

		 while [ $dhcp_active != 0 ] ; do
			 status=`lssrc -ls dhcpcd`
			 if [ $? -eq 0 ];then
				 break
			 fi
			 sleep 1     # otherwise wait a second and try again
			 dhcp_active=`expr $dhcp_active - 1`

		 done

		 if [ $dhcp_active = 0 ] ; then
			 return 0
		 fi

		 num_IF=`echo "$status" | egrep -c en[0-9]*`
                 if [ $num_IF = 0 ]; then
                         return 0
                 fi

                 ip_line=`expr $count % $num_IF + 1`
                 ipaddress=`echo "$status" | egrep en[0-9]* | awk ' {if(NR=='$ip_line') print $2}'`

		 while [ $ipaddress = "0.0.0.0" ]
		 do
			 if [ $count -gt $loop ]; then
				  return 0
			 fi
			 sleep $interval
			 count=`expr $count + 1`
			 ip_line=`expr $count % $num_IF + 1`
                         ipaddress=`echo "$status" | egrep en[0-9]* | awk ' {if(NR=='$ip_line') print $2}'`

		 done
	 fi
}
# Start up TCP traffic regulation (TCPTR)
#
/etc/rc.tcptr

# Start up the daemons
#
echo "Starting tcpip daemons:"
trap 'echo "Finished starting tcpip daemons."' 0

# Start up dhcpcd daemon 
#start /usr/sbin/dhcpcd "$src_running"
dhcpcd_IF_check "$src_running"

# Start up autoconf6 process
#start /usr/sbin/autoconf6 "" 

# Start up ndpd-host daemon
#start /usr/sbin/ndpd-host "$src_running"

# Start up the ndpd-router daemon
#start /usr/sbin/ndpd-router "$src_running"

# Start up syslog daemon (for error and event logging)
start /usr/sbin/syslogd "$src_running"

# Start up print daemon 
#start /usr/sbin/lpd "$src_running"

# Start up routing daemon (only start ONE)
#start /usr/sbin/routed "$src_running" -q
#start /usr/sbin/gated "$src_running"

# Start up the sendmail daemon.
#
# Sendmail will automatically build the configuration and alias
# data bases the first time it is invoked.  You may wish to update
# the alias source file /usr/lib/aliases with local information,
# and then rebuild the alias data base by issuing the command
# "/usr/lib/sendmail -bi" or "/usr/ucb/newaliases".
#
# When the configuration or alias data bases are changed, the
# sendmail daemon can be made to rebuild and re-read them by
# issuing the command "kill -1 `cat /etc/sendmail.pid`" or, if
# SRC was used to start the daemon, "refresh -s sendmail".
#
# The "qpi", or queue processing interval, determines how
# frequently the daemon processes the message queue.
#
qpi=30m  # 30 minute interval
#
start /usr/lib/sendmail "$src_running" "-bd -q${qpi}"

# Start up Portmapper
# If we are a Thin Server with NFSv4 filesystems, portmap has been
# started at the very beginning of phase 3 in rc.boot.
# In this case, no need to restart it.
portmap_pid=`ps -e | awk '$NF == "portmap" {print $1}'`
[ -z "$portmap_pid" ] && start /usr/sbin/portmap "${src_running}"

# Start up socket-based daemons
start /usr/sbin/inetd "$src_running"

# Start up Domain Name daemon
#start /usr/sbin/named "$src_running"

# Start up time daemon
#start /usr/sbin/timed "$src_running"

# Start up Network Time Protocol (NTP) daemon
start /usr/sbin/xntpd "$src_running" "-x"

# Start up rwhod daemon (a time waster)
#start /usr/sbin/rwhod "$src_running"

# Start up the Simple Network Management Protocol (SNMP) daemon
start /usr/sbin/snmpd "$src_running"

# Start up the DHCP Server
#start /usr/sbin/dhcpsd "$src_running"

# Start up the DHCP Relay Agent
#start /usr/sbin/dhcprd "$src_running"

# Start up the DPID2 daemon
#start /usr/sbin/dpid2 "$src_running"

# Start up the hostmibd daemon
start /usr/sbin/hostmibd "$src_running"

# Start up the snmpmibd daemon
start /usr/sbin/snmpmibd "$src_running"

# Start up the aixmibd daemon
start /usr/sbin/aixmibd "$src_running"

# Start up the mrouted daemon
#start /usr/sbin/mrouted "$src_running"

# Start up pxed daemon 
#start /usr/sbin/pxed "$src_running"

# Start up binld daemon 
#start /usr/sbin/binld "$src_running"

# Start up netcd daemon
#start /usr/sbin/netcd "$src_running"

# Start up ptpd daemon
#start /usr/sbin/ptpd "$src_running"

# Wait for srcmstr to complete
if [ -n "$src_running" ] ; then
	wait
fi
