#!/bin/sh
#
#    Copyright (c) 1996-2000 Brocade Communications Systems, Inc.
#    All rights reserved.
#
#    Initialization script to start/stop network services and interfaces.
#

CONFIG=/etc
CHECK=/sbin/chkconfig

if $CHECK verbose ; then
    ECHO='echo -e'
    VERBOSE=-v
    QUIET=
else
    ECHO=:
    VERBOSE=
    QUIET=-q
fi

YPCAT=/usr/bin/ypcat
YPSETDOM=/bin/ypdomainname
YPDOMFILE=/var/yp/ypdomain
LOCALHOST=127.0.0.1
IFCONFIG=/sbin/ifconfig
ROUTE=/sbin/route

AUTOMOUNT=/usr/sbin/automount
AUTOFILE=/etc/auto.master
AUTOMAP=auto.master

#
# opts <additional automount options>
#
# Filter automount options
#
opts()
{
    echo "$1" | sed -e 's/--*t\(imeout\)*[ \t]*[0-9][0-9]*//g
		        s/\(^\|[ \t]\)-/\1/g'
}

#
# getmounts
# 
# Check for file- or NIS-based automount maps
#
getmounts()
{
    #
    # Check for local, file-based maps to load.
    #
    if [ -f $AUTOFILE ]; then
	cat $AUTOFILE | sed -e '/^#/d' -e '/^$/d'| (
	    while read dir map options
	    do
		if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' ]; then

		    mountoptions=`cat $CONFIG/automount.options 2> /dev/null`
		    options=`opts $options`

		    cmd="$AUTOMOUNT $mountoptions $dir"

		    case $map in
		    /*)	
			if [ -x $map ]; then
			    echo "$cmd program $map $options"
			elif [ -f $map ]; then
			    echo "$cmd file $map $options"
			else
			    echo "$cmd `basename $map` $options"
			fi
			;;
		    *)
			if [ -f /etc/$map ] ; then
			    echo "$cmd file /etc/$map $options"
			else
			    echo "$cmd yp $map $options"
			fi
			;;
		    esac
		    unset cmd
		fi
	    done
	)
    fi

    $YPCAT -k $AUTOMAP
    # Check for remote, NIS-based maps to load

    if [ -x $YPCAT ] && [ `$YPCAT -k $AUTOMAP 2>/dev/null | wc -l` -gt 0 ]; then
	$YPCAT -k $AUTOMAP | (
	while read dir map options
	do
	    if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' ]; then
		map=`echo "$map" | sed -e 's/^auto_/auto./'`
		
		mountoptions=`cat $CONFIG/automount.options 2> /dev/null`
		options=`opts $options`
		cmd="$AUTOMOUNT $mountoptions $dir"

		echo "$cmd yp $map $options"
	    fi
	done
	)
    fi
}

#
# configif <name> <address> <number> 
#
# Configure a network interface
#
configif()
{
    $IFCONFIG $1 inet $2 `cat $CONFIG/ifconfig-$3.options 2> /dev/null` \
	2> /dev/null
}

#
# nfsroot
#
# Determine whether or not this system is running an NFS root
# file system.
#
nfsroot()
{
    grep ' / nfs' /proc/mounts > /dev/null 2>&1
    return
}
    

case "$1" in

'start')
    # Try to determine if we should come up in standalone mode or not.

    HOSTNAME=`hostname`
    NETMODE="standalone"

    if nfsroot; then
	NETMODE="inet"
    elif test -x /usr/bin/hostid; then
	if test "`hostid`" = "0"; then
	    SAREASON="Cannot resolve $HOSTNAME.\n"
	elif test "`hostid`" = "0a000000"; then
	    SAREASON="IP address set to factory default.\n"
	elif $CHECK network; then
	    NETMODE="inet"
	fi
    elif $CHECK network; then
	NETMODE="inet"			# Punt and assume it's OK
    fi

    # Figure out what the correct interface name(s) and address(es) is/are.
    # Here, "name" is the name of the network interface as reported by
    # 'netstat -i' and "address" is an IP address in dot notation.
    #
    # Edit /etc/config/if.options to override values used below.
    #
    # Note: If this host has more than 1 interface1, you must edit
    # /etc/config/if.options to set the IFNAME# and IFADDR# variables
    # as appropriate, and set IFNUM to the number of interfaces if
    # there are more than 2.

    # Setup default best-guesses

    IFADDR1=$HOSTNAME
    IFNUM=2

    # Derive interface names, don't worry about fc# interfaces, those
    # won't come up until after Fabric OS is up.

    if $IFCONFIG eth0 >/dev/null 2>&1; then
	IFNAME1=eth0
    fi

    # Source any platform-dependent configuration options.

    if test -s $CONFIG/if.options; then
	. $CONFIG/if.options
    fi

    # Setup PHY configuration prior to ifconfig up

    THIS_CP=$(sin |grep "^Control Processor" |sed 's/^.*.: //g')
    MIICFG=$CONFIG/fabos/mii.$THIS_CP.cfg
    if test -s $MIICFG; then
	$(cat $MIICFG) 1>/dev/null
    fi

    # Configure the primary network interface.

    if test "$NETMODE" = "inet"; then
	if ! configif $IFNAME1 $IFADDR1 1; then
	    SAREASON="Cannot access primary network interface $IFNAME1.\n"
	    NETMODE="standalone"
	fi
    elif test "$NETMODE" != "standalone"; then
	NETMODE="standalone"
    fi

    if test $NETMODE = "standalone"; then
	$ECHO "${SAREASON}Using standalone network mode.\n"
	sleep 5

	# At this point, there's not much we can do. Try to program
	# the primary interface and then take it down.

	if test "$IFNAME1" != ""; then
	    $IFCONFIG $IFNAME1 inet $IFADDR1 2> /dev/null
	    $IFCONFIG $IFNAME1 down          2> /dev/null
	fi
	
	# Configure the loopback interface and program the IP multicast
	# default route.

	$IFCONFIG lo $LOCALHOST
	$ROUTE add -net 224.0.0.0 netmask 240.0.0.0 $IFNAME1 > /dev/null
    else
	# Initialize secondary, tertiary, etc network interfaces.

	num=2
	while test $num -le $IFNUM; do
	    eval ifname='$'IFNAME${num} ifaddr='$'IFADDR${num}
	    if test -n "$ifname"; then
		if $IFCONFIG $ifname >/dev/null 2>&1; then
		    configif $ifname $ifaddr $num
		fi
	    fi

	    ((num++))
	done

	# Configure the loopback interface

	$IFCONFIG lo $LOCALHOST
    fi

    # Send traffic for this host through the loop-back for better performance

    $ROUTE add -host $HOSTNAME gw $LOCALHOST

    HWMODEL=`sin | grep "Platform" | sed -e"s/[    ][       ]*//g" | cut -d, -f1 | cut -d: -f2 2> /dev/null`
    if [ "$HWMODEL" = "SWBD10" ]; then
                $ECHO PLATFORM = swbd10
 
		#configure the internal ethernet eth1 so that proxy 
		#can be started earlier
		CPID=`/sbin/sin | grep "Processor No" | sed -e"s/[  ][       ]*/ /g" | cut -d: -f2 2> /dev/null`
		$ECHO CPID = $CPID
		if [ "$CPID" -eq 0 ]; then
			$IFCONFIG eth1 10.0.0.5 netmask 255.255.255.0 >/dev/null 2>&1
			$ROUTE add -host 10.0.0.6 dev eth1 >/dev/null 2>&1
		else
			$IFCONFIG eth1 10.0.0.6 netmask 255.255.255.0 >/dev/null 2>&1
			$ROUTE add -host 10.0.0.5 dev eth1 >/dev/null 2>&1
		fi
    fi

    # Configure static route(s), if available.

    if test $NETMODE = "inet" -a -s $CONFIG/static-routes.options; then
	. $CONFIG/static-routes.options
    fi
    route | grep -q default
    if [ $? -ne 0 ]; then
	    # Add a default route to the host itself
	    # in case no route present in /etc/static-routes.options
	    $ROUTE add default gw $IFADDR1
	    $ECHO "added route $IFADDR1"
    fi

    $ECHO "Network daemons:\c"

	/usr/bin/killall -q gated routed portmap

	if test $NETMODE = "inet"; then

	    # Start either gated or routed if they are installed and
	    # configured.

	    if $CHECK gated && test -x /usr/sbin/gated; then
		/usr/sbin/gated `cat $CONFIG/gated.options 2> /dev/null` &
						    $ECHO " gated\c"

	    elif $CHECK routed && test -x /usr/sbin/routed; then
		/usr/sbin/routed `cat $CONFIG/routed.options 2> /dev/null` &
						    $ECHO " routed\c"
	    fi					    
	fi

	# Program the IP multicast default route.

	$ROUTE add -net 224.0.0.0 netmask 240.0.0.0 $IFNAME1 > /dev/null

        # Start the RPC program number mapper (portmap)

        if test -x /sbin/portmap; then
	    /sbin/portmap `cat $CONFIG/portmap.options 2> /dev/null` &
                                                    $ECHO " portmap\c"
        fi

    $ECHO "."

    # NIS Support

    # Define the NIS domain name.

    if test -x $YPSETDOM; then
        # The domain file is needed only if NIS domain != Internet domain.
        YPDOMAIN=`cat $YPDOMFILE 2> /dev/null`

        if test -z "$YPDOMAIN"; then
            # Extract the domain from the hostname.
            YPDOMAIN=`echo $HOSTNAME | sed -e 's/[^.]*\.\(.*\)/\1/'`
            if test "$YPDOMAIN" = "$HOSTNAME"; then
                # Hostname doesn't contain domain.
                YPDOMAIN=""
            fi
        fi

        # Set the domain even if "yp" is not "on" so ypbind can be
        # started by hand later.

        if test "$YPDOMAIN"; then
            $YPSETDOM $YPDOMAIN
        fi
    fi

    # Start NIS daemons.

    if test $NETMODE = "inet" && $CHECK nis; then
        if test -z "$YPDOMAIN"; then
            $ECHO "Cannot start NIS -- domain name not defined.\n"
            $ECHO "Edit $YPDOMFILE to contain your domain name and reboot.\n"
            sleep 10
        else
            $YPSETDOM $YPDOMAIN
            /usr/bin/killall -q ypbind ypserv rpc.passwd

            $ECHO "NIS domain: $YPDOMAIN"
            $ECHO "NIS daemons:\c"

            # ypbind allows NIS clients to use NIS services.

            if test -x /sbin/ypbind; then
                /sbin/ypbind `cat $CONFIG/ypbind.options 2> /dev/null` &
                                                    $ECHO " ypbind\c"

		# wait till ypbind running or time out
		reps=0
		until ypwhich > /dev/null 2>&1 || [ "$reps" = "10" ]
		do
		    $ECHO ".\c" ;
		    sleep 1
		    reps=$[$reps+1]
		done
            fi

            $ECHO "."
        fi
    fi

    # Start NFS daemons.

    if test $NETMODE = "inet" && $CHECK nfs; then

      # Mount any NFS file systems specified in /etc/fstab

      /bin/mount $VERBOSE -at nfs

      if $CHECK automount || $CHECK lockd; then

        $ECHO "NFS daemons:\c"

	# Linux NFS daemons are kernel-spawned threads. The only thing
	# to start is the automounter and lockd/statd, if so configured.

	# Start the automounter

        if $CHECK automount && test -x $AUTOMOUNT; then
            /usr/bin/killall -q -TERM automount
	    getmounts | sh 2> /dev/null
                                                    $ECHO " automount\c"
        fi

	# Start lockd/statd

        if $CHECK lockd; then
            if test -x /sbin/rpc.lockd; then
		/usr/bin/killall -q -TERM rpc.lockd
		/sbin/rpc.lockd `cat $CONFIG/lockd.options 2> /dev/null`
						    $ECHO " lockd\c"
            fi

	    if test -x /sbin/rpc.statd; then
		/usr/bin/killall -q -TERM rpc.statd
		/sbin/rpc.statd `cat $CONFIG/statd.options 2> /dev/null`
						    $ECHO " statd\c"
	    fi
        fi

        $ECHO "."

      fi
    fi

    # Start Internet daemons. Just about everything is spawned from inetd.

    $ECHO "Internet daemons:\c"

       /usr/bin/killall -q inetd

       # Start the Internet "super-server"
       if test -x /usr/sbin/inetd; then
           /usr/sbin/inetd `cat $CONFIG/inetd.options 2> /dev/null` < /dev/null
						    $ECHO " inetd\c"
       fi

    $ECHO "."
    ;;

'stop')
    $ECHO "Stopping network services...\c"

    #  Servers that might start shells.
    USERS='in.rlogind in.rexecd in.rshd in.ftpd in.telnetd in.sshd'

    #  The following servers should be killed immediately.
    EDAEMONS='inetd bootp tftpd automount rpc.mountd rpc.statd rpc.lockd
                rpc.bootparamd rpc.rexd rpc.rstatd rpc.rusersd
                rpc.rwalld snmpd'

    #  Remote daemons
    RDAEMONS='ypbind gated routed'

    #  These daemons should be kept alive until the last minute.
    LDAEMONS='portmap tcpd'

    #  Kill (probable) shell process groups first
    /usr/bin/killall -q -g $USERS
    /usr/bin/killall -q -TERM $EDAEMONS $USERS
    /usr/bin/killall -q -TERM $RDAEMONS
    /usr/bin/killall -q mount umount $USERS $EDAEMONS $RDAEMONS $LDAEMONS

    # Unmount all nfs filesystems
    for nfs_fs in `mount | grep nfs | cut -f 3 -d ' '`; do
	umount $nfs_fs
    done
    
    # Bring down all interfaces.
    for device in `ifconfig | grep ^[a-z] | cut -f1 -d ' '`; do
	$IFCONFIG $device down
    done

    $ECHO "done."
    ;;

*)
    echo "usage: $0 {start | stop}"
    ;;
esac
