#!/bin/bash
# ok. We try to automatically figure out which device is connected to our
# module. Parse all entries in $SYSROOT and verify whether the corresponding
# driver is the name of the driver we are looking for. On success, return
# the devicename in $DEVICE. By this it doesn't matter how the device is actually
# called. Simply modifying the MODNAME accordingly would allow to use this for
# any network module in question. This is done by the function finddevice.

SED=/bin/sed
MODNAME=r8101
SYSROOT="/sys/class/net"
DRVROOT="/device/driver/module/drivers"
DEVICE=""

. /usr/lib/pm-utils/functions

function finddevice () {
	cd ${SYSROOT}
	for i in *; do
		# "lo" is nothing we'd care about.
		if [ "$i" != "lo" ]; then
			cd ${SYSROOT}/${i}/${DRVROOT}
			for j in *; do
			        # the module name is written like pci:r8101. Remove "pci:"
				DRVNAME=`echo -n ${j}| ${SED} "s/^.*://g"`
			done
			if [ "${DRVNAME}" == "${MODNAME}" ]; then
				export DEVICE=$i
				break
			fi
		fi
	done
	# do nothing and return an error if we were unable to detect the 
	# device in question.
	if [ "${DEVICE}" == "" ]; then
		exit -1
	fi
}

case "$1" in
	hibernate|suspend)
		finddevice
		ifdown ${DEVICE}
		rmmod ${MODNAME}
		;;
	thaw|resume)
		modprobe ${MODNAME}
		finddevice
		ifup ${DEVICE}
		;;
	*)
		;;
esac

exit $?
