#!/bin/bash

# You can call this script like this:
# $ ./volumeControl.sh up
# $ ./volumeControl.sh down
# $ ./volumeControl.sh mute

# Script modified from these wonderful people:
# https://github.com/dastorm/volume-notification-dunst/blob/master/volume.sh
# https://gist.github.com/sebastiencs/5d7227f388d93374cebdf72e783fbd6a

function get_volume {
  /usr/bin/amixer get Master | /usr/bin/grep '%' | /usr/bin/head -n 1 | /usr/bin/cut -d '[' -f 2 | /usr/bin/cut -d '%' -f 1
}

function is_mute {
  /usr/bin/amixer get Master | /usr/bin/grep '%' | /usr/bin/grep -oE '[^ ]+$' | /usr/bin/grep off > /dev/null
}

function send_notification {
  iconSound="audio-volume-high"
  iconMuted="audio-volume-muted"
  
  if is_mute ; then
    /usr/bin/dunstify -a "Volume" -i $iconMuted -u normal "" -h int:value:"$(get_volume)"
  
  else    
    # Send the notification
    /usr/bin/dunstify -a "Volume" -i $iconSound -u normal "" -h int:value:"$(get_volume)"
  
  fi
}

case $1 in
  up)
    # Set the volume on (if it was muted)
    /usr/bin/amixer -D pulse set Master on > /dev/null
    
    # Increase the volume (+ 5%)
    /usr/bin/amixer -D pulse sset Master 5%+ > /dev/null
    
    send_notification
    ;;
  
  down)
    # Set the volume on (if it was muted)
    /usr/bin/amixer -D pulse set Master on > /dev/null
    
    # Lower the volume (- 5%)
    /usr/bin/amixer -D pulse sset Master 5%- > /dev/null
    
    send_notification
    ;;

  mute)
    # Toggle mute
    /usr/bin/amixer -D pulse set Master 1+ toggle > /dev/null
    
    send_notification
    ;;
esac
