#!/bin/sh
# vim: set ft=sh noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 :

_add_mptcp_endpoint() {
	ip mptcp endpoint add "$@"
}

_delete_mptcp_endpoint() {
	id=
	id=$(ip mptcp endpoint | grep "$1" | awk '{print $3}')
	[ -z "$id" ] && return 1

	ip mptcp endpoint delete id "$id"
}

# No need to execute script if MPTCP is disabled
multipath_global_config=$(uci -q get "network.globals.multipath" || echo "disable")
[ "$multipath_global_config" != "enable" ] && exit 0

# Get the current multipath status
multipath_status="off"
case $(ip mptcp endpoint show | grep "$OTB_TRACKER_DEVICE") in
	*subflow*)      multipath_status="on"       ;;
	*backup*)       multipath_status="backup"   ;;
esac

# An interface in error will never be used in MPTCP
if [ "$OTB_TRACKER_STATUS" = "ERROR" ]; then
	[ "$multipath_status" = "off" ] && exit 0
	_log "$OTB_TRACKER_INTERFACE switched off"
	_delete_mptcp_endpoint "$OTB_TRACKER_DEVICE"
	exit 0
fi

multipath_config=$(uci -q get "network.$OTB_TRACKER_INTERFACE.multipath" || echo "off")
[ "$multipath_config" = "master" ] || [ "$multipath_config" = "handover" ] && multipath_config="on"
[ "$multipath_status" = "$multipath_config" ] && exit 0

# Delete old endpoint
[ $multipath_status != "off" ] && {
	_delete_mptcp_endpoint "$OTB_TRACKER_DEVICE"
}

# Get ipv4 address
[ -z "$OTB_TRACKER_DEVICE_IP" ] && {
	_log "failed to retrieve ip address of $OTB_TRACKER_INTERFACE"
	exit 0
}

_log "$OTB_TRACKER_INTERFACE switched to $multipath_config"

# shellcheck disable=SC2086
case $multipath_config in
	on) _add_mptcp_endpoint $OTB_TRACKER_DEVICE_IP dev $OTB_TRACKER_DEVICE subflow signal ;;
	backup) _add_mptcp_endpoint $OTB_TRACKER_DEVICE_IP dev $OTB_TRACKER_DEVICE backup ;;
esac
