#!/bin/sh

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/mwan3/mwan3.sh

[ "$ACTION" == "ifup" -o "$ACTION" == "ifdown" ] || exit 1
[ -n "$INTERFACE" ] || exit 2

if [ "$ACTION" == "ifup" ]; then
        [ -n "$DEVICE" ] || exit 3
fi

mwan3_set_connected_iptables

config_load mwan3
config_get enabled $INTERFACE enabled 0
[ "$enabled" == "1" ] || exit 0

if [ "$ACTION" == "ifup" ]; then
	config_get family $INTERFACE family ipv4
	if [ "$family" = "ipv4" ]; then
		ubus call network.interface.${INTERFACE}_4 status &>/dev/null
		if [ "$?" -eq "0" ]; then
			network_get_gateway gateway ${INTERFACE}_4
		else
			network_get_gateway gateway $INTERFACE
		fi
	elif [ "$family" = "ipv6" ]; then
		ubus call network.interface.${INTERFACE}_6 status &>/dev/null
		if [ "$?" -eq "0" ]; then
			network_get_gateway6 gateway ${INTERFACE}_6
		else
			network_get_gateway6 gateway ${INTERFACE}
		fi
	fi

	[ -n "$gateway" ] || exit 9
fi

$LOG notice "$ACTION interface $INTERFACE (${DEVICE:-unknown})"

case "$ACTION" in
	ifup)
		mwan3_set_general_rules
		mwan3_set_general_iptables
		mwan3_create_iface_rules $INTERFACE $DEVICE
		mwan3_create_iface_iptables $INTERFACE $DEVICE
		mwan3_create_iface_route $INTERFACE $DEVICE
		mwan3_track $INTERFACE $DEVICE
		mwan3_set_policies_iptables
		mwan3_set_user_rules
		mwan3_flush_conntrack $INTERFACE $DEVICE "ifup"
	;;
	ifdown)
		mwan3_delete_iface_rules $INTERFACE
		mwan3_delete_iface_iptables $INTERFACE
		mwan3_delete_iface_route $INTERFACE
		mwan3_delete_iface_ipset_entries $INTERFACE
		mwan3_track_signal $INTERFACE $DEVICE
		mwan3_set_policies_iptables
		mwan3_set_user_rules
		mwan3_flush_conntrack $INTERFACE $DEVICE "ifdown"
	;;
esac

exit 0
