#!/bin/sh
[ -n "${IPKG_INSTROOT}" ] || {
	# Restore dispatcher from backup
	DISPATCHER="/usr/share/ucode/luci/dispatcher.uc"
	if [ -f "${DISPATCHER}.bak" ]; then
		mv "${DISPATCHER}.bak" "$DISPATCHER"
		echo "Restored LuCI dispatcher from backup"
	fi

	# Remove JS hook from modern LuCI theme headers
	for header in /usr/share/ucode/luci/template/themes/*/header.ut; do
		[ -f "$header" ] && sed -i '/secubox-auth-hook/d' "$header" 2>/dev/null || true
	done

	# Remove JS hook from legacy LuCI theme headers
	for header in /usr/lib/lua/luci/view/themes/*/header.htm; do
		[ -f "$header" ] && sed -i '/secubox-auth-hook/d' "$header" 2>/dev/null || true
	done

	# Remove JS hook from sysauth
	if [ -f /usr/lib/lua/luci/view/sysauth.htm ]; then
		sed -i '/secubox-auth-hook/d' /usr/lib/lua/luci/view/sysauth.htm 2>/dev/null || true
	fi

	# Restart uhttpd to apply changes
	/etc/init.d/uhttpd restart 2>/dev/null || true
}
exit 0
