#!/bin/sh /etc/rc.common

START=30
USE_PROCD=1

#validate_healthcheck_section() {
#}

#healthcheck_config() {
#}

reload_service() {
	config_load healthcheck
}

service_triggers()
{
	procd_add_reload_trigger "healthcheck"
}

start_service() {
	uci -q show healthcheck || {
		touch /etc/config/healthcheck
		uci set healthcheck.internet_connection=internet_connection
		uci set healthcheck.internet_connection.force_disable_swupdated="off"
		uci set healthcheck.internet_connection.force_disable_rtty="off"
		uci -q show healthcheck.internet_connection.status || {
		uci set healthcheck.internet_connection.status="offline"
		}
		uci set healthcheck.plc=plc
		uci set healthcheck.plc.resets=0
		uci commit healthcheck
	}

	# reset plc statistics
	uci set healthcheck.plc.resets=0
	uci commit healthcheck

	[ ! -d /root/logs ] && mkdir /root/logs
	procd_open_instance
	procd_set_param command /usr/sbin/healthcheck

	#respawn automatically if something died, be careful
	procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-2} ${respawn_retry:-0}
	#reload
	procd_set_param file /etc/config/healthcheck
	#log printfs
	procd_set_param stdout 1
	procd_set_param stderr 1
	procd_set_param user root
	
	if [[ ! -f "/root/backup.tar.bz2" ]];then
		logger "Generating backup..."
		sysupgrade -b /root/backup.tar.bz2
	fi

	procd_close_instance
}

#stop_service() {
#}

