#!/bin/bash

R=$'\033[1;31m'
G=$'\033[1;32m'
Y=$'\033[1;33m'
C=$'\033[1;36m'
DIM=$'\033[0;37m'
RST=$'\033[0m'
BOLD=$'\033[1m'
BGR=$'\033[41m'
BGG=$'\033[42m'
BLK=$'\033[0;30m'
BGC=$'\033[46m'

W=70

# -- Box char functions (printf hex — works on any locale) ---------------------
_TL() { printf '\xe2\x95\x94'; }  # +
_EQ() { printf '\xe2\x95\x90'; }  # -
_TR() { printf '\xe2\x95\x97'; }  # +
_ML() { printf '\xe2\x95\xa0'; }  # ¦
_MR() { printf '\xe2\x95\xa3'; }  # ¦
_VB() { printf '\xe2\x95\x91'; }  # ¦
_BL() { printf '\xe2\x95\x9a'; }  # +
_BR() { printf '\xe2\x95\x9d'; }  # +
_HD() { printf '\xe2\x94\x80'; }  # -

rep() {
    local i out=''
    for (( i=0; i<$2; i++ )); do out+="$1"; done
    printf '%s' "$out"
}

hline() { rep "$(_EQ)" $W; }

# Print a content line padded to W, with ¦ borders
bline() {
    local visible="$1" colored="${2:-$1}"
    local plain
    plain=$(printf '%s' "$visible" | sed 's/\x1b\[[0-9;]*m//g; s/\x1b[^a-zA-Z]*[a-zA-Z]//g')
    local vlen=${#plain}
    local pad=$(( W - vlen ))
    (( pad < 0 )) && pad=0
    local ps
    ps=$(rep ' ' $pad)
    printf "%b%s%b%s%s%b%s%b\n" "$C" "$(_VB)" "$RST" "$colored" "$ps" "$C" "$(_VB)" "$RST"
}

divline() {
    printf "%b%s%b%s%b%s%b\n" "$C" "$(_VB)" "$RST" "$(rep "$(_HD)" $W)" "$C" "$(_VB)" "$RST"
}

empty() {
    local ps
    ps=$(rep ' ' $W)
    printf "%b%s%b%s%b%s%b\n" "$C" "$(_VB)" "$RST" "$ps" "$C" "$(_VB)" "$RST"
}

# -- Gather interface data (eth*, eno*, bond* only, sorted) --------------------
declare -A _iface_ips
declare -a _iface_order
while read -r iface addr; do
    ip_only=$(echo "$addr" | cut -d'/' -f1)
    if [[ -z "${_iface_ips[$iface]}" ]]; then
        _iface_order+=("$iface")
    fi
    _iface_ips[$iface]+="${ip_only} "
done < <(ip -o -4 addr show \
    | awk '$2 ~ /^(eth|eno|bond)/ {print $2, $4}' \
    | sort)

# -- Gather IPMI data ----------------------------------------------------------
_IPMI_SUM=$(ipmicfg -summary 2>/dev/null)
IPMI_IP=$(echo "$_IPMI_SUM"  | grep "IPv4 Address"      | cut -d':' -f2 | xargs)
IPMI_MAC=$(echo "$_IPMI_SUM" | grep "BMC MAC"            | cut -d':' -f2- | xargs)
IPMI_FW=$(echo "$_IPMI_SUM"  | grep "Firmware Revision"  | cut -d':' -f2 | xargs)
IPMI_GW=$(ipmicfg -g 2>/dev/null | grep -i "gateway" | grep -v "#" | cut -d'=' -f2 | xargs)
IPMI_NM=$(ipmicfg -k 2>/dev/null | grep -i "subnet\|mask" | grep -v "#" | cut -d'=' -f2 | xargs)
# Convert netmask to CIDR if it looks like a dotted mask
if [[ "$IPMI_NM" =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
    IPMI_CIDR=$(python3 -c "import ipaddress; print(ipaddress.IPv4Network('0.0.0.0/${IPMI_NM}', strict=False).prefixlen)" 2>/dev/null)
    [ -n "$IPMI_CIDR" ] && IPMI_NM="${IPMI_NM}/${IPMI_CIDR}"
fi

# -- Draw box ------------------------------------------------------------------
printf '\n'
printf "%b%s%s%s%b\n" "$C" "$(_TL)" "$(hline)" "$(_TR)" "$RST"

# Title row with cyan background
title="  NETWORK & CONNECTIVITY"
tpad=$(rep ' ' $(( W - ${#title} )))
printf "%b%s%b%b%b%b%s%s%b%b%s%b\n" \
    "$C" "$(_VB)" "$RST" "$BGC" "$BLK" "$BOLD" "$title" "$tpad" "$RST" "$C" "$(_VB)" "$RST"

printf "%b%s%s%s%b\n" "$C" "$(_ML)" "$(hline)" "$(_MR)" "$RST"

# -- Each network interface ----------------------------------------------------
for iface in "${_iface_order[@]}"; do
    empty

    speed=$(ethtool "$iface" 2>/dev/null | grep "Speed"        | cut -d: -f2 | xargs)
    link=$( ethtool "$iface" 2>/dev/null | grep "Link detected" | cut -d: -f2 | xargs)
    mac=$(  cat "/sys/class/net/${iface}/address" 2>/dev/null)

    ip_count=$(echo "${_iface_ips[$iface]}" | wc -w)
    ip_label="(${ip_count} IP$([ "$ip_count" -ne 1 ] && echo s))"

    if [ -n "$speed" ]; then
        hdr_vis="  ${iface} ${ip_label}  |  Speed: ${speed}  Link: ${link}"
        hdr_col="  ${BOLD}${C}${iface}${RST} ${DIM}${ip_label}${RST}  ${DIM}|${RST}  Speed: ${Y}${speed}${RST}  Link: ${G}${link}${RST}"
    else
        hdr_vis="  ${iface} ${ip_label}"
        hdr_col="  ${BOLD}${C}${iface}${RST} ${DIM}${ip_label}${RST}"
    fi
    bline "$hdr_vis" "$hdr_col"

    # MAC
    bline "    MAC: ${mac}" "    ${DIM}MAC: ${mac}${RST}"

    # IPs
    for ip in ${_iface_ips[$iface]}; do
        bline "    ${ip}" "    ${BOLD}${ip}${RST}"
    done

    empty
    divline
done

# -- IPMI block ----------------------------------------------------------------
empty

if [ -z "$IPMI_IP" ]; then
    bline "  IPMI INFO:" "  ${BOLD}${C}IPMI INFO:${RST}"
    bline "    Could not determine IP" "    ${Y}Could not determine IP${RST}"
else
    bline "  IPMI INFO:" "  ${BOLD}${C}IPMI INFO:${RST}"
    bline "    FW:      ${IPMI_FW:-N/A}" "    ${G}FW:${RST}      ${Y}${IPMI_FW:-N/A}${RST}"
    bline "    IP:      ${IPMI_IP}" "    ${G}IP:${RST}      ${BOLD}${IPMI_IP}${RST}"
    bline "    GW:      ${IPMI_GW:-N/A}" "    ${G}GW:${RST}      ${DIM}${IPMI_GW:-N/A}${RST}"
    bline "    NETMASK: ${IPMI_NM:-N/A}" "    ${G}NETMASK:${RST} ${DIM}${IPMI_NM:-N/A}${RST}"
    bline "    MAC:     ${IPMI_MAC:-N/A}" "    ${G}MAC:${RST}     ${DIM}${IPMI_MAC:-N/A}${RST}"
fi

empty
printf "%b%s%s%s%b\n" "$C" "$(_BL)" "$(hline)" "$(_BR)" "$RST"
printf '\n'