#!/bin/bash

msgTag="volume"
TIME=1000

case "$1" in
  up)
    wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 5%+
    ;;
  down)
    wpctl set-volume -l 1 @DEFAULT_AUDIO_SINK@ 5%-
    ;;
  mute)
    wpctl set-mute @DEFAULT_AUDIO_SINK@ toggle
    mute=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep -q MUTED && echo "yes" || echo "no")
    if [[ "$mute" == "yes" ]]; then
        dunstify -t $TIME \
                 -a "Volume" \
                 -h string:x-dunst-stack-tag:$msgTag \
                 "Muted"
    else
        dunstify -t $TIME \
                 -a "Volume" \
                 -h string:x-dunst-stack-tag:$msgTag \
                 "Unmuted"
    fi
    exit 0
    ;;
  *)
    echo "Usage: $0 [up/down/mute/mic]"
    exit 1
    ;;
esac

volume=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | awk '{print int($2*100)}')
mute=$(wpctl get-volume @DEFAULT_AUDIO_SINK@ | grep -q MUTED && echo "yes" || echo "no")

if [[ "$mute" != "yes" ]]; then
  dunstify -t $TIME \
           -a "Volume" \
           -h string:x-dunst-stack-tag:$msgTag \
           -h int:value:"$volume" \
           "Volume: $volume%"
fi
