#!/bin/sh

. /lib/functions.sh

OVERLAY="$( grep ' /overlay ' /proc/mounts )"

case "$ACTION" in
pressed)
	[ -z "$OVERLAY" ] && return 0
	#return the timeout 10 seconds

	if [ -f /tmp/test_mode ]; then 
		echo "TEST_MODE_RESET pressed" > /dev/console
		echo 1 > /tmp/reset_button
		return
	fi	
	
	return 10
;;
timeout)
	#. /etc/diag.sh
	#set_state failsafe
	killall ledhandler
	ledctrl power orange on
	ledctrl power white off
;;
released)
	if [ -f /tmp/test_mode ]; then 
		echo "TEST_MODE_RESET released" > /dev/console
		rm -rf /tmp/reset_button
		return
	fi

	if [ "$SEEN" -lt 1 ]
	then
		echo "REBOOT" > /dev/console
		sync
#		reboot
	elif [ "$SEEN" -ge 10 -a -n "$OVERLAY" ]
	then
		echo "FACTORY RESET" > /dev/console
		jffs2reset -y && reboot &
	fi
;;
esac

return 0
