#!/bin/bash

[ "$(uname -m)" == "aarch64" ] || exit

[[ -f /etc/os-release ]] && . /etc/os-release

function get_wan_address() {
	curl -4 --max-time 2 -s https://ipv4.whatismyip.akamai.com/
}

function get_wan6_address() {
	curl -6 --max-time 2 -s https://ipv6.whatismyip.akamai.com/
}

function get_ip_addresses() {
	local ipv4s=()
	local ipv4
	local address4
	local ipv6s=()
	local ipv6
	local address6
	local intf
	local f

	for f in /sys/class/net/*; do
		intf=$(basename "${f}")
		if [ "$intf" = "lo" ]; then
			continue
		fi

		ipv4=$(ip -4 addr show dev "${intf}")
		ipv4=$(echo "${ipv4}" | grep -v "${intf}:avahi")
		ipv4=$(echo "${ipv4}" | awk '/inet/ {print $2}')
		ipv4=$(echo "${ipv4}" | cut -d'/' -f1)
		ipv4=$(echo "${ipv4}" | awk '!x[$0]++')

		ipv6=$(ip -6 addr show dev "${intf}" | grep -v fe80)
		ipv6=$(echo "${ipv6}" | grep -v "${intf}:avahi")
		ipv6=$(echo "${ipv6}" | awk '/inet6/ {print $2}')
		ipv6=$(echo "${ipv6}" | cut -d'/' -f1)
		ipv6=$(echo "${ipv6}" | awk '!x[$0]++')

		for address4 in ${ipv4}; do
			ipv4s+=("${address4}")
		done
		for address6 in ${ipv6}; do
			ipv6s+=("${address6}")
		done
	done

	echo "${ipv4s[*]}|${ipv6s[*]}"
}

# Get other variables
ip_address=$(get_ip_addresses &)
wan_ip_address=$(get_wan_address &)
wan_ip6_address=$(get_wan6_address &)

# Get access point info
if systemctl is-active --quiet service hostapd && [[ -f /etc/hostapd/hostapd.conf ]]; then
	. /etc/hostapd/hostapd.conf
fi

TERM=linux toilet -f standard -F metal "WATTER Heater"

# Read RPI model from dt
BOARD_NAME=$(tr '\0' '\n' < /proc/device-tree/model | sed -E 's/ Rev [0-9.]+$//')

ARG1=$(xxd -p /proc/device-tree/chosen/bootloader/arg1)
case "$ARG1" in
	00000000)
		IOB="Aegis rev1"
		;;
	00000001)
		IOB="Squid"
		;;
	00000002)
		IOB="Aegis rev2"
		;;
	*)
		IOB="Unknown"
		;;
esac

IMAGE=$(head -1 /boot/issue.txt)
PKG=$(dpkg -s watter-collector  | grep Version | sed -e 's/.*: //')

printf "\e[0;90m Specs:  \x1B[0m"
echo ""
# Display version, board, and kernel version
echo -e " OS:           \e[0;92m${PRETTY_NAME}\x1B[0m"
echo -e " Board Name:   \e[0;92m${BOARD_NAME}\x1B[0m"
echo -e " Kernel:       \e[0;92m$(uname -r)\x1B[0m"
echo -e " IO Board:     \e[0;92m${IOB}\x1B[0m"
echo -e " Image:        \e[0;92m${IMAGE}\x1B[0m"
echo -e " WATTER:       \e[0;92mCollector v${PKG}\x1B[0m"

echo ""
printf "\e[0;90m Network:  \x1B[0m"
echo ""

# Display IP addresses
IFS='|' read -r ipv4s ipv6s <<< "${ip_address}"
# remove WAN IPv4 from the list of all IPv4
if [[ "${ipv4s}" == *${wan_ip_address}* ]]; then
        ipv4s=$(echo $ipv4s | sed "s/"${wan_ip_address}"//g" | xargs )
fi
if [[ -n ${ipv4s} || -n ${wan_ip_address} ]]; then
	all_ip_address=" IPv4:        "
	if [[ -n ${ipv4s} ]]; then
		all_ip_address+=" \x1B[93m(LAN)\x1B[0m "
	fi
	all_ip_address+="\x1B[92m${ipv4s// /, }\x1B[0m "
	if [[ -n ${wan_ip_address} ]]; then
		all_ip_address+="\x1B[93m(WAN)\x1B[0m \x1B[95m${wan_ip_address}\x1B[0m "
	fi
	echo -e "${all_ip_address}"
fi
# remove WAN IPv6 from the list of all IPv6
if [[ -n "${wan_ip6_address}" && "${ipv6s}" == *${wan_ip6_address}* ]]; then
	ipv6s=$(echo $ipv6s | sed "s/"${wan_ip6_address}"//g" | xargs )
fi

if [[ -n ${ipv6s} || -n ${wan_ip6_address} ]]; then
	all_ip6_address=" IPv6:         "
	if [[ -n ${ipv6s} ]]; then
		all_ip6_address+="\x1B[96m${ipv6s// /, }\x1B[0m "
	fi
	if [[ -n ${wan_ip6_address} ]]; then
		all_ip6_address+="\x1B[93m(WAN)\x1B[0m \x1B[95m${wan_ip6_address}\x1B[0m "
	fi
	echo -e "${all_ip6_address}"
fi

# Display hostapd
if [[ -n ${ssid} ]]; then
	echo -e " WiFi AP:      SSID: (\x1B[91m${ssid}\x1B[0m), $(iw dev "${interface:-}" info | grep channel | xargs)"
fi
