#!/bin/sh -
#
# pccard_ether_remove interfacename
#
# example: pccard_ether_remove ep0
#
# $Id: pccard_ether_remove,v 1.3 1999/09/22 13:58:02 toshi Exp $
# Mar 9, 1996 by Hajimu UMEMOTO <ume@calm.imasy.or.jp>
# Mar 31, 1997 by Tatsumi Hosokawa <hosokawa@jp.FreeBSD.org>

# Suck in the configuration variables
if [ -f /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
elif [ -f /etc/rc.conf ]; then
	. /etc/rc.conf
fi

interface=$1

if expr "$pccard_ether" : ".*${interface}.*" > /dev/null; then
	eval ifconfig_args=\$ifconfig_${interface}
	if [ "x$ifconfig_args" = "xDHCP" ]; then
		# Stop the DHCP client for this interface
		if [ -s /var/run/dhclient.pid ]; then
			# for ISC-DHCP client program
			kill `cat /var/run/dhclient.pid`
			rm -f /var/run/dhclient.pid /etc/resolv.conf
		elif [ -s /var/run/dhcpc.$interface.pid ]; then
			# for WIDE-DHCP client program
			kill `cat /var/run/dhcpc.$interface.pid`
			rm -f /var/run/dhcpc.$interface.pid
		else
			# by way of precaution
			killarg=`ps xc | awk '$5 ~/^dhc(pc|lient)$/ {print $1}'`
			if [ -n "$killarg" ] ; then
				ps xc | grep -q dhclient && rm -f /etc/resolv.conf
				kill $killarg
			fi
		fi
	elif [ -n "$ifconfig_args" ]; then
		# Delelte static route if specified
		eval ifx_routes=\$static_routes_${interface}
		if [ -n "$ifx_routes" ]; then
			for i in ${ifx_routes}; do
				eval route_args=\$route_${i}
				route delete ${route_args}
			done
		fi
		# Delete aliases if exist
		alias=0
		while :
		do
			eval ifx_args=\$ifconfig_${interface}_alias${alias}
			if [ -n "$ifx_args" ]; then
				ifconfig $interface $ifx_args alias delete
				alias=`expr ${alias} + 1`
			else
				break;
			fi
		done
	fi

	# Remove the network interface and cleaning ARP table
	ifconfig $interface delete
	arp -d -a

	# Clean the routing table
	#  $route_flush==YES || $defaultrouter!=NO || $static_routes!=NULL
	if [ "$route_flush" = "YES" -o -n "${static_routes}" -o \
	     -n "$defaultrouter" -a "x$defaultrouter" != "xNO" ]; then
		route -n flush
	fi
fi