#!/bin/sh

# Get kernel uevent attributes
params="$*"

# Filter out unwanted element
attrs="${params#* }"

# "source" them into this shell script
eval "$attrs"

# File indicating whether reconfiguring device is needed or not
reconf_file="/sys/class/android_usb/android0/reconfigure"

# Directory of USB compositions
COMP_DIR="/usr/bin/usb/compositions"

# Match "CONFIGURED" uevent
if [[ "$USB_STATE" == "CONFIGURED" ]] && [[ "$SUBSYSTEM" == "android_usb" ]] \
	&& [[ "$ACTION" == "change" ]]
then
	if [[ "$(cat $reconf_file)" == "1" ]]
	then
		echo 0 > ${reconf_file}
		$COMP_DIR/tplink n
	fi
# Match usb power supply off uevent, which means usb is unplugged
elif [[ "$POWER_SUPPLY_ONLINE" == "0" ]] && [[ "$POWER_SUPPLY_NAME" == "usb" ]] \
	&& [[ "$SUBSYSTEM" == "power_supply" ]] && [[ "$ACTION" == "change" ]]
then
	$COMP_DIR/tplink_comp n
fi
