#!/bin/sh

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

MAPFILE="/var/run/multiap/multiap.backhaul"

issue_discovery() {
	local iface="$1"

	ubus list ieee1905 > /dev/null 2>&1

	[ "$?" != 0 ] && return

	res=$(ubus -t2 call ieee1905 buildcmdu "{\"type\":0, \"ifname\":\"${iface}\"}")
	json_load "$res" > /dev/null 2>&1
	json_get_var data data

	[ "$data" == "" ] && return

	ubus -t1 wait_for ieee1905.al.$iface > /dev/null 2>&1
	[ "$?" != 0 ] && return

	ubus call ieee1905.al.$iface cmdu "{\"dst\":\"01:80:c2:00:00:13\", \"type\":0, \"data\":\"${data}\"}"
}

remove_from_bridge() {
	local bridge=$2

	config_get ifname "$section" ifname

	brctl delif $bridge $ifname
}

update_bstas() {
	local section="$1"
	local action="$2"
	local ifname onboarded

	config_get ifname "$section" ifname
	config_get_bool enabled "$section" enabled 0

	if [ "$action" = "down" ]; then
		wpa_cli -i "$ifname" disconnect > /dev/null 2>&1
		wpa_cli -i "$ifname" disable_network 0  > /dev/null 2>&1
#		wpa_cli -i "$ifname" save_config  > /dev/null 2>&1
	elif [ "$action" = "up" ]; then
		[ "$enabled" -eq 0 ] && return
		wpa_cli -i "$ifname" reconnect > /dev/null 2>&1
		wpa_cli -i "$ifname" enable_network 0  > /dev/null 2>&1
#		wpa_cli -i "$ifname" save_config  > /dev/null 2>&1
	fi
}

up() {
#	touch "$MAPFILE"
#	echo "$1" > "$MAPFILE"

	config_load "mapagent"
	bridge=$(uci get mapagent.agent.al_bridge)
	config_foreach remove_from_bridge bsta $bridge
	config_foreach update_bstas bsta down
}

down() {
	rm -f "$MAPFILE"
	config_load "mapagent"
	config_foreach update_bstas bsta up
}

find_bridge() {
	local ifname=$2

	config_get ports "$section" ports

	if [ "$(echo $ports | grep -w $ifname)" != "" ]; then
		config_get name "$section" name

		echo $name
		break
	fi
}

bridge_modif() {
	local action=$1
	local ifname=$2

	config_load "network"

	bridge=$(config_foreach find_bridge device $ifname)

	if [ "$bridge" != "" ]; then
		brctl ${action}if $bridge $ifname
	fi
}

bridge_addif() {
	local ifname=$1

	bridge_modif add $ifname
	issue_discovery $ifname
}

bridge_delif() {
	local ifname=$1

	bridge_modif del $ifname
}

persist_cntlr() {
	local mode=$(uci -q get mapagent.@controller_select[0].id)
	local brcm_setup="$(uci -q get mapagent.agent.brcm_setup)"

	agent_update_acs() {
		local section=$1

		config_get device ${section} device
		config_get type ${section} type
		config_get ifname ${section} ifname

		[ "$type" != "fronthaul" ] && return

		nvram unset ${ifname}_mode > /dev/null 2>&1

	}

	agent_disable_bsta() {
		local section=$1

		uci set mapagent.${section}.enabled="0"
	}

	wireless_disable_bsta() {
		local section=$1

		config_get mode ${section} mode
		config_get ifname ${section} ifname

		[ "$mode" != "sta" ] && return

		uci set wireless.${section}.disabled="1"
		wpa_cli -i $ifname disable 0
	}

	[ "$mode" == "auto" ] || return

	config_load "mapagent"
	config_foreach agent_disable_bsta bsta
	[ "$brcm_setup" = "1" ] && config_foreach agent_update_acs ap

	config_load "wireless"
	config_foreach wireless_disable_bsta wifi-iface
	uci commit wireless

	uci set mapagent.@controller_select[0].local="1"
	uci set mapagent.@controller_select[0].id="controller"
	uci commit mapagent
	kill -SIGHUP `pidof mapagent`
}


disable_cntlr() {
	local mode=$(uci -q get mapagent.@controller_select[0].id)

	[ "$mode" == "auto" ] || return

	uci set mapagent.@controller_select[0].local="0"
	uci set mapagent.@controller_select[0].autostart="0"
	uci set mapagent.@controller_select[0].id="disabled"
	uci commit mapagent
	kill -SIGHUP `pidof mapagent`
}

dhcp_discovery() {
	local bridge=$(uci get mapagent.agent.al_bridge)

	[ "$bridge" = "" ] && bridge="br-lan"

	udhcpc -qn -t 1 -T1 -i $bridge > /dev/null 2>&1

	echo "$?"
}

func=$1
shift

case "$func" in
	up) up $@;;
	down) down $@;;
	bridge_delif) bridge_delif $@;;
	bridge_addif) bridge_addif $@;;
	persist_cntlr) persist_cntlr $@;;
	disable_cntlr) disable_cntlr $@;;
	dhcp_discovery) dhcp_discovery $@;;
	*) exit 1;;
esac
