#!/bin/sh -e
#
# simple pulse wrapper for controlling
# audio stuff i care about

get_current_volume_percent() {
    percent="$(pactl get-sink-volume @DEFAULT_SINK@ | awk '/Volume/ {print $5}')"
    strip_perc="$(printf "%s" "$percent" | cut -d '%' -f 1)"
    printf "%s" "$strip_perc"
}

notify_current_volume() {
    vol="$(get_current_volume_percent)"
    if [ "$vol" -gt 100 ]; then
        # prevent volumes of >100% for the
        # good of all ears
        pactl set-sink-volume @DEFAULT_SINK@ 100%
        notify-send '🔈 vol' --hint=int:value:100 -t 800 -h string:x-canonical-private-synchronous:anything
    elif [ "$vol" -lt 0 ]; then
        # prevent volumes of <0% for the
        # good of all ... dog... whateij fiwjoefj
        pactl set-sink-volume @DEFAULT_SINK@ 0%
        notify-send '🔈 vol' --hint=int:value:0 -t 800 -h string:x-canonical-private-synchronous:anything
    else
        notify-send '🔈 vol' --hint=int:value:"$vol" -t 800 -h string:x-canonical-private-synchronous:anything
    fi
}

case "$1" in
    ls|list)
        pactl list short sinks | awk '{print $1" "$2}'
        ;;
    set|use)
        pactl set-default-sink "$2"
        ;;
    [0-9]*)
        pactl set-sink-volume @DEFAULT_SINK@ "$1"%
        notify_current_volume
        ;;
    up)
        # expect $2 = % to increase
        pactl set-sink-volume @DEFAULT_SINK@ +"$2"%
        notify_current_volume
        ;;
    down)
        pactl set-sink-volume @DEFAULT_SINK@ -"$2"%
        notify_current_volume
        ;;
    *)
        printf "unknown command\n"
        exit 1
        ;;
esac
