#! /bin/sh
#
# PAN setup
#

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

# Get device attributes
get_info $DEVICE

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

match () { case $1 in $2) return 0; ;; *) return 1; ;; esac ; }

RESOLV=/etc/resolv.conf

bootp_setup ()
{
    if match `uname -r` "2.[2-9].*" ; then
	/sbin/ifconfig $DEVICE up
    else
	/sbin/ifconfig $DEVICE up 0.0.0.0
	/sbin/route add default dev $DEVICE netmask 0.0.0.0
    fi
}

start ()
{
    test "$IF_PORT" && /sbin/ifport $DEVICE $IF_PORT

    if is_true $PUMP ; then
	/sbin/pump -i $DEVICE
    elif is_true $BOOTP ; then
	bootp_setup
	if [ -x /sbin/bootpc ] ; then
	    eval `/sbin/bootpc --bootfile '' --returniffail \
		--timeoutwait 10 --dev $DEVICE`
	    /sbin/ifconfig $DEVICE down
	    if [ "$GATEWAYS" ] ; then
		set - $GATEWAYS ; GATEWAY=$1
	    fi
	elif [ -x /sbin/pump ] ; then
	    /sbin/pump -i $DEVICE
	else
	    exit 1
	fi
    elif is_true $DHCP ; then
	bootp_setup
	HN=${DHCP_HOSTNAME:+-h $DHCP_HOSTNAME}
	TO=${DHCP_TIMEOUT:+-t $DHCP_TIMEOUT}
	if [ -x /sbin/dhcpcd ] ; then
	    # This is a version check: I know it looks weird
	    if /sbin/dhcpcd -XYZZY 2>&1 | grep -q DHCP ; then
		/sbin/dhcpcd $HN $TO $DEVICE >/dev/null 2>&1 || exit 1
	    else
		# Jump through hoops for lame 0.70-era dhcpcd
		L=/var/run/dhcp-lock-$DEVICE
		/bin/echo "#!/bin/sh\nrm $L" > $L ; chmod +x $L
		/sbin/dhcpcd $HN -c $L $DEVICE >/dev/null 2>&1
		for t in 0 1 2 3 4 5 6 7 8 9 ; do
		    sleep 2 ; if [ ! -e $L ] ; then break ; fi
		done
		rm -f $L
		if [ -e /etc/dhcpc/resolv.conf ] ; then
		    echo "# $DEVICE begin" > $RESOLV.N
		    cat /etc/dhcpc/resolv.conf >> $RESOLV.N
		    echo "# $DEVICE end" >> $RESOLV.N
		    cat $RESOLV >> $RESOLV.N ; mv $RESOLV.N $RESOLV
		fi
	    fi
	elif [ -x /sbin/dhclient ] ; then
	    /sbin/dhclient $DEVICE >/dev/null 2>&1 || exit 1
	elif [ -x /sbin/pump ] ; then
	    /sbin/pump $HN -i $DEVICE
	else
	    exit 1
	fi
    fi

    if [ "$IPADDR" ] ; then
	# Basic network setup
	NM=${NETMASK:+netmask $NETMASK}
	BC=${BROADCAST:+broadcast $BROADCAST}
	MTU=${MTU:+mtu $MTU}
	/sbin/ifconfig $DEVICE up $IPADDR $NM $BC $MTU
	if [ "$NETWORK" ] ; then
	    /sbin/ifuser $DEVICE $NETWORK || \
		/sbin/route add -net $NETWORK $NM dev $DEVICE
	elif [ "$GATEWAY" ] ; then
	    /sbin/ifuser $DEVICE $GATEWAY || \
		/sbin/route add $GATEWAY $DEVICE
	fi
	test "$GATEWAY" && /sbin/route add default gw $GATEWAY metric 1
    fi
    
    # Update DNS stuff
    if [ "$DOMAIN$SEARCH$DNSSRVS$DNS_1$DNS_2$DNS_3" ] ; then
	echo "# $DEVICE begin" > $RESOLV.N
	test "$DOMAIN" && echo "domain $DOMAIN" >> $RESOLV.N
	test "$SEARCH" && echo "search $SEARCH" >> $RESOLV.N
	for DNS in $DNSSRVS $DNS_1 $DNS_2 $DNS_3 ; do
	    echo "nameserver $DNS" >> $RESOLV.N
	done
	echo "# $DEVICE end" >> $RESOLV.N
	sed -e "/# $DEVICE begin/,/# $DEVICE end/d" $RESOLV >> $RESOLV.N
	mv $RESOLV.N $RESOLV
    fi

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

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

    start_fn $DEVICE
}

