#!/bin/sh

if command -v mako >/dev/null
then
    notification=mako
else
    notification=dunst
fi

if command -v pulsemixer >/dev/null
then
    _notify_volume() {
        volume=$(pulsemixer --get-volume)
        # 2 channels
        left=$(echo "${volume}" | cut -d' ' -f1)
        right=$(echo "${volume}" | cut -d' ' -f2)

        notify-send -h string:x-canonical-private-synchronous:audio -h int:value:"${left}" -i "$HOME/.config/${notification}/speaker.png" "Volume: L:${left}% / R:${right}%"
    }

    case $1 in
    increase)
        pulsemixer --change-volume +5 && _notify_volume
        ;;
    decrease)
        pulsemixer --change-volume -5 && _notify_volume
        ;;
    toggle)
        pulsemixer --toggle-mute
        status=$(pulsemixer --get-mute)
        if [ "${status}" -eq 1 ] || [ "${status}" = "true" ]
        then
            notify-send -i "$HOME/.config/${notification}/mute.png" "Sound muted"
        else
            notify-send -i "$HOME/.config/${notification}/speaker.png" "Sound unmuted"
        fi ;;
    *)
        ;;
    esac
else
    _notify_volume() {
        volume=$(amixer get Master | sed -nre 's/.*\\[(.*%)\\].*/\\1/p')
        notify-send -h string:x-canonical-private-synchronous:audio -h int:value:"${volume}" -i "$HOME/.config/${notification}/speaker.png" "Volume: ${volume}"
    }

    case $1 in
    increase)
        amixer set Master 5+ && _notify_volume
        ;;
    decrease)
        amixer set Master 5- && _notify_volume
        ;;
    toggle)
        amixer set Master toggle
        if amixer get Master | grep "off"
        then
            notify-send -i "$HOME/.config/${notification}/mute.png" "Sound muted"
        else
            notify-send -i "$HOME/.config/${notification}/speaker.png" "Sound unmuted"
        fi ;;
    *)
        ;;
    esac
fi
