#!/bin/sh
# ifrename hotplug script by Petr Baudis <pasky@ucw.cz>

HOTPLUGFUNCS=/etc/hotplug/hotplug.functions
[ -f $HOTPLUGFUNCS ] || exit 1
. $HOTPLUGFUNCS
DEBUG=yes export DEBUG

[ "$INTERFACE" ] || { mesg Bad invocation: \$INTERFACE is not set ; exit 1 ; }

# Prevent circular renaming
[ ! $IFRENAME_BREAK ] || { exit 0 ; }

case $ACTION in
add|register)
	# Run ifrename as needed - Jean II
	# Remap interface names based on MAC address. This workaround
	# the dreaded configuration problem "all my cards are 'eth0'"...
	# This needs to be done before ifup otherwise ifup will get
	# confused by the name changed and because iface need to be
	# down to change its name.
	if [ -x /sbin/ifrename ] && [ -r /etc/iftab ]; then
		debug_mesg invoke ifrename for $INTERFACE
		NEWNAME=`/sbin/ifrename -i $INTERFACE`
		if [ -n "$NEWNAME" ]; then
			debug_mesg iface $INTERFACE is remapped to $NEWNAME
			# We run hotplug from scratch for the new interface,
			# since we can't give it back the new interface name.
			# Nothing will be run two times since we should be
			# the first one in chain.
			export INTERFACE=$NEWNAME
			export DEVPATH=/class/net/$INTERFACE
			DEBUG= IFRENAME_BREAK=1 /sbin/hotplug "$@"
			exit 4
		fi;
	fi
	;;
remove|unregister)
	;;
esac
