#!/bin/sh
if [ -z "${IPKG_INSTROOT}" ]; then
	# Stop mwan3evtd if present (older installs included this daemon). The
	# pre-upgrade hook runs before APK removes files no longer in the
	# package, so this must happen here to avoid leaving a running process
	# with no init script or binary after the file swap.
	/etc/init.d/mwan3evtd stop 2>/dev/null
	/etc/init.d/mwan3evtd disable 2>/dev/null
	# Record whether this is a fresh install so postinst can auto-enable
	# without overriding an explicit disable by the user on upgrade.
	[ -f /etc/init.d/mwan3 ] || touch /tmp/mwan3_first_install
	# Stop mwan3 before APK replaces any files. This is critical for
	# upgrades: if mwan3 is running when the init script is replaced,
	# procd's inotify trigger restarts the service while the old ip rules
	# are still installed, producing RTNETLINK "File exists" errors when
	# start_service tries to re-add them. Stopping here puts the service
	# into procd's "stopped" state before the init script changes, so
	# procd does not auto-restart it.
	/etc/init.d/mwan3 stop 2>/dev/null
fi
exit 0
