/etc/init.d/miniupnpd enabled || exit 0

# If miniupnpd is not running:
# - check on _any_ event (event updates may contribute to network_find_wan*)

# If miniupnpd _is_ running:
# - check only on ifup (otherwise lease updates etc would cause
#   miniupnpd state loss)

[ "$ACTION" != "ifup" ] && /etc/init.d/miniupnpd running && exit 0

tmpconf="/var/etc/miniupnpd.conf"
external_iface=$(uci -q get upnpd.config.external_iface)
external_iface6=$(uci -q get upnpd.config.external_iface6)
external_zone=$(uci -q get upnpd.config.external_zone)
[ -x "$(command -v nft)" ] && FW="fw4" || FW="fw3"

. /lib/functions/network.sh

if [ -n "$external_iface" ] ; then
	network_get_device ifname "$external_iface"
else
	if [ -n "$external_zone" ] ; then
		ifname=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
	else
		network_find_wan external_iface && \
			network_get_device ifname "$external_iface"
	fi
fi
if [ -n "$external_iface6" ] ; then
	network_get_device ifname6 "$external_iface6"
else
	if [ -n "$external_zone" ] ; then
		ifname6=$($FW -q zone "$external_zone" 2>/dev/null | head -1)
	else
		network_find_wan6 external_iface6 && \
			network_get_device ifname6 "$external_iface6"
	fi
fi

[ "$DEVICE" != "$ifname" ] && [ "$DEVICE" != "$ifname6" ] && exit 0

grep -qs "^ext_ifname=$ifname" "$tmpconf" && grep -qs "^ext_ifname6=$ifname6" "$tmpconf" && exit 0

/etc/init.d/miniupnpd restart
