#!/bin/sh

[ -n "$IPKG_INSTROOT" ] && exit 0





if [ "$1" = "upgrade" ] || [ "$PKG_UPGRADE" = "1" ]; then
	[ -x /etc/init.d/quicksilver ] && /etc/init.d/quicksilver stop >/dev/null 2>&1
	exit 0
fi


if [ -x /usr/sbin/quicksilvercore ]; then
	stopped=0
	for attempt in 1 2; do
		if /usr/sbin/quicksilvercore stop >/dev/null 2>&1; then
			stopped=1
			break
		fi
		sleep 2
	done
	if [ "$stopped" != 1 ]; then


		rm -f /tmp/dnsmasq.d/ezlink-quicksilver.conf /tmp/dnsmasq.*.d/ezlink-quicksilver.conf
		/etc/init.d/dnsmasq restart >/dev/null 2>&1



		if uci -q get dhcp.@dnsmasq[0].server 2>/dev/null | grep -q '127\.0\.0\.1#'; then
			echo "Unable to restore router DNS settings; quicksilver was not removed. Run '/usr/sbin/quicksilvercore stop', then remove it again." >&2
			exit 1
		fi
		uci -q delete firewall.ezlink_quicksilver_wan
		uci -q delete firewall.ezlink_quicksilver_wan_ipv6
		uci -q commit firewall
		/etc/init.d/firewall reload >/dev/null 2>&1
		echo "Warning: the quicksilver proxy did not stop cleanly; its network changes were removed and the package is being uninstalled." >&2
	fi
fi



if [ -x /etc/init.d/quicksilver ]; then
	/etc/init.d/quicksilver stop >/dev/null 2>&1 || true
	/etc/init.d/quicksilver disable >/dev/null 2>&1 || true
fi





rm -rf /tmp/ezlink/quicksilver /tmp/ezlink/.quicksilver /tmp/quicksilver /tmp/.quicksilver
rm -f /tmp/ezlink-progress/quicksilver.*
exit 0
