#!/bin/sh
# shellcheck shell=dash
# shellcheck disable=SC1091,SC2039
# vim: set noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 :

. /lib/functions.sh

[ "$(uci -q get "network.lan.proto")" = static ] || \
	uci -q batch <<-EOF
	set network.lan=interface
	set network.lan.proto=static
	set network.lan.ipaddr=192.168.100.1
	set network.lan.netmask=255.255.255.0
	EOF

uci -q batch <<EOF
delete network.none
delete network.if6rd
delete network.wan6
reorder network.loopback=0
reorder network.globals=1
reorder network.lan=2
set network.globals.multipath=enable
EOF

# Delete tun0 and xtun0 and let the configure get the right configuration
uci -q batch <<EOF
delete network.tun0
delete network.xtun0
EOF

# Set the ip rule for the lan with a pref of 100
uci -q show network.lan_rule >/dev/null || \
	uci -q batch <<-EOF
	set network.lan_rule=rule
	set network.lan_rule.lookup=lan
	set network.lan_rule.priority=100
	EOF

if [ "$(uci -q get network.vpn0.proto)" = "none" ]; then
	uci -q delete network.vpn0
fi

_setup_multipath() {
	uci -q get "network.$1.multipath" >/dev/null && return
	uci -q set "network.$1.multipath=$2"
}

_setup_macaddr() {
	uci -q get "network.$1_dev.macaddr" >/dev/null && return
	uci -q set "network.$1_dev.macaddr=$2"
}

_setup_dhcp() {
	uci -q get "network.$1.ipaddr" >/dev/null && return
	uci -q set "network.$1.proto=dhcp"
}

_setup_macvlan() {
	uci -q get "network.$1_dev.device" >/dev/null && return

	# do not create a macvlan for a VLAN interface
	local _device
	_device=$(uci -q get "network.$1.device")
	case "$_device" in
	eth*.*) return ;;
	esac

	# do not create a macvlan if the lan is not a macvlan
	[ "$1" = "lan" ] && [ "$_device" != "lan" ] && return

	uci -q batch <<-EOF
	set network.$1_dev=device
	set network.$1_dev.name=$1
	set network.$1_dev.type=macvlan
	set network.$1_dev.device=eth0
	set network.$1.device=$1
	EOF
	_macaddr=$(uci -q get "network.$1.macaddr")
	_setup_macaddr "$1" "${_macaddr:-auto$(date +%s)}"
}

_fix_old_vlan() {
	case "$1" in
	vlan?*)
		uci -q delete "network.$1"
		return
		;;
	esac

	local _type
	config_get _type "$1" type
	[ "$_type" = "macvlan" ] || return

	local _iface
	config_get _iface "$1" interface

	case "$_iface" in
	eth*.*)
		uci -q batch <<-EOF
		delete network.$1.type
		delete network.$1.interface
		delete network.$1.macaddr
		set network.$1.device=$_iface
		EOF
		;;
	esac
}
config_load network
config_foreach _fix_old_vlan interface

_rename_to_ifX() {
	case "$1" in
	loopback|lan|if*|cif*|wan*|*tun*)
		return
		;;
	esac

	local _type
	config_get _type "$1" type
	[ "$_type" = "macvlan" ] || return

	local _id=1
	while uci -q get "network.if$_id" >/dev/null; do
		_id=$((_id+1))
	done

	uci -q batch <<-EOF
	set network.$1.label=$1
	del_list firewall.wan.network=$1
	rename network.$1=if$_id
	EOF
}
config_load network
config_foreach _rename_to_ifX interface

_setup() {
	# Disable ipv6 on all interfaces except for loopback
	[ "$1" != "loopback" ] && {
			uci -q set "network.$1.ipv6=0"
			uci commit
		}
	[ "$1" = "loopback" ] && {
		uci -q set "network.$1.ipv6=1"
		uci commit
	}

	case "$1" in
	wwan?)
		;;
	wan?6)
		uci -q batch <<-EOF
		delete network.$1
		EOF
		;;
	wan?*)
		uci -q batch <<-EOF
		set network.$1.metric=${1#wan}
		set network.$1.ip4table=$((200+${1#wan}))
		del_list firewall.wan.network=$1
		add_list firewall.wan.network=$1
		EOF
		_setup_multipath "$1" on
		_setup_dhcp "$1"
		;;
	if0)
		proto=$(uci -q get "network.$1.proto")
		[ "$proto" = "none" ] || proto="dhcp"
		uci -q batch <<-EOF
		set network.$1.proto=$proto
		set network.$1.metric=2000
		del_list firewall.wan.network=$1
		add_list firewall.wan.network=$1
		EOF
		_setup_multipath "$1" off
		_setup_macvlan "$1"
		_setup_macvlan lan
		;;
	if?*)
		uci -q batch <<-EOF
		set network.$1.metric=${1#if}
		set network.$1.ip4table=$((200+${1#if}))
		del_list firewall.wan.network=$1
		add_list firewall.wan.network=$1
		EOF
		_setup_multipath "$1" on
		_setup_macvlan "$1"
		;;
	*)
		_setup_multipath "$1" off
		;;
	esac
}
config_load network
config_foreach _setup interface

# Add the lan as a named routing table
if ! grep -s -q "lan" /etc/iproute2/rt_tables; then
	echo "50 lan" >> /etc/iproute2/rt_tables
fi
uci -q set network.lan.ip4table='lan'
