#! /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 [ $ON_AC -eq 1 ]; then
		if [ "$ACTIVATE" -eq 1 ]; then
			THROTTLE_ETHERNET="$LM_AC_THROTTLE_ETHERNET"
		else
			THROTTLE_ETHERNET="$NOLM_AC_THROTTLE_ETHERNET"
		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
                DISABLED=0
                path=`readlink -f /sys/class/net/$DEVICE`
		dev_path=""
                log "VERBOSE" "ethernet: $path"
                if ! [ -z $path ]; then
                        if [ -d $path/device ]; then
                          dev_path=`readlink -f $path/device`
                          log "VERBOSE" "ethernet: $dev_path"
                        fi
                fi

                if ! [ -z $dev_path ] && [ -f $dev_path/enable ]; then
                        if [ x$DISABLE_ETHERNET = x1 ]; then
                                if [ -f $dev_path/enable ]; then
                                        echo 0 > $dev_path/enable
                                        log "VERBOSE" "ethernet: Disabling ethernet device $DEVICE"
                                        DISABLED=1
                                fi
                        elif [ x$DISABLE_ETHERNET = x0 ]; then
                                if [ -f $dev_path/enable ]; then
                                        echo 1 > $dev_path/enable
                                        log "VERBOSE" "ethernet: Re-enabling ethernet device $DEVICE"
                                        DISABLED=0
                                fi
                        elif [ x$DISABLE_ETHERNET = x2 ]; then
                                DISABLED=0 # Be safe. :-)
                        else
                                DISABLED=0 # Same here. Be safe. :-)
                                           # For all other cases also, just disable it.
                        fi
                else
                        log "VERBOSE" "$DEVICE does not seem to be supporting enable/disable"
                fi

               if [ x$DISABLED = x1 ]; then
                       continue
               fi

		# 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
		
		# Determine speed
		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

		# 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
		else
			# Handle Speed Throttling
			ret=`$ETHTOOL -s $DEVICE speed $MAX_SPEED 2>&1`
                        exit_status=$?;
                        log "VERBOSE" "$ret";
			if [ $exit_status -eq 0 ]; then
				log "VERBOSE" "Restored speed to $MAX_SPEED Mbit for $DEVICE"
			else
				log "VERBOSE" "Could not restore speed for $DEVICE"
			fi
		fi
	done
else
	log "VERBOSE" "Ethernet module is disabled."
fi

