#!/bin/bash

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

aws_vpn_status() {
INTERFACE_CHECK=$(ip a | grep tun0)
if [[ ! -z $INTERFACE_CHECK ]]; then
INTERFACE_VALUE=$(echo $INTERFACE_CHECK | tail -n1 | awk '{print $15}')
echo -n "AWS VPN IP: $INTERFACE_VALUE"
else
echo -n "AWS VPN Down"
fi
}

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

echo -n "$DATETIME"
}

cpuTemp() {

CPUTEMP=$(sensors | grep CPU | awk '{print $2}')
echo -n "CPU Temp: $CPUTEMP"

}

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=$(wpa_cli status | egrep "^ssid" | sed s/ssid=//g)
ipAddr=$(wpa_cli status | egrep "^ip" | sed s/ip_address=//g)

echo -n "Wifi: $connectionName $div IP: $ipAddr"

}

ethPort() {

linkDetected=$(cat /sys/class/net/enp0s31f6/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/decryptedRoot-root | awk '{print $4}' | tail -n1)

echo -n "NVMe Free: $freeNVME"

}

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

}

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