#!/bin/sh

if [ "$(uci -q get mqvpn.interface.tun_name)" = "" ]; then
	uci -q batch <<-EOF >/dev/null
		set mqvpn.interface=interface
		set mqvpn.interface.tun_name=tun0
		commit mqvpn
	EOF
fi

# Raise the old shipped default log level 'error' to 'warn'.
#
# mqvpn logs the whole tunnel-establishment ladder at INFO ("Extended CONNECT
# sent" -> "tunnel 200 OK" -> "ADDRESS_ASSIGN" -> "TUN device tun0 created")
# and the deciding failure at WARN ("tunnel non-200 status=403", "tunnel
# malformed :status", "path stuck in PENDING"). At 'error' a router that never
# builds its tun records nothing but xquic's own connection-teardown report,
# which says "local close" even on a clean restart -- so a tunnel that fails
# between the QUIC handshake and the MASQUE address assignment leaves no usable
# evidence at all (issue 4367).
#
# 'warn' costs almost nothing: measured on a 3-WAN bench over ~3 minutes
# including one WAN flap, 4 WARN + 3 ERROR lines against 87 INFO lines.
#
# One-shot, marked in mqvpn.settings: a user who deliberately picks 'error'
# afterwards must not have it overridden again on the next package upgrade.
if [ "$(uci -q get mqvpn.settings.log_level_default_migrated)" != "1" ]; then
	[ "$(uci -q get mqvpn.interface.log_level)" = "error" ] && \
		uci -q set mqvpn.interface.log_level='warn'
	uci -q batch <<-EOF >/dev/null
		set mqvpn.settings=settings
		set mqvpn.settings.log_level_default_migrated='1'
		commit mqvpn
	EOF
fi

# OpenMPTCProuter routes the LAN into the tunnel and the VPS through the WANs
# itself (omr-tracker), so mqvpn no longer sets routes: its catch-alls into the
# tunnel took precedence over the default routes the tracker moves, and its
# metric 0 route to the VPS through the WAN it started on kept the proxy on
# that WAN when it failed upstream with its link up.
#
# One-shot, marked in mqvpn.settings: a user who turns them back on keeps them.
if [ "$(uci -q get mqvpn.settings.no_routes_default_migrated)" != "1" ]; then
	uci -q batch <<-EOF >/dev/null
		set mqvpn.interface.no_routes='1'
		set mqvpn.settings=settings
		set mqvpn.settings.no_routes_default_migrated='1'
		commit mqvpn
	EOF
fi

# mqvpn 0.18.0 removed the dgram reinjection mode and runs the old
# reinjection_mode = dgram as the default (idle) mode, which the page then
# could not show.
if [ "$(uci -q get mqvpn.multipath.reinjection_mode)" = "dgram" ]; then
	uci -q batch <<-EOF >/dev/null
		set mqvpn.multipath.reinjection_mode='default'
		commit mqvpn
	EOF
fi

exit 0
