#! /bin/sh
#
# network 1.32 1997/05/13 02:16:47 (David Hinds)
#
# modified 1997/12/23 13:01 (G D Rijhwani)
#
# 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
export display

test $display && \
    echo "=====================================================================" > $display

# 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"

. $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
	/sbin/ifconfig $DEVICE up $IPADDR netmask $NETMASK \
	    ${BROADCAST:+broadcast $BROADCAST} > $display
        test "$display" && \
            echo "Ifconfig: $IPADDR/$NETMASK -> $DEVICE" > $display

	if [ "$NETWORK" != "" ] ; then
	    /sbin/route add -net $NETWORK netmask $NETMASK dev $DEVICE > $display
            test "$display" && \
                echo "Route: $NETWORK/$NETMASK -> $DEVICE" > $display
	elif [ "$GATEWAY" != "" ] ; then
            for gw in $GATEWAY; do
	        /sbin/route add $gw $DEVICE
                test "$display" && \
                    echo "Route: $gw -> $DEVICE" > $display
            done
	fi

	if [ "$GATEWAY" != "" ]; then
            for gw in $GATEWAY; do
                /sbin/route add default gw $gw metric $METRIC
                test "$display" && \
                    echo "Route: default -> $gw (metric $METRIC)" > $display
            done
        else
            test "$display" && \
                echo "No gateways" > $display
        fi

	if [ "$HOSTSFILE" != "" ]; then
            if [ -L /etc/hosts ]; then
                rm -f /etc/hosts
            else
                mv /etc/hosts /etc/hosts.default
            fi
            if [ ! -f $HOSTSFILE ]; then
                echo "# Auto hostsfile (`basename $HOSTSFILE`), created `date`" > $HOSTSFILE
                echo "127.0.0.1		localhost" >> $HOSTSFILE
                echo "# End hosts" >> $HOSTSFILE
                echo "$HOSTSFILE created, with localhost default"
            fi
            ln -s $HOSTSFILE /etc/hosts
            test "$display" && \
                echo "Set $HOSTSFILE as hosts file" > $display
        fi

	# Update DNS stuff
	grep -svx "domain $DOMAIN" /etc/resolv.conf | \
            sed -e "/# PCMCIA $DEVICE begin/,/# PCMCIA $DEVICE end/d" > /etc/resolv.new

	echo "# PCMCIA $DEVICE begin" >> /etc/resolv.new
        if [ "$DOMAIN" != "" ]; then
	    echo "domain $DOMAIN" >> /etc/resolv.new
            test $display && \
                echo "Domain $DOMAIN added to resolver config" > $display
        fi
	if [ "$DNS_1$DNS_2$DNS_3" != "" ] ; then
	    for DNS in $DNSSRVS $DNS_1 $DNS_2 $DNS_3 ; do
                if ! grep "nameserver $DNS" resolv.new; then
		    echo "nameserver $DNS" >> /etc/resolv.new
                    test $display && \
                        echo "Added nameserver $DNS to resolver config" > $display
                fi
	    done
	fi
	echo "# PCMCIA $DEVICE end" >> /etc/resolv.new
	mv /etc/resolv.new /etc/resolv.conf

	if [ "$NETWORK" != "" ] ; then
            ipfwadm -F -p deny
            ipfwadm -F -a accept -m -P tcp -S $NETWORK/$NETMASK
            ipfwadm -F -a accept -m -P udp -S $NETWORK/$NETMASK
            test "$display" && \
            	( echo "Masquerading rules"
            	ipfwadm -F -l ) > $display
        fi

        if [ "$PPPGATE" != "" ]; then
           for ppphost in $PPPHOSTS; do
               /sbin/route add -host $ppphost gw $PPPGATE
               test $display && \
                   echo "Added route to $ppphost via $PPPGATE" > $display
           done
        fi

	# 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

    is_func start_fn && start_fn $DEVICE
    ;;

'stop')

    is_func 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 "/# PCMCIA $DEVICE begin/,/# PCMCIA $DEVICE end/d" \
	    < /etc/resolv.conf > /etc/resolv.new && \
	    mv /etc/resolv.new /etc/resolv.conf
        test "$display" && \
            echo "Removed resolver config additions" > $display

        # Remove masquerading
	if [ "$NETWORK" != "" ] ; then
            ipfwadm -F -p deny
            ipfwadm -F -d accept -m -P tcp -S $NETWORK/$NETMASK
            ipfwadm -F -d accept -m -P udp -S $NETWORK/$NETMASK
            test "$display" && \
            	( echo "Masquerading rules"
            	ipfwadm -F -l ) > $display
        fi

	if [ "$HOSTSFILE" != "" ]; then
            if [ -L /etc/hosts ]; then
                rm -f /etc/hosts
            else
                mv /etc/hosts /etc/hosts.default
            fi
            ln -s /etc/hosts.default /etc/hosts
            test "$display" && \
                echo "Set default hosts file" > $display
        fi

    fi
    /sbin/ifconfig $DEVICE down
    ;;

'check')
    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 -v '127.0.0.1|FIN_WAIT|TIME_WAIT|CLOSE' > /dev/null && exit 1
    ;;

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

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

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

if [ "$display" != "" ]; then
    echo "---------------------------------------------------------------------" > $display
    unset display; export display
fi

exit 0
