#! /bin/sh
#
# network 1.75 2000/07/07 23:37:10 (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.
#

if [ -r ./shared ] ; then . ./shared ; else . /etc/pcmcia/shared ; fi
if [ -r ./network.functions ] ; then . ./network.functions ; else . /etc/pcmcia/network.functions ; fi

# except two slot card
# 2003/10/01 matty
scan_stab()
{
  local DEV0 CLASSLOCAL CLASS0
  local SOCKET CLASS DRIVER INSTANCE DEVS MAJOR MINOR
  #DEV0=${2}0
  CLASS=""
  CLASS0=""
  CLASSLOCAL=""
  while read SOCKET CLASS DRIVER INSTANCE DEVS MAJOR MINOR;
  do
	if [ "$SOCKET" = "0" ] ; then DEV0=$DEVS ; fi
    if [ "$DEV0" = "$DEVS" ] ; then CLASS0=$CLASS ; fi
    if [ "$1" = "$DEVS" ] ; then CLASSLOCAL=$CLASS ; fi
  done
  if [ -n "$CLASSLOCAL" ] ; then
    # echo $CLASSLOCAL $CLASS0 $1 $DEV0
    if [ "$CLASSLOCAL" = "$CLASS0" -a "$1" != "$DEV0" ] ; then
      return 1
    else
      return 0
    fi
  fi
  return 1
}

# Get device attributes
get_info $DEVICE
if [ "`echo $SCHEME | cut -d: -f1`" != "CardSuspend" ] ; then
    DEVNAME=`echo $DEVICE | sed -e 's/[0-9]//g'`
    scan_stab $DEVICE $DEVNAME < "/var/lib/pcmcia/stab" || exit 0
fi
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

# Now, run the specific script for Wireless LAN interfaces
# Note : we need the wireless parameters to be set up before IP parameters,
# so that we can perform DHCP over the Wireless link if needed. Jean II
if [ -x ./wireless ] ; then
    . ./wireless
else
    . /etc/pcmcia/wireless
fi

# Suspend/Resume for no working.
if [ "`echo $SCHEME | cut -d: -f1`" = "CardResume" ] ; then
    case "$ACTION" in ('start')
	/sbin/cardctl resume $SOCKET
	/sbin/ifconfig $DEVICE 0.0.0.0
	/sbin/ifconfig $DEVICE down
    ;; 'cksum') exit 1; ;; esac
    exit 0
fi
if [ "`echo $SCHEME | cut -d: -f1`" = "CardSuspend" -o "$CARDSUSPEND" = "y" ] ; then
    case "$ACTION" in ('start'|'resume')
	/sbin/ifconfig $DEVICE 0.0.0.0
	/sbin/ifconfig $DEVICE down
	/sbin/cardctl suspend $SOCKET
    ;; 'cksum') exit 1; ;; esac
    exit 0
fi

case "$ACTION" in

'start')
    ifup
    start_fn $DEVICE
    ;;

'stop')

    stop_fn $DEVICE
    ifdown
    ;;

'check')
    is_true $NO_CHECK && exit 0
    /sbin/ifconfig $DEVICE | grep -q RUNNING || exit 0

    # Check for any in-use NFS mounts
    nfscheck ()
    {
	while read HOST MT ; do
	    /sbin/ifuser $DEVICE $HOST && do_fuser -sm $MT && exit 1
	done
    }
    mount -t nfs | grep "type nfs" | sed -e 's/:.* on \(.*\) type .*/ \1/' | nfscheck

    # Check for active TCP or UDP connections
    getdests ()
    {
    	IFS=" :" ; read A ; read A
	while read A B C D E HOST PORT STATE ; do
	    if [ "$STATE" != "FIN_WAIT1" -a "$STATE" != "FIN_WAIT2" \
		-a "$STATE" != "CLOSE_WAIT" -a "$STATE" != "TIME_WAIT" \
		-a \( "$STATE" != "LAST_ACK" || "$C" -ne 1 \) \
		-a "$HOST" != "127.0.0.1" -a "$HOST" != "0.0.0.0" \
		-a "$STATE" != "CLOSE" -a "$PORT" != "*" ] ; then
		echo $HOST
	    fi
	done
    }
    DESTS=`netstat -ntuw | getdests`
    /sbin/ifuser $DEVICE $DESTS && exit 1
    ;;

'cksum')
    if [ $WIRELESS = 1 ] ; then exit 1 ; fi
    chk_simple "$NEW_SCHEME,$SOCKET,$INSTANCE,$HWADDR" || exit 1
    ;;

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

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0
