summaryrefslogtreecommitdiff
path: root/polybar/scripts/wg-pc-status.sh
blob: b708695836882bc97f34c3eeacd238b0a9b9bac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/usr/bin/env bash
set -euo pipefail

iface="${WG_QUICK_PROFILE:-PC}"

age_label() {
  local latest now age

  latest="$(wg show "$iface" latest-handshakes 2>/dev/null | awk 'NF { if ($2 > newest) newest = $2 } END { print newest + 0 }')"
  if [ -z "$latest" ] || [ "$latest" -le 0 ] 2>/dev/null; then
    return 0
  fi

  now="$(date +%s)"
  age=$((now - latest))

  if [ "$age" -lt 60 ]; then
    printf ' %ss' "$age"
  elif [ "$age" -lt 3600 ]; then
    printf ' %sm' "$((age / 60))"
  elif [ "$age" -lt 86400 ]; then
    printf ' %sh' "$((age / 3600))"
  else
    printf ' %sd' "$((age / 86400))"
  fi
}

if [ -d "/sys/class/net/$iface" ]; then
  echo "%{F#98971a}up$(age_label)%{F-}"
else
  echo "%{F#cc241d}down%{F-}"
fi