#!/bin/bash

key_micmute() {
  state=$(pactl get-source-mute @DEFAULT_SOURCE@ | awk -F': ' '{print $2}')
  case ${state} in
    yes)
      echo 0 | doas tee /sys/devices/platform/thinkpad_acpi/leds/platform::micmute/brightness
      pactl set-source-mute @DEFAULT_SOURCE@ 0 ;;
    no)
      echo 1 | doas tee /sys/devices/platform/thinkpad_acpi/leds/platform::micmute/brightness
      pactl set-source-mute @DEFAULT_SOURCE@ 1 ;;
  esac
}

vol_decrease() {
  pactl set-sink-volume @DEFAULT_SINK@ -5%
  volume=$(pactl get-sink-volume @DEFAULT_SINK@ | head -n 1 | awk -F' / *' '{print $2}')
  notify-send -t 2000 -u low "volume: $volume"
}

vol_increase() {
  pactl set-sink-volume @DEFAULT_SINK@ +5%
  volume=$(pactl get-sink-volume @DEFAULT_SINK@ | head -n 1 | awk -F' / *' '{print $2}')
  notify-send -t 2000 -u low "volume: $volume"
}

if [ $1 ]; then
  case $1 in
    dec) vol_decrease ;;
    inc) vol_increase ;;
    key) key_micmute ;;
  esac
else
  mic=$(pactl get-source-volume @DEFAULT_SOURCE@ | awk -F' / ' '{print $2}' | tr -d '%')
  [ $(pactl get-source-mute @DEFAULT_SOURCE@ | awk -F': ' '{print $2}') = yes ] || [ "$mic" -eq 0 ] && micmute="mic muted" || micmute=""
  vol=$(pactl get-sink-volume @DEFAULT_SINK@ | awk -F' / ' '{print $2}' | tr -d '%')
  [ $(pactl get-sink-mute @DEFAULT_SINK@ | awk -F': ' '{print $2}') = yes ] || [ "$vol" -eq 0 ] && printf "${micmute} audio muted" || printf "$micmute"
fi
