#!/bin/sh
# -d for description
# -s "iface iface" for SKIP iface in list
# -o "iface" - show ONLY iface in list
DESC=0
ALL=0
SKIP="ipfw ppp sl lp faith fwe fwip plip pfsync pflog tun vboxnet"

while getopts "ado:s:" opt; do
	case "${opt}" in
		a)
			ALL=1
			;;
		d)
			DESC=1
			;;
		o)
			ONLY_IF="${OPTARG}"
			;;
		s)
			SKIP="${SKIP} ${OPTARG}"
			;;
	esac
	shift $(($OPTIND - 1))
done

[ ${ALL} -eq 1 ] && SKIP=""

for i in $( /sbin/ifconfig -l ); do

	if_skip=0

	tif="${i%%[0-9]*}"

	for n in ${SKIP}; do
		[ "${n}" = "${tif}" ] && if_skip=1 && break
	done

	for n in ${ONLY_IF}; do
		[ "${n}" != "${tif}" ] && if_skip=1 && break
	done

	[ ${if_skip} -eq 1 ] && continue

	if [ ${DESC} -eq 1 ]; then
		#this need for rewrite to more stable and simple code
		uptest=$( /sbin/ifconfig ${i}| /usr/bin/awk '/status: /{printf $2}' )
		case "${uptest}" in
			active|associated)
				STATUS="UP"
				;;
			no*)
				STATUS="DOWN"
				;;
			*)
				STATUS="NO STATE"
		esac

		case "${tif}" in
			lo)
				IDENT="software loopback"
				;;
			vtnet)
				IDENT="VirtIO Ethernet driver"
				;;
			*)
				IDENT=$( /usr/sbin/pciconf -vl | /usr/bin/grep -A4 ^${i} | /usr/bin/egrep "device*=*" | /usr/bin/cut -d "=" -f 2|/usr/bin/xargs )
				;;
		esac
		[ -z "${IDENT}" ] && IDENT="Unknown vendor"
		# this is not work in msk0: its looks like:
		# mskc0@pci0:3:0:0:       class=0x020000 card=0x84391043 chip=0x438111ab rev=0x11 hdr=0x00
		# vendor     = 'Marvell Technology Group Ltd.'
		# device     = 'Yukon Optima 88E8059 [PCIe Gigabit Ethernet Controller with AVB]'
		# class      = network
		# subclass   = ethernet
		# - in pciconv, not msk0
		echo "${i}: $IDENT: ${STATUS}"
	else
		echo "${i}"
	fi

done
