#!/bin/sh

top -ibn5 -d1 | while read -r line; do
	new_cpu=$(printf "%s" "$line" | sed -nE 's_%Cpu\(s\):[[:space:]]*(.*) us,.*_\1 %_p')
	new_mem=$(printf "%s" "$line" | sed -nE 's_MiB Mem :[[:space:]]*(.*) total,.*,[[:space:]]*(.*) used,.*_\2 MB / \1 MB_p')
	cpu=${new_cpu:-$cpu}
	mem=${new_mem:-$mem}
	[ -f "/tmp/yt-music/current" ] && . /tmp/yt-music/current
	if [ -n "$new_mem" ]; then

		temp=$(cat /sys/class/thermal/thermal_zone0/temp)
		: $((temp /= 1000))
		bat_stats=$(cat /sys/class/power_supply/BAT0/status)
		vol_stats=$(wpctl get-volume @DEFAULT_AUDIO_SINK@)
		charge=$(cat /sys/class/power_supply/BAT0/capacity)
		[ "$bat_stats" = "Discharging" ] && { [ "$charge" -lt 20 ] && bat_icon=🪫 || bat_icon=🔋; } || bat_icon=🔌
		printf '%s' "$vol_stats" | grep -q 'MUTED' && vol_icon=🔇 || vol_icon=🔊
		notify-send -e -i "lol" "<----------(STATS)---------->" "🧠 CPU usage : $cpu\n🌡️ Cpu Temp : $temp °C\n🔳 RAM : $mem\n$bat_icon Battery : $charge (${bat_stats})\n☀️ Brightness : $("$(dirname "$0")"/brightness s)%\n$vol_icon Volume : $(printf '%s' "$vol_stats" | tr -cd '[0-9]' | sed 's|^0||g')%\n🎵 $SONG - $ARTIST" -h "string:x-canonical-private-synchronous:${0##*/}" -t 1500 &
	fi
done
