# The Sway configuration file in ~/.config/sway/config calls this script.
# You should see changes to the status bar after saving this script.

# Uptime 
uptime_formatted=$(uptime | awk -F'( |,|:)+' '{d=h=m=0; if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0,"days,",h+0,"hours,",m+0,"minutes"}')
# Date
date_formatted=$(date "+%a %F %H:%M")

# Kernel Version 
linux_version=$(uname -r)

# Gamemode status
status=$(gamemoded -s)
if [ "$status" == "gamemode is inactive" ]; then
	gamemode=🧊
else
	gamemode=🔥
fi

# Volume
volume=$(pactl list sinks | grep '^[[:space:]]Volume:' | \
    head -n $(( $SINK + 1 )) | tail -n 1 | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,')
mute=$(pactl list sinks | grep '^[[:space:]]Mute:' | head -n $(( $sink + 1 )) | tail -n 1 | awk '{print $2}')

if [ "$mute" == "yes" ]; then
    volume_color='#f92672'
else
    volume_color='#ffffff'
fi

#bluetooth=$(bluetoothctl devices | cut -f2 -d' ' | while read uuid; do bluetoothctl info $uuid; done | grep -e "Name\|Connected: yes" | grep -B1 "yes" | head -n 1 | cut -d\  -f2-)
#bluetooth="🫐"

# Network usage
r1=`cat /sys/class/net/enp3s0/statistics/rx_bytes`
t1=`cat /sys/class/net/enp3s0/statistics/tx_bytes`
sleep 1
r2=`cat /sys/class/net/enp3s0/statistics/rx_bytes`
t2=`cat /sys/class/net/enp3s0/statistics/tx_bytes`
tx=`expr $t2 - $t1`
rx=`expr $r2 - $r1`
txmb=$(echo "scale = 1; $tx / 1280000" | bc | awk '{printf "%05.1f\n", $0}')
rxmb=$(echo "scale = 1; $rx / 1280000" | bc | awk '{printf "%05.1f\n", $0}')

# Vpn status
if [ -f ~/.wg0 ] ; then
    vpn="wg0"
else
    vpn="none"
fi

# Logitech mouse battery status
bat=$(cat /sys/class/power_supply/hidpp_battery_[0-9]*/capacity | head -n 1)
if [ $bat -lt 20 ]; then
    bat_color="#f92672"
else
    bat_color="#ffffff"
fi

#<span foreground='#c16b26'>lel</span>
echo -e "👍 $txmb 👎 $rxmb | 📡 $vpn | ⬆️ $uptime_formatted | 🔉<span foreground='$volume_color'>$volume%</span> | 🐁<span foreground='$bat_color'>$bat%</span> | $gamemode | 🐧 $linux_version | $date_formatted "
