#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] && exit 0

_backup_external_configs() {
	_bdir="/etc/tachyon/external-configs-backup"
	for _comp in zapret zapret2 byedpi wdtt qwdtt olcrtc sing-box; do
		_cdir="$_bdir/$_comp"
		case "$_comp" in
			zapret) _srcs="/etc/config/zapret /opt/zapret/config /opt/zapret/ipset" ;;
			zapret2) _srcs="/etc/config/zapret2 /opt/zapret2/config /opt/zapret2/ipset" ;;
			byedpi) _srcs="/etc/config/byedpi /etc/byedpi" ;;
			wdtt|qwdtt) _srcs="/etc/config/wdtt /etc/wdtt /etc/qwdtt" ;;
			olcrtc) _srcs="/etc/config/olcrtc /etc/olcrtc" ;;
			sing-box) _srcs="/etc/sing-box/config.json /etc/config/sing-box" ;;
		esac
		_has_src=0
		for _s in $_srcs; do [ -e "$_s" ] && _has_src=1 && break; done
		[ "$_has_src" -eq 0 ] && continue
		mkdir -p "$_cdir" 2>/dev/null || continue
		for _s in $_srcs; do
			[ -e "$_s" ] || continue
			_dst="$_cdir/$(basename "$_s")"
			if [ -d "$_s" ]; then
				cp -a -n "$_s" "$_dst" 2>/dev/null || cp -a "$_s" "${_dst}.new" 2>/dev/null || true
			else
				[ -f "$_dst" ] && cmp -s "$_s" "$_dst" 2>/dev/null && continue
				cp -a "$_s" "$_dst" 2>/dev/null || true
			fi
		done
	done
}
_is_tachyon_managed() {
	_comp="$1"
	[ -f "/etc/init.d/$_comp" ] && grep -q "Tachyon managed" "/etc/init.d/$_comp" 2>/dev/null && return 0
	[ -d "/etc/tachyon/component-backups/$_comp" ] && return 0
	[ -d "/var/run/tachyon/$_comp" ] && return 0
	return 1
}

_backup_external_configs

# Stop and clean Tachyon internal steer subsystem
for _steer_svc in tachyon-steer-zapret steer; do
	if [ -f "/etc/init.d/${_steer_svc}" ]; then
		"/etc/init.d/${_steer_svc}" stop >/dev/null 2>&1 || true
		"/etc/init.d/${_steer_svc}" disable >/dev/null 2>&1 || true
		rm -f "/etc/init.d/${_steer_svc}" 2>/dev/null || true
	fi
done
rm -rf /etc/rc.d/*steer* /etc/steer /usr/lib/tachyon/steer /usr/sbin/steer /usr/bin/steer /var/run/tachyon/*steer* /tmp/*steer* 2>/dev/null || true
killall -9 steer >/dev/null 2>&1 || true
nft delete table inet steer >/dev/null 2>&1 || true

# Selective cleanup of external components only if Tachyon-managed
if [ -f /etc/init.d/tailscale ] && grep -q "Tachyon managed" /etc/init.d/tailscale 2>/dev/null; then
	/etc/init.d/tailscale stop >/dev/null 2>&1 || true
	/etc/init.d/tailscale disable >/dev/null 2>&1 || true
	rm -f /etc/init.d/tailscale /etc/rc.d/*tailscale* 2>/dev/null || true
	rm -rf /var/run/tachyon/tailscale /etc/tachyon/tailscale/*/native /etc/tachyon/component-backups/tailscale 2>/dev/null || true
	nft delete table inet tachyon_tailscale >/dev/null 2>&1 || true
