#!/usr/bin/env bash
# --- STAGE 1: THE INSTANT DATA ---
# Gather time, workspace, and hardware stats immediately
time_info=$(date +"%H:%M | %A, %d %b")
workspace_id=$(hyprctl activeworkspace -j | jq -r '.id')
cpu_load=$(top -bn1 | grep "Cpu(s)" | sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | awk '{print 100 - $1"%"}')
mem_usage=$(free -h | awk '/^Mem:/ {print $3 "/" $2}')
batt_pct=$(cat /sys/class/power_supply/BAT0/capacity 2>/dev/null || echo "0")
batt_stat=$(cat /sys/class/power_supply/BAT0/status 2>/dev/null)
batt_icon=$([ "$batt_stat" = "Charging" ] && echo "" || echo "")
# Send first notification immediately (ID 9998)
notify-send -r 9998 -t 10000 " $time_info" \
"Workspace: $workspace_id
CPU: $cpu_load Mem: $mem_usage Bat: $batt_icon $batt_pct%"
# --- STAGE 2: THE NETWORK & UPDATES ---
# This part will run in the background and pop up once data is gathered
{
# Original Networking Logic
net_ssid=$(nmcli -t -f active,ssid dev wifi | grep '^yes' | cut -d: -f2)
# If Wifi is empty, check for Wired
if [ -z "$net_ssid" ]; then
net_ssid=$(nmcli device | grep "ethernet" | grep "connected" | awk '{print "Wired "}')
fi
# Wireguard status
wg0=$(ip addr show wg0 2>/dev/null | grep -q "inet" && echo " wg0 " || echo "")
wg1=$(ip addr show wg1 2>/dev/null | grep -q "inet" && echo " wg1 " || echo "")
vpn_status="${wg0}${wg1}"
[ -z "$vpn_status" ] && vpn_status="No VPN"
# Updates (Arch Repo + Flatpak)
arch_updates=$(checkupdates 2>/dev/null | wc -l)
fp_updates=$(flatpak remote-ls --updates 2>/dev/null | wc -l)
total_upd=$((arch_updates + fp_updates))
upd_text=$([ "$total_upd" -gt 0 ] && echo " $total_upd updates available" || echo " System up-to-date")
# Send second notification (ID 9999)
notify-send -r 9999 -t 10000 " Network & Updates" \
"Net: ${net_ssid:-Disconnected}
VPN: $vpn_status
Updates: $upd_text"
} &