#! /bin/bash

VOLUME=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ 2>/dev/null | cut -d' ' -f2)
VOLUME_MUTE=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ 2>/dev/null | cut -d' ' -f3)

function BLUETOOTH {
  bt_device=$(bt-device -l | tail -n+2 | awk '{ print $1 }')

  for device in $bt_device
  do
    connection=$(bt-device -i "$device" 2>/dev/null | grep Connected | awk '{ print $2 }')
    if [[ $connection -eq 1 ]]
    then
      bluetooth=1
    fi
  done
}

VOLUME_PERCENT=$(echo "$VOLUME * 100 / 1" | bc)
BLUETOOTH

if [[ $VOLUME_MUTE = "[MUTED]" ]]
then
  if [[ $bluetooth -ne 0 ]]
  then
    echo "%{F#E07C00} %{F-} $VOLUME_PERCENT%"
  else
    echo "%{F#E07C00}%{F-} $VOLUME_PERCENT%"
  fi
else
  if [[ $bluetooth -ne 0 ]]
  then
    echo "%{F#4E9A06} %{F-} $VOLUME_PERCENT%"
  else
    echo "%{F#4E9A06}%{F-} $VOLUME_PERCENT%"
  fi
fi
