#!/bin/sh
# shellcheck disable=SC1091,SC3043
# This code is based on idea of https://github.com/egc112

WAN_INTERFACE='wan'
_ret='1'

insert_ip_rule() {
	local proto listen_port
	config_get proto "$1" proto
	config_get listen_port "$1" listen_port
	if [ "$proto" = 'wireguard' ] && [ -n "$listen_port" ]; then
		ip rule del sport "$listen_port" table "pbr_${WAN_INTERFACE}" >/dev/null 2>&1
		ip rule add sport "$listen_port" table "pbr_${WAN_INTERFACE}" >/dev/null 2>&1 && _ret=0
	fi
}

. /lib/functions.sh
config_load network
config_foreach insert_ip_rule 'interface'

return $_ret
