#!/bin/sh
#
# The WAN came up, or came back with a different address.
#
# Which protocol gets through is a property of the provider, and the daemon
# remembers what worked keyed on the WAN interface, its address and its gateway.
# When that address changes the router may be on a different provider entirely --
# a failover to LTE, a new lease after an outage, somebody moving the box to
# another flat -- and the tunnel it is holding was proved against a network that
# is no longer there.
#
# So on ifup we simply take the tunnel down. The daemon's own supervisor puts it
# back within half a minute, which re-reads the uplink, re-consults the memory
# for that uplink and walks the ladder if it has nothing to go on. Doing it this
# way rather than with a new control method means there is one reconnection path
# in the daemon and not two.

[ "$ACTION" = ifup ] || exit 0

case "$INTERFACE" in
	wan|wan6|wwan|wan_4|wan_6) ;;
	*) exit 0 ;;
esac

# Nothing to do when the service is not running -- including the common case of
# this firing during boot, before procd has started the daemon at all.
[ -x /etc/init.d/meduzavpn ] || exit 0
/etc/init.d/meduzavpn running >/dev/null 2>&1 || exit 0

# `disconnect` fails when nothing is connected, which is not worth a log line:
# the supervisor is already trying in that case.
/usr/bin/meduzavpn disconnect >/dev/null 2>&1 || exit 0
logger -t meduzavpn -p daemon.notice \
	"$INTERFACE came up: the tunnel was taken down so the protocol is chosen again for this uplink"
exit 0
