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

# Source common library
. /etc/init.d/functions

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 ${CONFDIR}/automount.options 2> ${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> ${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 ${CONFDIR}/automount.options 2> ${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 ${CONFDIR}/ifconfig-$3.options 2> ${NULL}` \
	2> ${NULL}
}

#
# nfsroot
#
# Determine whether or not this system is running an NFS root
# file system.
#
nfsroot()
{
    grep ' / nfs' /proc/mounts > ${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"

	# Configure the loopback interface and program the IP multicast
	# default route.

	$IFCONFIG lo $LOCALHOST netmask 255.255.0.0

    # Configure, if necessary by switching on the numeric portion of
    # the platform ID, the internal system network such that internal
    # services can be started earlier. Arguably, this is a non-ideal
    # place for this code. At some future point, this should be moved
    # out to a platform-specific script (yet ideally generically
    # named, e.g. /etc/init.d/network.internal or /etc/init.d/
    # network.platform) that this one calls.

    SWBD=`sin | swbd 2> ${NULL}`

    case ${SWBD##SWBD} in
	'36' | '50')
	    # Configure eth3 for Marathon/SCIMITAR (connected to Hifn).
	
	    $IFCONFIG eth3 10.0.0.1 \
		netmask 255.255.255.0 > ${NULL} 2>&1
	    ;;

	*)
	    ;;
    esac

	$ECHO "Network daemons:\c"

	${KILLALL} portmap

	# Start the RPC program number mapper (portmap)

	if test -x /sbin/portmap; then
		/sbin/portmap `cat ${CONFDIR}/portmap.options 2> ${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> ${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
            ${KILLALL} 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 ${CONFDIR}/ypbind.options 2> ${NULL}` &
                                                    $ECHO " ypbind\c"

		# wait till ypbind running or time out
		reps=0
		until ypwhich > ${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
            ${KILLALL} -TERM automount
	    getmounts | sh 2> ${NULL}
                                                    $ECHO " automount\c"
        fi

	# Start lockd/statd

        if $CHECK lockd; then
            if test -x /sbin/rpc.lockd; then
		${KILLALL} -TERM rpc.lockd
		/sbin/rpc.lockd `cat ${CONFDIR}/lockd.options 2> ${NULL}`
						    $ECHO " lockd\c"
            fi

	    if test -x /sbin/rpc.statd; then
		${KILLALL} -TERM rpc.statd
		/sbin/rpc.statd `cat ${CONFDIR}/statd.options 2> ${NULL}`
						    $ECHO " statd\c"
	    fi
        fi

        $ECHO "."

      fi
    fi

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

    $ECHO "Internet daemons:\c"

       ${KILLALL} inetd

       # Start the Internet "super-server"
       if test -x /usr/sbin/inetd; then
           /usr/sbin/inetd `cat ${CONFDIR}/inetd.options 2> ${NULL}` < ${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
    ${KILLALL} -g $USERS
    ${KILLALL} -TERM $EDAEMONS $USERS
    ${KILLALL} -TERM $RDAEMONS
    ${KILLALL} 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
