#!/bin/sh
# $NetBSD: script2,v 1.2 2001/07/12 19:08:05 garbled Exp $

if [ "$1" = "4" ]; then
	IPV4=`ifconfig $2 | grep 'inet ' | \
	     sed -e 's/inet \([0-9\.]*\) net.*/\1/'`
	if [ -z "$IPV4" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			IPV4=`cat /etc/ifconfig.$2 | \
			     sed -e 's/[a-zA-z ]*\([0-9\.]*\) .*/\1/'`
		else
			IPV4=" "
		fi
	fi
	echo $IPV4
	exit 0
fi
if [ "$1" = "n" ]; then
	NETMASK=`ifconfig $2 | grep 'inet ' | \
	     sed -e 's/.*netmask \([0-9a-fx\.]*\) broad.*/\1/'`
	if [ -z "$NETMASK" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			NETMASK=`cat /etc/ifconfig.$2 | grep netmask | \
			     sed -e 's/.*netmask *\([0-9a-fx\.]*\).*/\1/'`
		else
			NETMASK=" "
		fi
	fi
	echo $NETMASK
	exit 0
fi
if [ "$1" = "b" ]; then
	BROAD=`ifconfig $2 | grep 'inet ' | \
	     sed -e 's/.*broadcast \([0-9a-fx\.]*\)/\1/'`
	if [ -z "$BROAD" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			BROAD=`cat /etc/ifconfig.$2 | grep broadcast | \
			     sed -e 's/.*broadcast *\([0-9a-fx\.]*\).*/\1/'`
		else
			BROAD=" "
		fi
	fi
	echo $BROAD
	exit 0
fi
if [ "$1" = "m" ]; then
	MEDIA=`ifconfig $2 | grep 'media: ' | \
	     sed -e 's/.*media: Ethernet \([0-9a-zA-z]*\) .*/\1/'`
	if [ -z "$MEDIA" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			MEDIA=`cat /etc/ifconfig.$2 | grep media | \
			     sed -e 's/.*media *\([0-9a-zA-z]*\) .*/\1/'`
		fi
	fi
	if [ -z $MEDIA ]; then
		ifconfig -m $2 | grep 'media ' | awk '{print $2}' | sort | uniq
	else
		echo $MEDIA
		ifconfig -m $2 | grep -v $MEDIA | grep 'media ' | \
		    awk '{print $2}' | sort | uniq		
	fi
	exit 0
fi
if [ "$1" = "o" ]; then
	OPT=`ifconfig -m $2 | grep 'media: ' | awk '{print $4}'`
	if [ -z "$OPT" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			OPT=`cat /etc/ifconfig.$2 | grep mediaopt | \
			    sed -e 's/.*mediaopt *\([a-zA-Z0-9-]*\) *.*/\1/'`
		fi
	fi
	if [ -z "$OPT" ]; then
		echo "none"
		ifconfig -m $2 | grep 'mediaopt ' | awk '{print $4}' | \
		    sort | uniq
	else
		echo $OPT
		ifconfig -m $2 | grep -v $OPT | grep 'mediaopt ' | \
		    awk '{print $4}' | sort | uniq
		echo "none"
	fi
	exit 0
fi
if [ "$1" = "met" ]; then
	MET=`ifconfig $2 | grep 'metric ' | sed -e 's/.*metric \([0-9]*\) .*/\1/'`
	if [ -z "$MET" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			MET=`cat /etc/ifconfig.$2 | grep metric | \
			     sed -e 's/.*metric *\([0-9]*\).*/\1/'`
		else
			MET="0"
		fi
	if [ -z "$MET" ]; then
		MET="0"
	fi
	fi
	echo $MET
	exit 0
fi
if [ "$1" = "mtu" ]; then
	MTU=`ifconfig $2 | grep 'mtu ' | sed -e 's/.*mtu \([0-9]*\)/\1/'`
	if [ -z "$MTU" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			MTU=`cat /etc/ifconfig.$2 | grep mtu | \
			     sed -e 's/.*mtu *\([0-9]*\).*/\1/'`
		else
			MTU="1500"
		fi
	fi
	echo $MTU
	exit 0
fi
if [ "$1" = "6" ]; then
	IPV6=`ifconfig $2 | grep 'inet6 ' | \
	     sed -e 's/inet6 \([0-9:a-z%]*\) pre.*/\1/'`
	if [ -z "$IPV6" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			IPV6=`cat /etc/ifconfig.$2 | grep 'inet6 ' | \
			     sed -e 's/inet6 \([0-9:a-z%]*\) .*/\1/'`
		else
			IPV6=" "
		fi
	fi
	echo $IPV6
	exit 0
fi
if [ "$1" = "pre" ]; then
	PRE=`ifconfig $2 | grep 'inet6 ' | sed -e 's/.*prefixlen \([0-9]*\) .*/\1/'`
	if [ -z "$PRE" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			PRE=`cat /etc/ifconfig.$2 | grep prefixlen | \
			     sed -e 's/.*prefixlen *\([0-9]*\).*/\1/'`
		else
			PRE="64"
		fi
	if [ -z "$PRE" ]; then
		PRE="64"
	fi
	fi
	echo $PRE
	exit 0
fi
if [ "$1" = "i" ]; then
	NWID=`ifconfig $2 | grep 'nwid ' | \
	    sed -f /usr/share/sushi/network/interfaces/interface/nwid.sed`
	if [ -z "$NWID" ]; then
		if [ -f "/etc/ifconfig.$2" ]; then
			NWID=`cat /etc/ifconfig.$2 | grep nwid | \
			     sed -f /usr/share/sushi/network/interfaces/interface/nwid.sed`
		else
			NWID=""
		fi
	fi
	echo $NWID
	exit 0
fi

echo ""
