#!/bin/sh

# Wrapper script for qcontrol to execute single commands
# If called with -t, it only tests if the device is supported

# Test if device is supported
device=$(grep "Hardware[[:space:]]*:" /proc/cpuinfo 2>/dev/null | \
	 head -n1 | sed "s/^[^:]*: //")
case $device in
    "QNAP TS-109/TS-209" | "QNAP TS-119/TS-219" | "QNAP TS-409" | "QNAP TS-41x")
	# Success or continue 
	[ "$1" = "-t" ] && exit 0 || true ;;
    *)
	# Failure or silently exit
	[ "$1" = "-t" ] && exit 1 || exit 0 ;;
esac

# The gpio_keys character device is required with the default
# Debian configuration file.
test_event_dev() {
	[ -c /dev/input/by-path/platform-gpio-keys-event ] || return 1
}

case $device in
    "QNAP TS-409" | "QNAP TS-41x")
	if [ "$1" = powerled ]; then
		exit 0
	fi ;;
esac

test_event_dev || exit 0

# Returns 1 even on success (TODO: is that still true?)
qcontrol --direct "$@" || true

exit 0
