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

set -e
set -o pipefail

. /lib/functions.sh
. /lib/functions/network.sh
. /lib/overthebox

[ "$OTB_SERVICE_ID" ]

INTERFACES="[]"
MOUNTS="[]"

_get_interface_json() {
	local ipaddr gateway subnet asn
	network_get_ipaddr ipaddr "$1"
	network_get_gateway gateway "$1"
	network_get_subnet subnet "$1"

	asn="$(otb_get_data "$1/asn" | jq -c '{
		country: .as_country_code,
		description: .as_description,
		number: .as_number,
	}')"
	[ "$asn" ] || asn="null"

	# Retrieve the netmask from the subnet
	[ "$subnet" ] && eval "$(ipcalc.sh "$subnet" | grep NETMASK)"

	local device label multipath
	config_get device "$1" device
	config_get label "$1" label
	config_get multipath "$1" multipath

	ubus call "network.interface.$1" status | jq -c \
		--argjson asn "$asn" \
		--arg name "$1" \
		--arg ipaddr "$ipaddr" \
		--arg netmask "$NETMASK" \
		--arg gateway "$gateway" \
		--arg device "$device" \
		--arg public_ip "$(otb_get_data "$1/public_ip")" \
		--arg _label "$label" \
		--arg multipath "$multipath" '{
			as: $asn,
			ip: $ipaddr,
			netmask: $netmask,
			gateway: $gateway,
			public_ip: $public_ip,
			dns_servers: ."dns-server",
			name: $name,
			label: $_label,
			device: $device,
			multipath_status: $multipath,
		}'
}

_get_interface() {
	interface=$(_get_interface_json "$1")
	INTERFACES=$(jq -n -c \
		--argjson interfaces "$INTERFACES" \
		--argjson interface "$interface" \
		'$interfaces + [$interface]')
	otb_debug "$(printf "\\nInterface $1 properties:\\n%s" "$(otb_pretty_print "$interface")")"
}

_get_mounts() {
	while read -r dev mount_point fs options _; do
		MOUNTS=$(jq -n -c \
			--argjson mounts "$MOUNTS" \
			--arg dev "$dev" \
			--arg mount_point "$mount_point" \
			--arg fs "$fs" \
			--arg options "$options" \
			'$mounts + [{
				device: $dev,
				mount_point: $mount_point,
				fs: $fs,
				options: $options,
			}]')
	done < /proc/mounts
	otb_debug "$(printf "\\nMounts :\\n%s" "$(otb_pretty_print "$MOUNTS")")"
}

config_load network
config_foreach _get_interface interface ret

_get_mounts

ubus call system board | jq -c \
	--argjson interfaces "$INTERFACES" \
	--argjson mounts "$MOUNTS" \
	--arg macaddr "$(cat /sys/class/net/eth0/address)" '{
		interfaces: $interfaces,
		mounts: $mounts,
		release: .release,
		board: (del(.release) + {mac_addr: $macaddr})
} | tojson'| otb_device_post properties -d@-
