#!/usr/bin/bash

. '/etc/os-release'

CU='\033[4m'    # underline
C0='\033[0m'    # clear
C1='\033[31m'   # red
C2='\033[32m'   # green
C3='\033[33m'   # yellow
C4='\033[34m'   # blue

HOSTNAME=$(/usr/bin/hostname)
INTERFACES=($(/usr/bin/ip -oneline link show | /usr/bin/mawk -F ': ' '{sub(/@.*$/,"",$2); print $2}'))
UPLINK=()

/usr/bin/echo -e "\t${C3}Hostname:${C0}\t\t\t\t\t${HOSTNAME}"
for INTERFACE in "${INTERFACES[@]}"; do
  IP4=($(/usr/bin/ip -family 'inet' address show dev "${INTERFACE}" | /usr/bin/grep 'scope' | /usr/bin/mawk '{print $2}'))
  IP6=($(/usr/bin/ip -family 'inet6' address show dev "${INTERFACE}" | /usr/bin/grep 'scope global' | /usr/bin/mawk '{print $2}'))
  for IP4 in "${IP4[@]}"; do
    /usr/bin/echo -e "\t${C3}IPv4-Address [${INTERFACE}]:${C0}\t\t\t\t${IP4}"
  done
  for IP6 in "${IP6[@]}"; do
    /usr/bin/echo -e "\t${C3}IPv6-Address [${INTERFACE}]:${C0}\t\t\t\t${IP6}"
  done
  if [[ -n "${IP4}" ]] || \
     [[ -n "${IP6}" ]]; then
    UPLINK+=( "${INTERFACE}" )
  fi
  unset IP4
  unset IP6
done
for INTERFACE in "${UPLINK[@]}"; do
  BRTX=$(/usr/bin/networkctl status "${INTERFACE}" | /usr/bin/grep --word-regexp 'Bit Rate (Tx/Rx):' | /usr/bin/sed --expression='s/.*Bit Rate (Tx\/Rx):[[:space:]]//' --expression='s/\/.*//')
  BRRX=$(/usr/bin/networkctl status "${INTERFACE}" | /usr/bin/grep --word-regexp 'Bit Rate (Tx/Rx):' | /usr/bin/sed --expression='s/.*Bit Rate (Tx\/Rx):[[:space:]]//' --expression='s/.*\///')
  if [[ "${#INTERFACE}" -lt '4' ]]; then
    /usr/bin/echo -e "\t${C3}Bit Rate (Tx/Rx) [${INTERFACE}]:${C0}\t\t\t\t${BRTX} / ${BRRX}"
  else
    /usr/bin/echo -e "\t${C3}Bit Rate (Tx/Rx) [${INTERFACE}]:${C0}\t\t\t${BRTX} / ${BRRX}"
  fi
done
