#!/bin/sh

. /lib/functions.sh
. /usr/share/libubox/jshn.sh
. /lib/netmode/netmode_helpers.sh

source "/etc/device_info"

l3_network_pppoe_config() {
	logger -s -p user.info -t "netmode" "Generating L3 network configuration"

	# delete interfaces apart from lan, lan6, wan, wan6
	delete_extra_interfaces
	# delete existing vlan and macvlan sections to prevent clashes
	delete_vlan_and_macvlan_sections

	wandev="$(uci -q get network.WAN.ifname)"

	# Configure L3 Network Mode
	uci -q set network.lan=interface
	uci -q set network.lan.device='br-lan'
	uci -q set network.lan.proto='static'
	uci -q set network.lan.ipaddr='192.168.1.1'
	uci -q set network.lan.netmask='255.255.255.0'
	uci -q set network.lan.ip6assign='60'
	uci -q delete network.lan.vendorid
	uci -q delete network.lan.clientid
	uci -q delete network.lan.reqopts
	uci -q delete network.lan.sendopts

	uci -q delete network.lan6

	uci -q set network.wan=interface
	uci -q set network.wan.proto='pppoe'
	uci -q set network.wan.username="$NETMODE_username"
	uci -q set network.wan.password="$NETMODE_password"
	uci -q delete network.wan.disabled
	uci -q delete network.wan.ipaddr
	uci -q delete network.wan.gateway
	uci -q delete network.wan.netmask

	uci -q set network.wan6.disabled='1'

	if [ -n "$wandev" ] && echo "$NETMODE_vlanid" | grep -Eq '^[0-9]+$' && [ "$NETMODE_vlanid" -ge 1 ]; then
		uci -q set network.vlan_${NETMODE_vlanid}=device
		uci -q set network.vlan_${NETMODE_vlanid}.type="8021q"
		uci -q set network.vlan_${NETMODE_vlanid}.name="$wandev.$NETMODE_vlanid"
		uci -q set network.vlan_${NETMODE_vlanid}.ifname="$wandev"
		uci -q set network.vlan_${NETMODE_vlanid}.vid=$NETMODE_vlanid

		wandev="$wandev.$NETMODE_vlanid"
	fi

	uci -q set network.wan.device="$wandev"
	uci -q set network.wan6.device="$wandev"

	uci -q set network.WAN.mtu="$NETMODE_mtu"

	uci -q delete network.wan.dns
	if [ -n "$NETMODE_dns_servers" ]; then
		dns_servers="$(echo $NETMODE_dns_servers | tr ',' ' ')"
		for server in $dns_servers; do
		    uci -q add_list network.wan.dns=$server
		done
	fi

	uci -q delete network.br_lan.ports
	uci -q set network.br_lan.bridge_empty='1'

	add_port_to_br_lan() {
		port="$1"
		[ -n "$port" -a -d /sys/class/net/$port ] || continue
		uci add_list network.br_lan.ports="$port"
	}

	if [ -f /etc/board.json ]; then
		json_load_file /etc/board.json
		json_select network
		json_select lan
		if json_is_a ports array; then
			json_for_each_item add_port_to_br_lan ports
		else
			json_get_var device device
			[ -n "$device" ] && uci add_list network.br_lan.ports="$device"
		fi
		json_select ..
		json_cleanup
	fi

	uci -q commit network

	# Enable DHCP Server
	uci -q set dhcp.lan.ignore=0
	uci -q set dhcp.wan.ignore=1
	uci -q commit dhcp
	/etc/init.d/odhcpd enable

	# Enable SSDPD
	uci -q set ssdpd.ssdp.enabled="1"
	uci -q commit ssdpd

	# Update CWMP Agent WAN Interface
	uci -q set cwmp.cpe.default_wan_interface="wan"
	uci -q commit cwmp

	# Update gateway WAN Interface
	uci -q set gateway.global.wan_interface="wan"
	uci -q commit gateway

	# Enable firewall
	uci -q set firewall.globals.enabled="1"
	uci -q commit firewall
}

l3_network_pppoe_config
l3_mcast_config

# If device is already boot-up, assume netmode changed during runtime
if [ -f /var/run/boot_complete ]; then
	/etc/init.d/odhcpd restart 2>/dev/null
	for config in network dhcp ssdpd cwmp gateway firewall mcast; do
		ubus call uci commit "{\"config\":\"$config\"}"
		sleep 1
	done
fi
