#!/bin/sh

# finds the active sink for pulse audio and increments the volume. useful when you have multiple audio outputs and have a key bound to vol-up and down
# Taken from http://customlinux.blogspot.fr/2013/02/pavolumesh-control-active-sink-volume.html
# and slightly adapted by lhark to work with notify-send

notify='dunst' # no/yes/dunst
notiftime='400'
notifid='42'
inc='2'
capvol='yes'
maxvol='200'
autosync='no'

main_sink=$(pacmd list-sinks |awk '/* index:/{print $3}')
active_sinks=$(pacmd list-sinks |awk '/index:/{print $NF}' | tr '\n' ' ' | sed 's/ $/\n/g')
limit=$((100 - inc))
maxlimit=$((maxvol - inc))

updateSinks () {
    active_sinks=$(pacmd list-sinks |awk '/index:/{print $NF}' | tr '\n' ' ' | sed 's/ $/\n/g')
}


volUp () {
    updateSinks
    getCurVol

    for sink in ${active_sinks}; do
        if [ ${capvol} = 'yes' ]; then
            if [ "${curVol}" -le 100 ] && [ "${curVol}" -ge ${limit} ]; then
                pactl set-sink-volume "${sink}" 100%
            elif [ "${curVol}" -lt ${limit} ]; then
                pactl set-sink-volume "${sink}" +${inc}%
            fi
        elif [ "${curVol}" -le ${maxvol} ] && [ "${curVol}" -ge ${maxlimit} ]; then
            pactl set-sink-volume "${sink}" ${maxvol}%
        elif [ "${curVol}" -lt ${maxlimit} ]; then
            pactl set-sink-volume "${sink}" +${inc}%
        fi
    done

    getCurVol

    if [ ${notify} = 'yes' ]; then
        notify-send "Volume : ${curVol}%" -t ${notiftime}
    elif [ "$notify" = 'dunst' ]; then
        dunstify -r "$notifid" -t "$notiftime" "Volume: $curVol%"
    fi

    if [ ${autosync} = 'yes' ]; then
        volSync
    fi
}

volDown () {
    updateSinks
    for sink in ${active_sinks}; do
        pactl set-sink-volume "${sink}" -${inc}%
    done
    getCurVol

    if [ ${notify} = 'yes' ]; then
        notify-send "Volume : ${curVol}%" -t ${notiftime}
    elif [ "$notify" = 'dunst' ]; then
        dunstify -r "$notifid" -t "$notiftime" "Volume: $curVol%"
    fi

    if [ ${autosync} = 'yes' ]; then
        volSync
    fi
}


volSync () {
    updateSinks
    getCurVol

    for each in ${active_sinks}; do
        pactl set-sink-volume "${each}" "${curVol}"%
    done
}


getCurVol () {
    curVol=$(pacmd list-sinks |grep -A 15 'index: '"${main_sink}"'' |grep 'volume:' |egrep -v 'base volume:' |awk -F : '{print $3}' |grep -o -P '.{0,3}%'|sed s/.$// | tr -d ' ')
}


volMute () {
    case "$1" in
        mute)
            for sink in ${active_sinks}; do
                pactl set-sink-mute "${sink}" 1
            done
            curVol=0
            status=1
            ;;
        unmute)
            for sink in ${active_sinks}; do
                pactl set-sink-mute "${sink}" 0
            done
            getCurVol
            status=0
            ;;
    esac

    if [ ${notify} = 'yes' ]; then
        notify-send "$([ "${status}" = 1 ] && echo "Sound muted" || echo "Sound unmuted")" -t ${notiftime}
    elif [ "$notify" = 'dunst' ]; then
        dunstify -r "$notifid" -t "$notiftime" "$([ "${status}" = 1 ] && echo "Sound muted" || echo "Sound unmuted")"
    fi
}

volMuteStatus () {
    curStatus=$(pacmd list-sinks |grep -A 15 'index: '"${main_sink}"'' |awk '/muted/{ print $2}')

    if [ "${curStatus}" = 'yes' ]; then
        volMute unmute
    else
        volMute mute
    fi
}


volHelp () {
    echo "Finds the active sink for pulse audio and manages the volume."
    echo "Options :"
    echo ""
    echo "  --down"
    echo "  --help"
    echo "  --mute"
    echo "  --sync      Sync all sinks volumes"
    echo "  --toggle"
    echo "  --unmute"
    echo "  --up"
}


case "$1" in
--up)
volUp
;;
--down)
volDown
;;
--toggle)
volMuteStatus
;;
--help)
volHelp
;;
--mute)
volMute mute
;;
--unmute)
volMute unmute
;;
--sync)
volSync
;;
esac