#! /bin/sh
#
# network 1.35 1998/02/13 23:41:04 (David Hinds)
#
# Initialize or shutdown a PCMCIA ethernet adapter
#
# This script should be invoked with two arguments.  The first is the
# action to be taken, either "start", "stop", or "restart".  The
# second is the network interface name.
#
# The script passes an extended device address to 'network.opts' in
# the ADDRESS variable, to retrieve device-specific configuration
# options.  The address format is "scheme,socket,instance,hwaddr"
# where "scheme" is the current PCMCIA device configuration scheme,
# "socket" is the socket number, "instance" is used to number multiple
# interfaces in a single socket, and "hwaddr" is the card's hardware
# ethernet address.
#

. ./shared

# Get device attributes
get_info $DEVICE
HWADDR=`/sbin/ifconfig $DEVICE | sed -n -e 's/.*addr \(.*\)/\1/p'`

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE,$HWADDR"
start_fn () { return; }
stop_fn () { return; }
. $0.opts

case "$ACTION" in

'start')

    test "$IF_PORT" && /sbin/ifport $DEVICE $IF_PORT

    if is_true $BOOTP ; then
	/sbin/ifconfig $DEVICE up
	/sbin/route add default dev $DEVICE
	eval `/sbin/bootpc --bootfile '' --dev $DEVICE`
	/sbin/route del default
	if [ "$GATEWAYS" ] ; then
	    set - $GATEWAYS ; GATEWAY=$1
	fi
    fi

    if [ "$IPADDR" != "" ] ; then

	# Basic network setup
	BC=${BROADCAST:+broadcast $BROADCAST}
	/sbin/ifconfig $DEVICE up $IPADDR netmask $NETMASK $BC

	if [ "$NETWORK" != "" ] ; then
	    /sbin/route add -net $NETWORK netmask $NETMASK dev $DEVICE
	elif [ "$GATEWAY" != "" ] ; then
	    /sbin/route add $GATEWAY $DEVICE
	fi

	test "$GATEWAY" && /sbin/route add default gw $GATEWAY metric 1

	# Update DNS stuff
	cp /etc/resolv.conf /etc/resolv.new
	echo "# $DEVICE begin" >> /etc/resolv.new
	test "$DOMAIN" && echo "domain $DOMAIN" >> /etc/resolv.new
	if [ "$DNSSRVS $DNS_1 $DNS_2 $DNS_3" != "   " ] ; then
	    for DNS in $DNSSRVS $DNS_1 $DNS_2 $DNS_3 ; do
		echo "nameserver $DNS" >> /etc/resolv.new
	    done
	fi
	echo "# $DEVICE end" >> /etc/resolv.new
	mv /etc/resolv.new /etc/resolv.conf

	# Handle NFS mounts
	if [ "$MOUNTS" != "" ] ; then
	    for MT in $MOUNTS ; do mount -v $MT ; done
	fi

    fi

    if [ "$IPX_NETNUM" != "" ] ; then
	ipx_interface add $DEVICE $IPX_FRAME $IPX_NETNUM
    fi

    start_fn $DEVICE
    ;;

'stop')

    stop_fn && stop_fn $DEVICE
    
    if is_true $BOOTP || [ "$IPADDR" != "" ] ; then

	# Shut down NFS mounts
	if [ "$MOUNTS" != "" ] ; then
	    for MT in $MOUNTS ; do
		if mount | fgrep " on $MT type" ; then
		    fuser -s -k -m $MT
		    umount -v $MT
		fi
	    done
	fi

	test "$IPX_NETNUM" && ipx_interface del $DEVICE $IPX_FRAME

	# Remove nameservers
	sed -e "/# $DEVICE begin/,/# $DEVICE end/d"	\
	    < /etc/resolv.conf > /etc/resolv.new &&	\
	    mv /etc/resolv.new /etc/resolv.conf

    fi
    /sbin/ifconfig $DEVICE down
    ;;

'check')
    /sbin/ifconfig $DEVICE | grep -q RUNNING || exit 0
    if [ "$MOUNTS" != "" ] ; then
	for MT in $MOUNTS ; do
	    if mount | fgrep " on $MT type" ; then
		fuser -s -m $MT && exit 1
	    fi
	done
    fi
    # This is crude -- it checks for any external network activity
    netstat -ntuw | tail +3 | awk '{ print $5, $6 }' | \
	egrep -qv '127.0.0.1|FIN_WAIT|TIME_WAIT|CLOSE' && exit 1
    ;;

'cksum')
    chk_simple "$3,$SOCKET,$INSTANCE,$HWADDR" || exit 1
    ;;

'restart')
    test "$IPADDR" && /sbin/ifconfig $DEVICE down up
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
