#! /bin/sh
#
# Laptop mode tools module: Ethernet power saving tweaks.
#

if [ x$CONTROL_ETHERNET = x1 ] || [ x$ENABLE_AUTO_MODULES = x1 -a x$CONTROL_ETHERNET = xauto ]; then

	# Let's check the binaries firts
	if [ -x /sbin/ethtool ]; then
		ETHTOOL=/sbin/ethtool
	elif [ -x /usr/sbin/ethtool ]; then
		ETHTOOL=/usr/sbin/ethtool
	else
		log "VERBOSE" "ethtool is not installed"
		ETHTOOL=/bin/false
	fi

	if [ -x /sbin/mii-tool ]; then
		MIITOOL=/sbin/mii-tool
	elif [ -x /usr/sbin/mii-tool ]; then
		MIITOOL=/usr/sbin/mii-tool
	else
		log "VERBOSE" "mii-tool is not installed"
		MIITOOL=/bin/false
	fi

	if [ -x /bin/ip ]; then
		IPTOOL=/bin/ip
	else
		log "VERBOSE" "ip is not installed"
		IPTOOL=/bin/false
	fi


	# Determine speed capability of physical device
	speed=`$MIITOOL -v $DEVICE 2>/dev/null | grep capabilities | tr ' ' '\n' |\
		sort -n | sed -ne '/^1.*/p' | cut -d "b" -f1`
	if [ -z "$speed" ]; then
		speed=0;
	fi
	max_s=0;
	min_s=100000;
	for s in $speed;
	do
		if [ $s -gt $max_s ]; then
			max_s=$s
		fi
		if [ $s -lt $min_s ]; then
			min_s=$s
		fi
	done
	MAX_SPEED=$max_s;

	case "$THROTTLE_SPEED" in
		"slowest")
			THROTTLE_SPEED=$min_s
			;;
		"fastest")
			THROTTLE_SPEED=$max_s
			;;
	esac

	# Carrier detection
	if $IPTOOL link show $DEVICE | grep -q NO-CARRIER; then
		carrier="false";
	else
		carrier="true";
	fi


	# What state we are in
	if [ $ON_AC -eq 1 ]; then
		if [ "$ACTIVATE" -eq 1 ]; then
			THROTTLE_ETHERNET="$LM_AC_THROTTLE_ETHERNET"
		else
			THROTTLE_ETHERNET="$NOLM_AC_THROTTLE_ETHERNET"
		fi

		# One off a case.
		# So that when  back on AC, we can resume the speed back to MAX.
		if [ x$THROTTLE_ETHERNET = x0 ]; then
			THROTTLE_SPEED=$MAX_SPEED;
		fi

		if [ x$DISABLE_ETHERNET_ON_BATTERY = x1 ]; then
			# We are ON_AC and Disable feature is requested
			# So we might be required to re-enable the device.
			DISABLE_ETHERNET=0 #zero would mean that the device needs be re-enabled.
		else
			DISABLE_ETHERNET=2
			# IF this is the case, don't touch the device for
			# enable/disable
		fi
	else
		THROTTLE_ETHERNET="$BATT_THROTTLE_ETHERNET"

		if [ x$DISABLE_ETHERNET_ON_BATTERY = x1 ]; then
			DISABLE_ETHERNET=1
		else
			DISABLE_ETHERNET=2
		fi
	fi

	for DEVICE in $ETHERNET_DEVICES ; do
		log "VERBOSE" "ethernet: $DEVICE"

		# Wakeup-on-LAN handling
		if [ x$DISABLE_WAKEUP_ON_LAN = x1 ] ; then
			ret=`$ETHTOOL -s $DEVICE wol d 2>&1`
			exit_status=$?;
			log "VERBOSE" "$ret"
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Enabled wakeup-on-LAN for $DEVICE"
			else
				log "VERBOSE" "Could not enable wakeup-on-LAN for $DEVICE"
			fi
		fi

		# Handle throttling
		if [ x$THROTTLE_ETHERNET = x1 ] ; then
			# Handle Speed Throttling
			ret=`$ETHTOOL -s $DEVICE speed $THROTTLE_SPEED 2>&1`
			exit_status=$?;
			log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Throttled speed to $THROTTLE_SPEED Mbit for $DEVICE"
			else
				log "VERBOSE" "Could not throttle speed for $DEVICE"
			fi
		fi

		# Shut down interface
		if [ x$DISABLE_ETHERNET = x1 -a $carrier = "false" ]; then
			log "VERBOSE" "ethernet: Disabling ethernet device $DEVICE"
			$IPTOOL link set dev $DEVICE down
		elif [ x$DISABLE_ETHERNET = x0 ]; then
			$IPTOOL link set dev $DEVICE up
			log "VERBOSE" "ethernet: Re-enabling ethernet device $DEVICE"
		fi

	done
else
	log "VERBOSE" "Ethernet module is disabled."
fi

