#!/bin/bash
# minimal infos for swaybar

# active window
wfoc=`swaymsg -t get_tree | jq ".. | select(.type?) | select(.focused==true).name"`
wfocnb=`echo ${#wfoc}`
if [ "$wfocnb" -gt 60 ]; then
    wfocus=`echo "${wfoc:0:59}...\""`
else
    wfocus="$wfoc"
fi

# apt updates
updates="$(($(apt list --upgradable 2>/dev/null | wc -l) - 1))"
if [[ ${updates} -gt 0 ]]; then
    wapt=" [ ${updates}] "
else
    wapt=" "
fi

# cpu - source: i3block
PREV_TOTAL=0
PREV_IDLE=0
cpuFile="/tmp/.cpu"
if [[ -f "${cpuFile}" ]]; then
  fileCont=$(cat "${cpuFile}")
  PREV_TOTAL=$(echo "${fileCont}" | head -n 1)
  PREV_IDLE=$(echo "${fileCont}" | tail -n 1)
fi
CPU=(`cat /proc/stat | grep '^cpu '`) # Get the total CPU statistics.
unset CPU[0]                          # Discard the "cpu" prefix.
IDLE=${CPU[4]}                        # Get the idle CPU time.
# Calculate the total CPU time.
TOTAL=0
for VALUE in "${CPU[@]:0:4}"; do
  let "TOTAL=$TOTAL+$VALUE"
done
if [[ "${PREV_TOTAL}" != "" ]] && [[ "${PREV_IDLE}" != "" ]]; then
  # Calculate the CPU usage since we last checked.
  let "DIFF_IDLE=$IDLE-$PREV_IDLE"
  let "DIFF_TOTAL=$TOTAL-$PREV_TOTAL"
  let "DIFF_USAGE=(1000*($DIFF_TOTAL-$DIFF_IDLE)/$DIFF_TOTAL+5)/10"
  wcpu="$(echo "${DIFF_USAGE}%")"
else
  wcpu="?"
fi
# Remember the total and idle CPU times for the next check.
echo "${TOTAL}" > "${cpuFile}"
echo "${IDLE}" >> "${cpuFile}"

# ram
mem_total=`free -m | grep "Mem" | awk '{print $2}'`
mem_used=`free -m | grep "Mem" | awk '{print $3}'`
mem_perc="$(($mem_used*100/$mem_total))"
# disk
hd_used=`df | grep "sda1" | awk '{print $3}'`
hd_total=`df | grep "sda1" | awk '{print $2}'`
hd_perc="$(($hd_used*100/$hd_total))"

# network
if [ "$(cat /sys/class/net/enp0s25/operstate)" = "up" ] || [ "$(cat /sys/class/net/wls1/operstate)" = "up" ]; then
        myip="$(wget -q -O - checkip.dyndns.org | sed -e 's/[^[:digit:]\|.]//g')"
    else
        myip=""
fi
if [ "$(cat /sys/class/net/enp0s25/operstate)" = "up" ]; then
    wnet=" $myip"
elif
    [ "$(cat /sys/class/net/wls1/operstate)" = "up" ]; then
    wnet=" $myip"
else
    wnet=""
fi

# temp
wtemp=$(sensors | grep CPU | awk '{print $2}' | cut -d'+' -f2)

# battery
urgent=15
batt_info=$(acpi -b | grep "Battery ${BATTERY}")
batt_state=$(echo "$batt_info" | grep -wo "Full\|Charging\|Discharging")
batt_time=$(echo "$batt_info" | awk '{print $5}' | cut -c1-5)
batt_perc=$(echo "$batt_info" | grep -o '[0-9]\+%' | tr -d '%')
case $batt_state in
    FULL) wbatt="" ;;
    Charging) wbatt=" $batt_perc%" ;;
    Discharging)
        if [ "$batt_perc" -gt 80 ]; then
            wbatt=" $batt_perc%"
        elif [ "$batt_perc" -gt 50 ]; then
            wbatt=" $batt_perc%"
        elif [ "$batt_perc" -gt "$urgent" ]; then
            wbatt=" $batt_perc%"
        else
            wbatt=" $batt_time"
        fi
        ;;
    *) wbatt=" $batt_perc%" ;;
esac

# brightness
level=`cat "/sys/class/backlight/acpi_video0/brightness"`
max=`cat "/sys/class/backlight/acpi_video0/max_brightness"`
wbright="$(( $level * 100 / $max ))"
if [ "$wbright" -gt 70 ]; then
    wbrighti=""
elif [ "$wbright" -gt 50 ]; then
    wbrighti=""
elif [ "$wbright" -gt 30 ]; then
    wbrighti=""
else
    wbrighti=""
fi

# audio
mastervol=$(amixer get Master | tail -n 1)
status=$(echo "${mastervol}" | grep -wo "on")
volume=$(echo "${mastervol}" | awk -F ' ' '{print $5}' | tr -d '[]%')
if [ "$status" == "on" ]; then
    if [ $volume -gt 60 ]; then
        wvol=" $volume%"
    elif [ $volume -gt 30 ]; then
        wvol=" $volume%"
    else
        wvol=" $volume%"
    fi
else
    wvol=""
fi

# capture
captvol=$(amixer get Capture | tail -n 1)
captstat=$(echo "${captvol}" | grep -wo "on")
captlev=$(echo "${captvol}" | awk -F ' ' '{print $5}' | tr -d '[]%')
if [ "$captstat" == "on" ]; then
    wcap=" $captlev%"
else
    wcap=""
fi

# trash
if [ ! -e $HOME/.local/share/Trash/files ]; then
    wtrash=""
else
    trashcount="$(ls -U -1 "$HOME/.local/share/Trash/files" | wc -l)"
    #wtrash="[ $trashcount] "
    if [[ ${trashcount} > 0 ]]; then
        wtrash=" [ $trashcount] "
    else
        wtrash=" "
    fi
fi

# date-time
sw_date=`date +'%Y-%m-%d'`
sw_time=`date +'%I:%M%p'`

echo "[$wfocus]$wapt[ $wcpu] [ $mem_perc%] [ $hd_perc%] [$wnet] [ $wtemp] [$wbatt] [$wbrighti $wbright%] [$wvol] [$wcap]$wtrash[ $sw_date  $sw_time]"
