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

. /lib/overthebox

_usage() {
	printf "%s [options]\n" "$(basename "$0")"
	printf "\t%-12s %s\n" "-c|--colors" "colored output"
	printf "\t%-12s %s\n" "-f|--full" "output all informations"
	printf "\t%-12s %s\n" "-h|--help" "print this help"
	exit 0
}

# Options parsing
FULL=0
COLOR=0
while [ -n "$1" ]; do
	case $1 in
		-c|--color) shift; COLOR=1 ;;
		-f|--full)  shift; FULL=1   ;;
		*) _usage ;;
	esac
done

# Colors
COLOR_WHITE=
COLOR_GREEN=
COLOR_RED=
if [ "$COLOR" = 1 ]; then
	COLOR_GREEN='1;32'
	COLOR_RED='1;31'
fi

WIDTH=73
HEADERS="INTERFACE DEVICE STATUS PUBLIC_IP LATENCY"
DATA='if .l3_device then .l3_device else .device end, $connectivity, $public_ip, $latency'
if [ "$FULL" = "1" ]; then
	WIDTH=$((WIDTH + 18))
	DATA="$DATA, .metric, .ip4table"
	HEADERS="$HEADERS METRIC TABLE"
fi
TABLE_WIDTH=$((WIDTH + 2))

_print() {
	format="| %-15s | %-15s | \e[${1}m%-6s\e[m | %-15s | %-8s |"
	[ "$COLOR" = 0 ] && format="| %-15s | %-15s | %-6s | %-15s | %-8s |"
	[ "$FULL" = 1 ] && format="$format %-6s | %-6s |"
	shift
	# shellcheck disable=SC2059
	printf "$format\n" "$@"
}

_print_border() {
	printf "%${TABLE_WIDTH}s\n" "" | tr ' ' -
}

_print_separator() {
	printf "|%${WIDTH}s|\n" "" | tr ' ' -
}

_print_header() {
	# shellcheck disable=SC2086
	_print "$COLOR_WHITE" $HEADERS
}

_show_interface_status() {
	connectivity=$(otb_get_data "$1/connectivity")
	public_ip=$(otb_get_data "$1/public_ip")
	latency=$(otb_get_data "$1/latency")
	[ -z "$connectivity" ] && connectivity="-"
	[ -z "$public_ip" ] && public_ip="-"
	[ -z "$latency" ] && latency="-"

	status=$(ubus call "network.interface.$1" status | \
		jq -r --arg iface "$1" \
		      --arg connectivity "$connectivity" \
		      --arg public_ip "$public_ip" \
		      --arg latency "${latency}ms" \
				"[\$iface, $DATA | tostring] | join(\" \")")

	# shellcheck disable=SC2086
	case "$status" in
		*OK*)    _print "$COLOR_GREEN" $status;;
		*ERROR*) _print "$COLOR_RED"   $status;;
	esac
}

_show_status() {
	. /lib/functions.sh
	config_load network
	config_foreach _show_interface_status interface
}

_print_border
_print_header
_print_separator
_show_status | sort
_print_border
