#!/bin/sh
[ ! -f "/usr/share/ucitrack/luci-app-timecontrol.json" ] && {
    cat > /usr/share/ucitrack/luci-app-timecontrol.json << EEOF
{
    "config": "timecontrol",
    "init": "timecontrol"
}
EEOF
}

uci -q batch <<-EOF >/dev/null
	delete firewall.timecontrol
EOF

if ! command -v fw4 >/dev/null 2>&1; then
	uci -q batch <<-EOF >/dev/null
		set firewall.timecontrol=include
		set firewall.timecontrol.type=script
		set firewall.timecontrol.path=/var/etc/timecontrol.include
		set firewall.timecontrol.reload=1
	EOF
	install -d /var/etc
	printf '%s\n' "/etc/init.d/timecontrol reload" > /var/etc/timecontrol.include
fi

uci -q commit firewall

[ -f "/etc/config/ucitrack" ] && {
	uci -q batch <<-EOF >/dev/null
		delete ucitrack.@timecontrol[-1]
		add ucitrack timecontrol
		set ucitrack.@timecontrol[-1].init=timecontrol
		commit ucitrack
	EOF
}

rm -rf /tmp/luci-*cache
exit 0
