#!/bin/bash

for link in /sys/class/net/* ; do
	export DEVPATH=${link#/sys}
	export INTERFACE=${DEVPATH#/class/net/}

	# whenever udev updates, this whitelist should be copied from
	#  /etc/udev/rules.d/75-persistent-net-generator.rules:
	case "$INTERFACE" in
		eth*|ath*|wlan*[0-9]|msh*|ra*|sta*|ctc*|lcs*|hsi*)
			;;
		*)
			continue
			;;
	esac

	# read "address" and "type" attributes
	export MATCHADDR=$(< $link/address)
	export MATCHIFTYPE=$(< $link/type)

	# do not use locally administered or empty (all-zeros) MAC addresses
	case "$MATCHADDR" in
		?[2367abef]:*|00:00:00:00:00:00)
			continue
			;;
	esac

	# ensure there's a driver
	[ -L $link/device/driver ] || continue
	export MATCHDRV=$(basename $(readlink $link/device/driver))

	# figure out what type of device this is
	tgt=$(readlink $link/device)
	case "$tgt" in
		*pcmcia*)
			card_id=$(< $link/device/card_id)
			manf_id=$(< $link/device/manf_id)
			export COMMENT="PCMCIA device ${card_id}:${manf_id} (${MATCHDRV})"
			;;
		*ieee1394*)
			host_id=$(< $link/device/host_id)
			export COMMENT="Firewire device ${host_id} (${MATCHDRV})"
			;;
		*usb*)
			# PCMCIA devices might, possibly, hang off a USB bus
			#  (not likely, but it doesn't hurt)
			vnd=$(< $link/device/idVendor)
			prd=$(< $link/device/idProduct)
			export COMMENT="USB device 0x${vnd}:0x${prd} (${MATCHDRV})"
			;;
		*pci*)
			# must be last: other types hang off the PCI bus too
			vnd=$(< $link/device/vendor)
			dev=$(< $link/device/device)
			export COMMENT="PCI device ${vnd}:${dev} (${MATCHDRV})"
			;;
		*)
			export COMMENT="Unknown-subsystem device (${MATCHDRV})"
			;;
	esac

	# don't worry about normal output, only errors
	/lib/udev/write_net_rules >/dev/null
done