fi
for _comp in zapret zapret2 byedpi wdtt-client qwdtt olcrtc; do
	_comp_marker="$_comp"
	case "$_comp" in wdtt-client) _comp_marker="wdtt" ;; esac
	if _is_tachyon_managed "$_comp_marker"; then
		if [ -f "/etc/init.d/${_comp}" ]; then
			"/etc/init.d/${_comp}" stop >/dev/null 2>&1 || true
			"/etc/init.d/${_comp}" disable >/dev/null 2>&1 || true
			rm -f "/etc/init.d/${_comp}" "/etc/rc.d/*${_comp}*" 2>/dev/null || true
		fi
		case "$_comp_marker" in
			zapret)
				killall -9 nfqws >/dev/null 2>&1 || true
				rm -f /usr/bin/nfqws 2>/dev/null || true
				rm -rf /opt/zapret /etc/config/zapret /var/run/tachyon/zapret* /etc/tachyon/component-backups/zapret /var/run/tachyon/component-update-checks/zapret.json 2>/dev/null || true
				for _tbl in zapret; do nft delete table inet "$_tbl" >/dev/null 2>&1 || true; nft delete table ip "$_tbl" >/dev/null 2>&1 || true; nft delete table ip6 "$_tbl" >/dev/null 2>&1 || true; done
				;;
			zapret2)
				killall -9 nfqws2 >/dev/null 2>&1 || true
				rm -f /usr/bin/nfqws2 2>/dev/null || true
				rm -rf /opt/zapret2 /etc/config/zapret2 /etc/tachyon/component-backups/zapret2 /var/run/tachyon/component-update-checks/zapret2.json 2>/dev/null || true
				for _tbl in zapret2; do nft delete table inet "$_tbl" >/dev/null 2>&1 || true; nft delete table ip "$_tbl" >/dev/null 2>&1 || true; nft delete table ip6 "$_tbl" >/dev/null 2>&1 || true; done
				;;
			byedpi)
				killall -9 ciadpi >/dev/null 2>&1 || true
				rm -f /usr/bin/ciadpi 2>/dev/null || true
				rm -rf /etc/config/byedpi /var/run/tachyon/byedpi /etc/tachyon/component-backups/byedpi /var/run/tachyon/component-update-checks/byedpi.json 2>/dev/null || true
				;;
			wdtt)
				killall -9 wdtt qwdtt-client >/dev/null 2>&1 || true
				rm -f /usr/bin/wdtt /usr/bin/qwdtt-client /usr/sbin/wdtt-genlists /usr/sbin/wdtt-resolve 2>/dev/null || true
				rm -rf /etc/qwdtt /etc/wdtt /etc/config/wdtt /var/run/tachyon/wdtt /var/run/qwdtt /etc/tachyon/component-backups/wdtt /var/run/tachyon/component-update-checks/wdtt.json 2>/dev/null || true
				;;
			olcrtc)
				killall -9 olcrtc >/dev/null 2>&1 || true
				rm -f /usr/bin/olcrtc 2>/dev/null || true
				rm -rf /etc/olcrtc /etc/config/olcrtc /var/run/tachyon/olcrtc /etc/tachyon/component-backups/olcrtc /var/run/tachyon/component-update-checks/olcrtc.json 2>/dev/null || true
				;;
		esac
	fi
done

TACHYON_LIB=/usr/lib/tachyon ucode -L /usr/lib/tachyon /usr/lib/tachyon/config/migration.uc migrate

/etc/init.d/tachyon enable >/dev/null 2>&1 || true
/usr/bin/tachyon package_postinst >/dev/null 2>&1 || true
# rpcd caches ACLs, so without this reload the policy shipped in this package
# stays inert until rpcd picks it up, and the stricter read role would look
# applied while the old one kept serving.
[ -x /etc/init.d/rpcd ] && /etc/init.d/rpcd reload >/dev/null 2>&1 || true
chmod 600 /etc/config/tachyon 2>/dev/null || true
chown root:root /etc/config/tachyon 2>/dev/null || true
if [ -d /www/cgi-bin ]; then
	if [ -n "$(uci -q get tachyon.settings.agent_api_token)" ]; then
		ln -sf /usr/lib/cgi-bin/tachyon-agent /www/cgi-bin/tachyon-agent 2>/dev/null || true
	else
		rm -f /www/cgi-bin/tachyon-agent 2>/dev/null || true
	fi
fi

for _f in /www/luci-static/resources/view/tachyon/*.js; do [ -f "$_f" ] && touch "$_f" 2>/dev/null || true; done
[ -f /www/luci-static/resources/luci.js ] && touch /www/luci-static/resources/luci.js 2>/dev/null || true; [ -d /www/luci-static/resources/view/tachyon ] && touch /www/luci-static/resources/view/tachyon 2>/dev/null || true; [ -d /www/luci-static/resources ] && touch /www/luci-static/resources 2>/dev/null || true

# Neutralize legacy unshielded postrm scripts on disk if upgrading from 1.3.21/1.3.22
for p in /usr/lib/opkg/info/tachyon.postrm /usr/lib/opkg/info/luci-app-tachyon.postrm /usr/lib/opkg/info/luci-i18n-tachyon-ru.postrm; do
	if [ -f "$p" ] && ! grep -q '\[ "$1" = "upgrade" \]' "$p"; then
		sed -i '2i[ "$1" = "upgrade" ] && exit 0' "$p" 2>/dev/null || true
	fi
done
exit 0
