#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          qcontrol
# Required-Start:    $all
# Required-Stop:     
# Should-Start:      
# Should-Stop:       
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Change status leds for QNAP Turbo Station devices
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

DAEMON=/usr/sbin/qcontrol
NAME=qcontrol

PIDFILE=/var/run/$NAME.pid 

test -x $DAEMON || exit 0

. /lib/lsb/init-functions

SOUND_BUZZER=yes

# Include defaults if available
if [ -f /etc/default/$NAME ] ; then
	. /etc/default/$NAME
fi

set -e

SOCKET=/var/run/qcontrol.sock

qcontrol_start() {
	# Should not already be running, but check anyway
	local pid="$(pidof qcontrol || true)"
	if [ -z "$pid" ]; then
		rm -f $SOCKET
		qcontrol -d >/dev/null & disown
		# allow time to startup (read config)
		sleep 0.5
		pid=$(pidof qcontrol)
	fi

	if [ "$pid" ]; then
		if [ -S $SOCKET ]; then
			echo $pid
			return 0
		else
			kill -TERM $pid
		fi
	fi
	return 1
}

# The gpio_keys character device is required with the default
# Debian configuration file.
test_event_dev() {
	if [ ! -c /dev/input/by-path/platform-gpio-keys-event ]; then
		log_warning_msg "qcontrol error: gpio_keys device not available"
		return 1
	fi
}

case "$1" in
    start)
	# Change status led to show green
	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")
		test_event_dev || exit 0
		if pid=$(qcontrol_start); then
			log_action_msg "System boot completed"
			# Returns 1 even on success
			qcontrol statusled greenon || true
			qcontrol powerled on || true
			if [ "$SOUND_BUZZER" != no ]; then
				qcontrol buzzer short || true
			fi

			# Kill the control process
			kill -TERM $pid
			rm -f $SOCKET
		fi
		;;
	    "QNAP TS-409" | "QNAP TS-41x")
		test_event_dev || exit 0
		if pid=$(qcontrol_start); then
			log_action_msg "System boot completed"
			# Returns 1 even on success
			qcontrol statusled greenon || true
			if [ "$SOUND_BUZZER" != no ]; then
				qcontrol buzzer short || true
			fi

			# Kill the control process
			kill -TERM $pid
			rm -f $SOCKET
		fi
		;;
	    *)
		log_warning_msg "qcontrol error: device is not supported"
		;;
	esac
	;;
    stop)
	# Change status led to show red
	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")
		test_event_dev || exit 0
		if pid=$(qcontrol_start); then
			log_action_msg "Preparing for shutdown"
			# Returns 1 even on success
			if [ "$SOUND_BUZZER" != no ]; then
				qcontrol buzzer short || true
			fi
			qcontrol statusled redon || true
			qcontrol powerled 1hz || true

			# Kill the control process
			kill -TERM $pid
			rm -f $SOCKET
		fi
		;;
	    "QNAP TS-409" | "QNAP TS-41x")
		test_event_dev || exit 0
		if pid=$(qcontrol_start); then
			log_action_msg "Preparing for shutdown"
			# Returns 1 even on success
			if [ "$SOUND_BUZZER" != no ]; then
				qcontrol buzzer short || true
			fi
			qcontrol statusled redon || true

			# Kill the control process
			kill -TERM $pid
			rm -f $SOCKET
		fi
		;;
	    *)
		log_warning_msg "qcontrol error: device is not supported"
		;;
	esac
	;;
    force-stop|restart|force-reload|status|reload)
	;;
    *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
	exit 1
	;;
esac

exit 0
