#!/bin/sh

. /lib/functions.sh

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

case "$ACTION" in
pressed)
	[ -z "$OVERLAY" ] && return 0

	return 5
;;
timeout)
	. /etc/diag.sh
	set_state failsafe
;;
released)
	if [ "$SEEN" -gt 3 ]
	then
		echo "RESET CONFIG & REBOOT" > /dev/console
		uci set network.lan.ipaddr=192.168.100.1
		uci set network.lan.netmask=255.255.255.0
		uci set network.lan.gateway=0.0.0.0
		uci set network.lan.proto=static
		uci commit
		/etc/init.d/dnsmasq enable
		echo '{"username":"admin","password":""}' >/root/login.conf
                
                SSL_FILE="/root/ssl.conf"
                . /usr/share/libubox/jshn.sh
                if [ -s "$SSL_FILE" ]; then
                    json_init
                    json_load "$(cat $SSL_FILE)"
                    json_add_boolean "force" 0
                    echo "$(json_dump)" > "$SSL_FILE"
                fi
		sync
		reboot
	fi
;;
esac

return 0
