#!/bin/bash

# omarchy:summary=Print active network status for the shell
# omarchy:group=network
# omarchy:args=[--verbose]

verbose=false
internet_probe=1.1.1.1

case "${1:-}" in
  "")
    ;;
  --verbose)
    verbose=true
    ;;
  *)
    echo "Usage: omarchy-network-status [--verbose]" >&2
    exit 2
    ;;
esac

print_status() {
  local device nm state ssid signal freq

  device=$(ip route get "$internet_probe" 2>/dev/null | awk '{ for (i = 1; i <= NF; i++) if ($i == "dev") { print $(i + 1); exit } }')

  if [[ -z $device ]]; then
    printf 'disconnected\t\t\t\n'
    return
  fi

  if [[ ! -d /sys/class/net/$device/wireless ]]; then
    printf 'ethernet\t%s\t\t\n' "$device"
    return
  fi

  nm=$(nmcli -t -f GENERAL.STATE,GENERAL.CONNECTION dev show "$device" 2>/dev/null)
  state=$(awk -F: '$1 == "GENERAL.STATE" { print $2; exit }' <<<"$nm")
  ssid=$(awk -F: '$1 == "GENERAL.CONNECTION" { print $2; exit }' <<<"$nm")
  signal=$(nmcli -t -f IN-USE,SIGNAL dev wifi list ifname "$device" --rescan no 2>/dev/null | awk -F: '$1 == "*" { print $2; exit }')
  freq=$(iw dev "$device" link 2>/dev/null | awk '/freq:/ { print $2; exit }')

  if [[ $state != 100* ]]; then
    printf 'disconnected\t\t\t\n'
    return
  fi

  printf 'wifi\t%s\t%s\t%s\n' "${ssid:-$device}" "$signal" "$freq"
}

ping_latency_ms() {
  local host=$1

  LC_ALL=C ping -n -c 1 -W 1 "$host" 2>/dev/null | awk -F'time[=<]' '/time[=<]/ { split($2, parts, " "); print parts[1]; exit }'
}

print_ping_samples() {
  local gateway=$1
  local tmpdir router_file internet_file
  local router_pid="" internet_pid=""

  omarchy-cmd-present ping || return
  tmpdir=$(mktemp -d) || return
  router_file="$tmpdir/router"
  internet_file="$tmpdir/internet"

  if [[ -n $gateway ]]; then
    ping_latency_ms "$gateway" >"$router_file" &
    router_pid=$!
  fi

  ping_latency_ms "$internet_probe" >"$internet_file" &
  internet_pid=$!

  if [[ -n $router_pid ]]; then
    wait "$router_pid"
    printf 'router_ping_ms\t%s\n' "$(cat "$router_file")"
  fi

  wait "$internet_pid"
  printf 'internet_ping_ms\t%s\n' "$(cat "$internet_file")"
  rm -rf "$tmpdir"
}

print_verbose() {
  local route_json iface gw src prefix link

  route_json=$(ip -j route get "$internet_probe" 2>/dev/null)
  [[ -z $route_json ]] && return

  iface=$(jq -r '.[0].dev // ""' <<<"$route_json" 2>/dev/null)
  gw=$(jq -r '.[0].gateway // ""' <<<"$route_json" 2>/dev/null)
  src=$(jq -r '.[0].prefsrc // ""' <<<"$route_json" 2>/dev/null)

  [[ -z $iface ]] && return

  prefix=$(ip -j addr show "$iface" 2>/dev/null | jq -r '.[0].addr_info[]? | select(.family == "inet") | .prefixlen // ""' 2>/dev/null | head -n 1)

  printf 'iface\t%s\n' "$iface"
  printf 'ip\t%s\n' "$src"
  printf 'prefix\t%s\n' "$prefix"
  printf 'gateway\t%s\n' "$gw"

  if [[ -r /sys/class/net/$iface/statistics/rx_bytes ]]; then
    printf 'rx_bytes\t%s\n' "$(cat /sys/class/net/$iface/statistics/rx_bytes)"
  fi
  if [[ -r /sys/class/net/$iface/statistics/tx_bytes ]]; then
    printf 'tx_bytes\t%s\n' "$(cat /sys/class/net/$iface/statistics/tx_bytes)"
  fi

  if [[ -d /sys/class/net/$iface/wireless ]]; then
    printf 'type\twifi\n'

    if omarchy-cmd-present iw; then
      link=$(iw dev "$iface" link 2>/dev/null)

      if [[ -n $link ]]; then
        printf 'ssid\t%s\n' "$(awk '/SSID:/ { sub(/.*SSID: /, ""); print; exit }' <<<"$link")"
        printf 'signal_dbm\t%s\n' "$(awk '/signal:/ { print $2; exit }' <<<"$link")"
        printf 'freq\t%s\n' "$(awk '/freq:/ { print $2; exit }' <<<"$link")"
        printf 'bitrate\t%s %s\n' "$(awk '/tx bitrate:/ { print $3; exit }' <<<"$link")" "$(awk '/tx bitrate:/ { print $4; exit }' <<<"$link")"
      fi
    fi
  else
    printf 'type\tethernet\n'
    [[ -r /sys/class/net/$iface/speed ]] && printf 'speed\t%s\n' "$(cat /sys/class/net/$iface/speed)"
    [[ -r /sys/class/net/$iface/duplex ]] && printf 'duplex\t%s\n' "$(cat /sys/class/net/$iface/duplex)"
  fi

  print_ping_samples "$gw"
}

if [[ $verbose == "true" ]]; then
  print_verbose
else
  print_status
fi
