#!/bin/sh

uci -q batch <<-EOF >/dev/null
	set dhcp.@dnsmasq[0].localuse=1
	commit dhcp
	[ -e "/etc/config/ucitrack" ] && {
	delete ucitrack.@passwall2[-1]
	add ucitrack passwall2
	set ucitrack.@passwall2[-1].init=passwall2
	commit ucitrack
	}
	delete firewall.passwall2
	set firewall.passwall2=include
	set firewall.passwall2.type=script
	set firewall.passwall2.path=/var/etc/passwall2.include
	set firewall.passwall2.reload=1
	commit firewall
	[ -e "/etc/config/ucitrack" ] && {
	delete ucitrack.@passwall2_server[-1]
	add ucitrack passwall2_server
	set ucitrack.@passwall2_server[-1].init=passwall2_server
	commit ucitrack
	}
	delete firewall.passwall2_server
	set firewall.passwall2_server=include
	set firewall.passwall2_server.type=script
	set firewall.passwall2_server.path=/var/etc/passwall2_server.include
	set firewall.passwall2_server.reload=1
	commit firewall
	set uhttpd.main.max_requests=50
	commit uhttpd
EOF

[ ! -s "/etc/config/passwall2" ] && cp -f /usr/share/passwall2/0_default_config /etc/config/passwall2

chmod +x /usr/share/passwall2/*.sh

[ -e "/etc/config/passwall2_show" ] && rm -rf /etc/config/passwall2_show

[ "$(uci -q get passwall2.@global_xray[0].sniffing)" == "1" ] && [ "$(uci -q get passwall2.@global_xray[0].route_only)" != "1" ] && uci -q set passwall2.@global_xray[0].sniffing_override_dest=1
uci -q delete passwall2.@global_xray[0].sniffing
uci -q delete passwall2.@global_xray[0].route_only
uci -q commit passwall2

rm -f /tmp/luci-indexcache
rm -rf /tmp/luci-modulecache/
killall -HUP rpcd 2>/dev/null

exit 0
