#!/bin/sh
#	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#
#	Copyright (c) 1996, Sun Microsystems Inc. All rights reserved.
#
# Get the list of network interfaces to configure by breaking 
# /etc/hostname.* into separate args by using "." as a shell separator 
# character, then step through args and ifconfig every other arg.

#ident	"$Header: /net/projects/S2.6/cvs/os_net/src_ws/usr/src/tadcmd/locations/init.d/inet,v 1.2 1997/12/05 13:42:49 ang Exp $"
#
#LocINFO  Network Configuration
#

case "$1" in
'start')
    cp $LOCDB/etc/hostname.??*[0-9] $LOCDB/etc/dhcp.??*[0-9] /etc 2>/dev/null
    cp $LOCDB/etc/inet/netmasks /etc/inet

    # Set the netmask along the way using local "/etc/netmasks" file.
    # This also sets up the streams plumbing for the interface.
    # With an empty /etc/hostname.* file this only sets up the streams plumbing
    # allowing the ifconfig auto-dhcp / auto-revarp command will attempt to
    # set the address.
    #
    interface_names="`echo /etc/hostname.*[0-9]             2>/dev/null`"
    if test "$interface_names" != "/etc/hostname.*[0-9]"
    then
        (
	    echo "configuring network interfaces:\c"
            IFS="$IFS."
            set `echo /etc/hostname\.*[0-9]`
            while test $# -ge 2
            do
                shift
                if [ "$1" != "xx0" ]
		then
		    addr=`cat /etc/hostname\.$1`
		    /sbin/ifconfig $1 plumb
		    if test -n "$addr"
		    then
			/sbin/ifconfig $1 inet "$addr" netmask + \
						broadcast + -trailers up \
						2>&1 > /dev/null
		    fi
		    echo " $1\c"
                fi
                shift
            done
	    echo "."
        )
    fi

    interface_names="`echo /etc/dhcp.*[0-9]         2>/dev/null`"
    if [ "$interface_names" != "/etc/dhcp.*[0-9]" ]
    then
	#
	# Allow configuration to be interrupted.
	# first find the primary interface. Default to the first interface
	# if not specified. First primary interface found "wins"
	# Run thru the list of interfaces again, this time trying DHCP.
	#
        (
	    firstif=""
	    primary=""
	    IFS="$IFS."
	    set `echo /etc/dhcp\.*[0-9]`
	    while [ ${#} -ge 2 ]
	    do
		shift
		if [ -z "${firstif}" ]
		then
		    firstif=$1
		fi
		for i in `cat /etc/dhcp\.$1`
		do
		    if [ "$i" = "primary" ]
		    then
			primary=$1
			break
		    fi
		done
		if [ ! -z "${primary}" ]
		then
		    break
		fi
		shift
	    done
	    if [ -z "${primary}" ]
	    then
		primary="${firstif}"
	    fi
	    cmdline=`cat /etc/dhcp\.${primary}`
	    echo "starting DHCP on primary interface ${primary}"
	    trap "" 2 3
	    /sbin/ifconfig ${primary} auto-dhcp primary $cmdline
	    trap 2 3
	    set `echo /etc/dhcp\.*[0-9]`
	    while [ ${#} -ge 2 ]
	    do
		shift
		cmdline=`cat /etc/dhcp\.$1`
		if [ "${1}" != "${primary}" ]
		then
		    echo "starting DHCP on interface ${1}"
		    /sbin/ifconfig $1 dhcp start wait 0 $cmdline
		fi
		shift
	    done
	)
    fi

    #
    # configure the rest of the interfaces automatically, quietly.
    #
    /sbin/ifconfig -adD auto-revarp netmask + broadcast + -trailers up \
	2>&1 >/dev/null

    ;;

'stop')
    interface_names="`echo /etc/hostname.*[0-9]             2>/dev/null`"
    if test "$interface_names" != "/etc/hostname.*[0-9]"
    then
        (
	    echo "Unconfiguring network interfaces:\c"
            IFS="$IFS."
            set `echo /etc/hostname\.*[0-9]`
            while test $# -ge 2
            do
                shift
                if [ "$1" != "xx0" ]
		then
		    /sbin/ifconfig $1 unplumb
		    echo " $1\c"
                fi
                shift
            done
	    echo "."
        )
    fi
    rm -f /etc/hostname.??*[0-9] /etc/dhcp.??*[0-9] /etc/inet/netmasks
    ;;

'create')
    echo "/etc/inet\n/etc/inet/netmasks" | cpio -pdmu $LOCDB 2>/dev/null
    N=`ls /etc/hostname.??*[0-9] /etc/dhcp.??*[0-9] 2>/dev/null`
    if [ -z "$N" ]
    then
	exit 1
    else
	cp -p $N $LOCDB/etc
    fi
    ;;

*)
    echo "Usage: $0 { start | stop | create }" 1>&2
    ;;
esac

exit 0
