#!/bin/sh
#
# Copyright (c) 2015 The Linux Foundation. All rights reserved.
# Copyright (c) 2011-2015 OpenWrt.org
#


. /lib/functions.sh
. /lib/functions/uci-defaults.sh
. /lib/functions/teltonika-defaults.sh
. /lib/functions/system.sh

[ -f /lib/functions/target-defaults.sh ] && \
	. /lib/functions/target-defaults.sh


set_ports() {
	local lan_list="$1"
	local count_from_zero="${2:-true}"
	local i=1
	for e in $lan_list; do
		local type num block parity pos
		case "$e" in
			port*) type="eth"; num="${e##port}"; i=$((i + 1)); block=0; parity=1 ;;
			sfp*)
				type="sfp"
				if "$count_from_zero"; then
					num="${e##sfp}";
				else
					num=$i;
					i=$((i + 1));
				fi
				block=1
				parity=0
				;;
			*) json_add_string "" "$e"; continue ;;
		esac
		[ "$((num % 2))" -eq "$parity" ] && pos="up" || pos="down"
		ucidef_set_port "static" device "name:${e} type:${type} num:${num} position:${pos} block:${block}" protocol static
		json_add_string "" "$e"
	done
}

setup_json() {
	local model="$1"
	lan_list=$(ls -1 -v -d /sys/class/net/port* /sys/class/net/sfp* | xargs -n1 basename | xargs)

	case "$model" in
	SWM28*)
		if [ "${model:5:1}" = "0" ]; then
			ucidef_set_poe "lan1:port1:4:30000" "lan2:port2:4:30000" "lan3:port3:4:30000" "lan4:port4:4:30000" "lan5:port5:4:30000" "lan6:port6:4:30000" "lan7:port7:4:30000" "lan8:port8:4:30000" "lan9:port9:4:30000" "lan10:port10:4:30000" "lan11:port11:4:30000" "lan12:port12:4:30000"
			ucidef_set_poe_reg "305000/30000/4/dummy"
			ucidef_set_hwinfo ethernet sfp_port sfp_switch poe switch port_link
		elif [ "${model:5:1}" = "1" ]; then
			ucidef_set_hwinfo ethernet sfp_port sfp_switch switch
		elif [ "${model:5:1}" = "2" ]; then
			ucidef_set_poe "lan1:port1:4:30000" "lan2:port2:4:30000" "lan3:port3:4:30000" "lan4:port4:4:30000" "lan5:port5:4:30000" "lan6:port6:4:30000" "lan7:port7:4:30000" "lan8:port8:4:30000" "lan9:port9:4:30000" "lan10:port10:4:30000" "lan11:port11:4:30000" "lan12:port12:4:30000" "lan13:port13:4:30000" "lan14:port14:4:30000" "lan15:port15:4:30000" "lan16:port16:4:30000" "lan17:port17:4:30000" "lan18:port18:4:30000" "lan19:port19:4:30000" "lan20:port20:4:30000" "lan21:port21:4:30000" "lan22:port22:4:30000" "lan23:port23:4:30000" "lan24:port24:4:30000"
			ucidef_set_poe_reg "305000/30000/4/dummy"
			ucidef_set_hwinfo ethernet sfp_port sfp_switch poe switch
		fi

		json_select_object network
		json_select_object static
		json_select_object ..
		json_select_object ..

		set_ports "$lan_list" false
		;;
	TSW202*)
		ucidef_set_poe "lan1:port1:4:30000" "lan2:port2:4:30000" "lan3:port3:4:30000" "lan4:port4:4:30000" "lan5:port5:4:30000" "lan6:port6:4:30000" "lan7:port7:4:30000" "lan8:port8:4:30000"
		ucidef_set_poe_reg "245000/30000/4/dummy"
		ucidef_set_max_multicast_groups "5120" # 512 per port
		ucidef_set_hwinfo ethernet sfp_port sfp_switch poe switch port_link

		json_select_object network
		json_select_object static
		json_select_object ..
		json_select_object ..

		set_ports "$lan_list"
		;;
	TSW212*)
		ucidef_set_max_multicast_groups "5120" # 512 per port
		ucidef_set_hwinfo ethernet sfp_port sfp_switch switch port_link

		set_ports "$lan_list"
		;;
	TSW213*)
		ucidef_set_release_version "1.7"
		ucidef_set_max_multicast_groups "5120" # 512 per port
		ucidef_set_hwinfo ethernet sfp_port sfp_switch switch ios port_link

		set_ports "$lan_list"
		;;
	TSW223*)
		ucidef_set_release_version "1.8"
		ucidef_set_poe "lan1:port1:8:90000" "lan2:port2:8:90000" "lan3:port3:4:30000" "lan4:port4:4:30000" "lan5:port5:4:30000" "lan6:port6:4:30000" "lan7:port7:4:30000" "lan8:port8:4:30000"
		ucidef_set_poe_reg "245000/30000/8/dummy"
		ucidef_set_max_multicast_groups "5120" # 512 per port
		ucidef_set_hwinfo ethernet sfp_port sfp_switch switch ios poe port_link

		set_ports "$lan_list"
		;;
	*)
		echo "Unsupported hardware. Network interfaces not intialized"
		;;
	esac

	type ucidef_target_defaults &> /dev/null && \
		ucidef_target_defaults "$model"
}

board=$(board_name)
board_config_update

ucidef_set_bridge_device br0

lan_mac="$(mtd_get_mac_binary config 0x0)"
lan_mac_end=""
label_mac=""

ucidef_set_interface_macaddr "static" $lan_mac
ucidef_set_bridge_mac "$lan_mac"
ucidef_set_label_macaddr $lan_mac
ucidef_set_interface "static" "default_ip" %%DEFAULT_IP%%

platform="$(cat /proc/device-tree/platform)" 2>/dev/null
model="$(mnf_info --name)" 2>/dev/null;

ucidef_add_port_security_capabilities 1 1 "hardware"

setup_json "$model"

ucidef_set_board_platform "$platform"

board_config_flush

exit 0

