#!/bin/bash

div="%{F#0092ff}<>%{F-}%{B-}"

Clock() {
DATETIME=$(date "+%a %b %d, %T")

echo -n "$DATETIME"
}

cpuTemp() {

CPUTEMP=$(sensors k10temp-pci-00c3 | grep Tdie | awk '{print $2}' | grep -o [0-9][0-9]||[0-9][0-9][0-9])
GPUTEMP=$(sensors amdgpu-pci-1d00 | grep temp1 | awk '{print $2}' | grep -o [0-9][0-9]||[0-9][0-9][0-9])
GPUFAN=$(sensors amdgpu-pci-1d00 | grep fan1 | awk '{print $2}')
if [[ $CPUTEMP -gt 70 ]]; then
CPUTEMP="%{F#ff5b00}$CPUTEMP%{F-}%{B-}"
else
CPUTEMP="%{F#ff9966}$CPUTEMP%{F-}%{B-}"
fi

if [[ $GPUTEMP -gt 70 ]]; then
GPUTEMP="%{F#ff5b00}$GPUTEMP%{F-}%{B-}"
else
GPUTEMP="%{F#ff9966}$GPUTEMP%{F-}%{B-}"
fi

CPUFREQ=$(lscpu | grep MHz | head -n1 | awk '{print $3}')

echo -n "CPU Temp: $CPUTEMP° CPU Freq: $CPUFREQ $div GPU Temp: $GPUTEMP° $div GPU FAN: $GPUFAN RPM"

}

batteryInfo() {

capacityRemaining=$(cat /sys/class/power_supply/BAT0/capacity)
batteryStatus=$(cat /sys/class/power_supply/BAT0/status)

if [[ $capacityRemaining -lt 25 ]]; then
capacityRemaining="%{F#ff5b00}$capacityRemaining %{F-}%{B-}"
fi

if [[ "$batteryStatus" == "Charging" ]]; then
batteryStatus="%{F#ff9966}$batteryStatus%{F-}%{B-}"
fi

capacityRemaining=$capacityRemaining%
echo -n "Battery: $capacityRemaining $batteryStatus"

}

wifiInfo() {

connectionName=$(nmcli -f GENERAL,WIFI-PROPERTIES dev show wlp26s0 | grep GENERAL.CONNECTION: | awk '{print $2}')
connectionStrength=$(nmcli dev wifi | tail -n1 | awk '{print $7}')

echo -n "Wifi: $connectionName $connectionStrength%"

}

ethPort() {

linkDetected=$(cat /sys/class/net/enp24s0/carrier)
if [[ $linkDetected -eq 0 ]]; then
connected="eth0: down"
elif [[ $linkDetected -eq 1 ]]; then
connected="eth0: up"
fi

echo -n "$connected"

}

diskFree() {

freeNVME=$(df -h /dev/mapper/ubuntu--vg-root | awk '{print $4}' | tail -n1)
freeHDD=$(df -h /dev/mapper/hddVolume | awk '{print $4}' | tail -n1)

echo -n "NVMe Free: $freeNVME $div HDD Free: $freeHDD"

}

loadAvg() {

la=$(uptime | grep -o :\ [0-9].* | sed s/://g | awk '{print $1}' | sed s/,//g)

if (( $(echo "$la > 8" |bc -l) )); then
la="%{F#ff5b00}$la%{F-}%{B-}"
else
la="%{F#ff9966}$la%{F-}%{B-}"
fi


echo -n "Load: $la"

}

soundState() {

muted=$(pactl list sinks | grep Mute: | awk '{print $2}')
if [[ "$muted" == yes ]]; then
state="Volume: 0%"
else
level=$(pactl list sinks | grep Volume: | awk '{print $5}' | head -n1)
state="Volume: $level"
fi

echo -n $state

}

expressVpn() {

expressValue=$(expressvpn status | grep Connected 2>&1 > /dev/null; echo $?)
if [[ "$expressValue" -ne 0 ]]; then
echo -n "%{F#ff5b00}EVPN Down%{F-}%{B-}"
elif [[ "$expressValue" -eq 0 ]]; then
echo -n "%{F#ff9966}EVPN Up%{F-}%{B-}"
else
echo -n "Could not determine ExpressVPN status"
fi
}


now_playing() {

artist=$(cmus-remote -Q | grep "tag artist " | sed s/^tag\ artist\ //g)
title=$(cmus-remote -Q | grep "tag title " | sed s/^tag\ title\ //g)
album=$(cmus-remote -Q | grep "tag album " | sed s/^tag\ album\ //g)
echo -n "Now Playing: $artist - $album - %{F#ff9966}$title%{F-}%{B-}"
}

while true; do
sleep 1
echo "%{l} $(Clock) $div $(cpuTemp) $div $(loadAvg) $div $(wifiInfo) $div $(ethPort) $div $(diskFree) $div $(soundState) $div $(expressVpn) $div $(now_playing)"
sleep 0.5
done