#!/bin/sh
# unplumb skip module on interface
# Argument 1: IP addr

#did you lock your NFS server out? This _may_ help...
PATH=/usr/ucb:/sbin:/bin:/usr/sbin
export PATH

# enough args?
if [ $# -lt 1 ] ; then
  echo "$0: Usage $0 <IP address | interface> [STREAMS device name]";
  exit 1;
fi

# find interface name
IFNAME=`netstat -ni | grep $1`;
if [ "$IFNAME" ] ; then
  IFNAME=`echo $IFNAME | awk '{ print $1 }'`
  IFADDR=`echo $IFNAME | awk '{ print $4 }'`
  if [ $# -lt 2 ] ; then
    IFDEV=`echo $IFNAME | sed -e "s/[0-9]//g"`
  else
    IFDEV=$2
  fi
else
  echo "$0: interface with address $1 not found."
  exit 1
fi

# parse interface configuration
CONF=`ifconfig $IFNAME`;

# trailer packet allowed?
if [ "`echo $CONF | grep NOTRAILERS`" ] ; then
  TRAILER="-trailers"
else
  TRAILER="trailers"
fi

# arp enabled?
if [ "`echo $CONF | grep NOARP`" ] ; then
  ARP="-arp"
else
  ARP="arp";
fi

# is interface up or down?
if [ "`echo $CONF | grep UP`" ] ; then
  UP="up"
else
  UP="down"
fi

# is there a multicast route over the interface?
if [ "`netstat -nr | grep 224.0.0.0 | grep $IFNAME`" ] ; then
  ROUTE="yes"
else
  ROUTE="no"
fi

# LAN or PPP?
if [ "`echo $CONF | grep POINTOPOINT`" ] ; then
  # PPP interface, parse addr, dstaddr, netmask
  ADDR=`ifconfig $IFNAME | grep inet | sed -e "s/-->//g"`;
  exit 2
else
  # LAN interface, parse addr, netmask, broadcast
  ADDR=`ifconfig $IFNAME | grep inet`
fi
# prepend 0x to netmask
ADDR=`echo $ADDR | sed -e "s/ff/0xff/1"`

# remove autopush info
MAJOR=`grep "^$IFDEV " /etc/name_to_major | awk '{ print $2 }'`

autopush -r -M $MAJOR -m 0 > /dev/null 2>&1

/sbin/ifconfig $IFNAME unplumb
/sbin/ifconfig $IFNAME plumb
/sbin/ifconfig $IFNAME $ADDR $ARP $TRAILER $UP

if [ "$ROUTE" = "yes" ] ; then
  route add net 224.0.0.0 $IFADDR 0 > /dev/null 2>&1
fi
