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

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

CFG=/etc/board.json

# do not run on preinit/early init
[ "$EARLY_INIT" ] && return

# Adds modem supported features
add_modem_features() {
	local revision bands modem_func_id multi_apn operator_scan dhcp_filter dynamic_mtu ipv6 volte low_signal_reconnect auto_5g_mode red_cap
	revision="$(jsonfilter -s "$modem_info_from_unhandler" -qe '$.cache.firmware')"
	modem_func_id="$(jsonfilter -s "$default_modem_func" -e '@.id')"

	# Loads modem info
	json_set_namespace support old_cb
	json_load "$modem_info_from_unhandler"
	json_get_vars multi_apn operator_scan dhcp_filter dynamic_mtu ipv6 volte csd wwan_gnss_conflict framed_routing low_signal_reconnect auto_5g_mode red_cap verizon_disable_5g_sa
	json_get_values bands band_list
	json_set_namespace "$old_cb"

	# Sets modem supported feature values
	json_add_string revision "$revision"
	json_add_int modem_func_id "$modem_func_id"
	json_add_boolean multi_apn "$multi_apn"
	json_add_boolean operator_scan "$operator_scan"
	json_add_boolean dhcp_filter "$dhcp_filter"
	json_add_boolean dynamic_mtu "$dynamic_mtu"
	json_add_boolean ipv6 "$ipv6"
	json_add_boolean volte "$volte"
	json_add_boolean csd "$csd"
	json_add_boolean wwan_gnss_conflict "$wwan_gnss_conflict"
	json_add_boolean framed_routing "$framed_routing"
	json_add_boolean low_signal_reconnect "$low_signal_reconnect"
	json_add_boolean auto_5g_mode "$auto_5g_mode"
	json_add_boolean red_cap "$red_cap"
	json_add_boolean verizon_disable_5g_sa "$verizon_disable_5g_sa"

	# Adds supported band array
	json_is_a band_list array || {
		json_add_array band_list
		for band in $bands; do
			json_add_string "" "$band"
		done
		json_close_array
	}
}

#~ Get model name for RUTX products
setup_modem() {
	local key="$1"
	local object_num="$2"
	local id gps boudrate type desc control product vendor stop_bits devicename

	json_select "$object_num"
	json_get_vars id product

	if [ "$id" = "$id_from_unhandler" ]; then
		# Adds features for specific modem
		add_modem_features

		[ -z "$product" ] || {
			# Returns if no internal usb modems
			[ -f "/sys/bus/usb/devices/$id/idVendor" ] && [ -f "/sys/bus/usb/devices/$id/idProduct" ] || {
				json_select ..
				return 1
			}
		}

		vendor="$(cat "/sys/bus/usb/devices/$id/idVendor")"
		product="$(cat "/sys/bus/usb/devices/$id/idProduct")"

		[ -f "/lib/network/wwan/$vendor:$product" ] && {
			devicename="$id"

			json_set_namespace defaults old_cb
			json_load "$(cat /lib/network/wwan/$vendor:$product)"
			json_get_vars gps boudrate type desc control stop_bits
			json_set_namespace "$old_cb"

			[ "${devicename%%:*}" = "$devicename" ] && {
				json_add_string vendor "$vendor"
				json_add_string product "$product"
				json_add_string gps "$gps"
				json_add_string stop_bits "$stop_bits"
				json_add_string boudrate "$boudrate"
				json_add_string type "$type"
				json_add_string desc "$desc"
				json_add_string control "$control"
			}
		}
	fi
	json_select ..
}

[ -s "${CFG}" ] || exit 1

id_from_unhandler="$1"
modem_info_from_unhandler="$2"
default_modem_func="$3"

lock /var/run/board_modem.lock

chmod 664 /var/run/board_modem.lock
chown root:lock /var/run/board_modem.lock

board_config_update
json_for_each_item setup_modem modems

board_config_flush

lock -u /var/run/board_modem.lock

exit 0
