#!/bin/sh
[ -n "$IPKG_INSTROOT" ] || {
	/etc/init.d/ednovas enable 2>/dev/null
	# Make the LuCI menu entry show up: drop the dispatcher caches and let
	# rpcd pick up the new ACL file (menu is hidden without it).
	rm -f /tmp/luci-indexcache /tmp/luci-indexcache.* 2>/dev/null
	rm -rf /tmp/luci-modulecache 2>/dev/null
	# rpcd scans /usr/libexec/rpcd only at startup: restart it when our ubus
	# object is missing (first install). Upgrades skip the restart -- exec
	# plugins are re-read per call, and a restart would drop LuCI sessions.
	if ! ubus list ednovas >/dev/null 2>&1; then
		/etc/init.d/rpcd restart 2>/dev/null
	fi
	/etc/init.d/rpcd reload 2>/dev/null
	# Persistent firewall integration (opkg does not run uci-defaults on a
	# live system): register the fw3 include, then reload so the fw4 drop-in
	# (usr/share/nftables.d) or the fw3 include takes effect immediately.
	if [ -f /etc/uci-defaults/99-ednovas-firewall ]; then
		sh /etc/uci-defaults/99-ednovas-firewall && rm -f /etc/uci-defaults/99-ednovas-firewall
	fi
	/etc/init.d/firewall reload 2>/dev/null
	# The edition flag lives in the conffile, and opkg preserves a modified
	# conffile across a standard<->mini switch: assert the edition this
	# package ships instead of trusting the packaged default to land.
	uci -q set ednovas.main.mini=1 2>/dev/null; uci commit ednovas 2>/dev/null
	# Restart so an upgrade takes effect immediately (also refreshes a stale
	# agent binary via the init script's version check). The .run installer
	# sets a marker and starts the service itself after copying binaries.
	if [ ! -f /tmp/.ednovas-run-install ] && /etc/init.d/ednovas enabled 2>/dev/null; then
		/etc/init.d/ednovas stop >/dev/null 2>&1
		/etc/init.d/ednovas start 2>/dev/null
	fi
	echo "EdNovas installed. Open LuCI -> Services -> EdNovas to use it."
	echo "(Log out/in of LuCI once if the menu entry is missing.)"
}
exit 0
