#!/usr/bin/env bash
# Network status / toggle via NetworkManager.

case "${1:-status}" in
  toggle)
    if [ "$(nmcli radio wifi)" = enabled ]; then nmcli radio wifi off
    else nmcli radio wifi on; fi
    ;;
  status|*)
    line=$(nmcli -t -f NAME,TYPE,DEVICE connection show --active 2>/dev/null \
             | grep -v ':loopback' | head -1)
    name=$(printf '%s' "$line" | cut -d: -f1)
    type=$(printf '%s' "$line" | cut -d: -f2)
    icon="󰖪"; label="Offline"
    if [ -n "$name" ]; then
      case "$type" in
        *wireless*) icon="󰖩"; label="$name";;
        *ethernet*) icon="󰈀";  label="$name";;
        *)          icon="󰈀";  label="$name";;
      esac
    fi
    jq -nc --arg i "$icon" --arg l "$label" '{icon:$i,label:$l}'
    ;;
esac
