#! /bin/sh
#
# network 1.26 1996/11/27 03:44:44 (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.
#

usage()
{
    echo "usage: network [action] [device name]"
    echo "  actions: start check stop suspend resume"
    exit 1
}

if [ $# -lt 2 ] ; then usage ; fi
ACTION=$1 ; DEVICE=$2

# Get device attributes
INFO=`fgrep "	$DEVICE" /var/run/stab` || usage
set -- $INFO
SOCKET=$1 ; INSTANCE=$4
HWADDR=`/sbin/ifconfig $DEVICE | sed -n -e 's/.*addr \(.*\)/\1/p'`
if [ -s /var/run/pcmcia-scheme ] ; then
    SCHEME=`cat /var/run/pcmcia-scheme`
else
    SCHEME="default"
fi

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE,$HWADDR"
. /etc/pcmcia/network.opts

case "$ACTION" in

'start')

    if [ "$IF_PORT" != "" ] ; then
	/sbin/ifport $DEVICE $IF_PORT
    fi

    if [ "$BOOTP" = "y" -o "$BOOTP" = "Y" ] ; 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
	if [ "$BROADCAST" != "" ] ; then
	    /sbin/ifconfig $DEVICE up $IPADDR broadcast $BROADCAST \
		netmask $NETMASK
	else
	    /sbin/ifconfig $DEVICE up $IPADDR netmask $NETMASK
	fi

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

	if [ "$GATEWAY" != "" ] ; then
	    /sbin/route add default gw $GATEWAY metric 1
	fi

	# Update DNS stuff
	cp /etc/resolv.conf /etc/resolv.new
	echo "# $DEVICE begin" >> /etc/resolv.new
	if [ "$DOMAIN" != "" ] ; then
	    echo "domain $DOMAIN" >> /etc/resolv.new
	fi
	if [ "$DOMAIN $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
    ;;

'stop')

    if [ "$BOOTP" = "y" -o "$IPADDR" != "" ] ; then

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

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

	/sbin/ifconfig $DEVICE down

	# Remove nameservers
	awk "/# $DEVICE begin/ { p = 1 }		\
	    { if (!p) print }				\
	    /# $DEVICE end/ { p = 0 }"			\
	    < /etc/resolv.conf > /etc/resolv.new &&	\
	    mv /etc/resolv.new /etc/resolv.conf

    fi
    ;;

'check')
    if [ "$MOUNTS" != "" ] ; then
	for MT in $MOUNTS ; do
	    fuser -s -m $MT && exit 1
	done
    fi
    # This is crude -- it checks for any external network activity
    netstat -ntuw | tail +3 | awk '{ print $5, $6 }' | \
	egrep -v '127.0.0.1|TIME_WAIT|CLOSE' > /dev/null && exit 1
    ;;

'restart')
    if [ "$IPADDR" != "" ] ; then
	/sbin/ifconfig $DEVICE down up
    fi
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
