#!/bin/sh

key_name() {
	case "$1" in
		256) echo "BTN_0" ;;
		257) echo "BTN_1" ;;
		258) echo "BTN_2" ;;
		259) echo "BTN_3" ;;
		260) echo "BTN_4" ;;
		261) echo "BTN_5" ;;
		262) echo "BTN_6" ;;
		263) echo "BTN_7" ;;
		264) echo "BTN_8" ;;
		265) echo "BTN_9" ;;
		408) echo "reset" ;;
		529) echo "wps" ;;
		247) echo "rfkill" ;;
		*) echo "UNKNOWN" ;;
	esac
}

mesh_btn=""
wps_btn=""

for base in /proc/device-tree/gpio-keys /proc/device-tree/keys; do
	[ -d "$base" ] || continue
	for d in "$base"/*; do
		[ -f "$d/label" ] || continue
		name=$(cat "$d/label")
		lname=$(echo "$name" | tr 'A-Z' 'a-z')
		hex=$(hexdump -n 4 -e '4/1 "%02x"' "$d/linux,code")
		code=$((0x$hex))
		btn=$(key_name $code)

		case "$lname" in
			*mesh*) mesh_btn="$btn" ;;
			*wps*)  wps_btn="$btn" ;;
		esac
	done
done

if [ -n "$mesh_btn" ]; then
	MESHBTN=$mesh_btn
elif [ -n "$wps_btn" ]; then
	MESHBTN=$wps_btn
fi

led=$(ls /sys/class/leds/ | grep -m1 -i mesh)
[ -z "$led" ] && led=$(ls /sys/class/leds/ | grep -m1 -i wps)
[ -z "$led" ] && led=$(ls /sys/class/leds/ | grep -m1 -i wlan)
if [ -z "$led" ]; then
	. /lib/functions/leds.sh
	led=$(get_dt_led running)
	[ -z "$led" ] && led=$(get_dt_led boot)
fi

if [ -n "$led" ]; then
	LED_PATH="/sys/class/leds/$led"
fi

[ -z "$MESHBTN" ] && exit 0

if [ -n "$MESHBTN" ]; then
	cat <<'EOF' | sed "s|@LED_PATH@|$LED_PATH|" > "/etc/rc.button/${MESHBTN}"
#!/bin/sh

LED_MESH_TIMER_PID="/tmp/led_mesh_blinker.pid"
LED_MESH_LONGACTION_LOCK="/tmp/led_mesh_longaction.lock"

if [ -f "$LED_MESH_LONGACTION_LOCK" ]; then
	exit 0
fi

led_mesh_control() {
	LED_PATH=@LED_PATH@
	TMP_PREFIX="/tmp/led_mesh"
	PARAMS="trigger device_name link rx tx brightness"

	get_current_trigger() {
		sed -n 's/.*\[\(.*\)\].*/\1/p' "$LED_PATH/trigger"
	}

	case "$1" in
		off)
			for param in $PARAMS; do
				file="${LED_PATH}/${param}"
				tmp="${TMP_PREFIX}_$param"
				if [ -f "$tmp" ] && [ -f "$file" ]; then
					cat "$tmp" > "$file" 2>/dev/null
					rm -f "$tmp"
				fi
			done
			;;
		*)
			DELAY_ON="${1:-500}"
			DELAY_OFF="${2:-500}"

			for param in $PARAMS; do
				file="${LED_PATH}/${param}"
				tmp="${TMP_PREFIX}_$param"
				[ -f "$tmp" ] && continue

				case "$param" in
					trigger)
						get_current_trigger > "$tmp" 2>/dev/null
						;;
					*)
						[ -f "$file" ] && cat "$file" > "$tmp" 2>/dev/null
						;;
				esac
			done

			echo timer > "$LED_PATH/trigger"
			echo "$DELAY_ON" > "$LED_PATH/delay_on"
			echo "$DELAY_OFF" > "$LED_PATH/delay_off"
			;;
	esac
}

start_wps_blinker() {
	if [ -f "$LED_MESH_TIMER_PID" ]; then
		old_pid=$(cat "$LED_MESH_TIMER_PID")
		kill "$old_pid" 2>/dev/null
	fi

	(
		led_mesh_control 500 500
		sleep 120
		led_mesh_control off
		rm -f "$LED_MESH_TIMER_PID"
	) &
	echo $! > "$LED_MESH_TIMER_PID"
}

case "$ACTION" in
	pressed)
		return 5
		;;

	timeout)
		led_mesh_control 200 200
		;;

	released)
		if [ "$SEEN" -lt 1 ]; then
			if [ -f "$LED_MESH_LONGACTION_LOCK" ]; then
				exit 0
			fi
			if /usr/bin/wps-button; then
				start_wps_blinker
			fi
		elif [ "$SEEN" -ge 5 ]; then
			if [ -f "$LED_MESH_LONGACTION_LOCK" ]; then
				exit 0
			fi

			touch "$LED_MESH_LONGACTION_LOCK"

			(
				if [ -f "$LED_MESH_TIMER_PID" ]; then
					old_pid=$(cat "$LED_MESH_TIMER_PID")
					kill "$old_pid" 2>/dev/null
					rm -f "$LED_MESH_TIMER_PID"
				fi

				led_mesh_control off
				led_mesh_control 200 200
				/usr/bin/mesh-button
				sleep 15
				led_mesh_control off
				/etc/init.d/led restart >/dev/null 2>&1
				sleep 1 # imp!
				rm -f "$LED_MESH_LONGACTION_LOCK"
			) &

			exit 0
		fi
		;;
esac

return 0
EOF
	chmod +x "/etc/rc.button/${MESHBTN}"
fi

exit 0