stop ()
{
    stop_fn $DEVICE

    if is_true $PUMP || is_true $BOOTP || is_true $DHCP || [ "$IPADDR" ] ; then

	# Shut down all NFS mounts on this interface
	nfsstop ()
	{
	    local HOST MT
	    if read HOST MT ; then
		nfsstop
		if /sbin/ifuser $DEVICE $HOST ; then
		    do_fuser -k -m $MT > /dev/null
		    umount -v $MT
		fi
	    fi
	}
	mount -t nfs | grep "type nfs" | sed -e 's/:.* on \(.*\) type .*/ \1/' | nfsstop

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

	# Remove nameservers
	if fgrep -q "# $DEVICE begin" $RESOLV ; then
	    sed -e "/# $DEVICE begin/,/# $DEVICE end/d"	$RESOLV > $RESOLV.N
	    mv $RESOLV.N $RESOLV
	fi

	if is_true $PUMP ; then
	    pump -r -i $DEVICE
	elif is_true $DHCP ; then
	    if [ -x /sbin/dhcpcd ] ; then
		kill -TERM `cat /var/run/dhcpcd-$DEVICE.pid`
		sleep 2
		/sbin/dhcpcd -XYZZY 2>&1 | grep -q DHCP || \
		    rm -f /var/run/dhcpcd-$DEVICE.pid
	    elif [ -x /sbin/dhclient ] ; then
		kill -TERM `cat /var/run/dhclient.pid`
	    elif [ -x /sbin/pump ] ; then
		/sbin/pump -r -i $DEVICE
	    fi
	fi
    fi
    /sbin/ifconfig $DEVICE down
}

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
}


case "$ACTION" in

'start')

    if [ "$NAP" = "" ]; then
	echo "NAP not defined!"
	exit 1;
    fi
        
    modprobe bnep
    /usr/bin/pand --role PANU --service NAP --connect $NAP --nodetach
    
    # find the name of our new bnepX device
    CONS=`/usr/bin/pand -l`
    if [ "$CON" = "" ]; then
	echo "No PAN connections created!"
	exit 1;
    fi
    
    DEVICE=`echo "$CONS" | grep $NAP | awk '{print $1}'`
    
    if [ "$DEVICE" = "" ]; then
	echo "No BNEP device found!"
	exit 1;
    fi
    
#    /sbin/ifconfig $DEVICE up
    
    # setup IP
    start
    
    ;;

'stop')

    # find the name of our new bnepX device
    CONS=`/usr/bin/pand -l`
    if [ "$CON" = "" ]; then
	echo "No PAN connections created!"
	exit 1;
    fi
    
    DEVICE=`echo "$CONS" | grep $NAP | awk '{print $1}'`
    
    if [ "$DEVICE" = "" ]; then
	echo "No BNEP device found!"
	exit 1;
    fi
    
    stop
    
    /usr/bin/pand -k $NAP
    
    if [ "`/usr/bin/pand -l`" = "" ]; then
	rmmod bnep
    fi

    ;;

'cksum')
    ;;

'restart')
    restart
    
    ;;

'suspend'|'resume')
    ;;

*)
    usage
    ;;

esac

exit 0

