#!/bin/bash
#################################################################################
#                                                                               #
#  Volume OSD                                                                   #
#                                                                               #
#  Depends on: amixer, pactl, wmctrl                                            #
#                                                                               #
#  Usage: vol-osd.sh up    =  volume up                                         #
#         vol-osd.sh down  =  volume down                                       #
#                                                                  version 1    #
#################################################################################

# How much % volume change per step
volperc="5"

# OSD delay in milliseconds
delay="50"

# Max volume  (change also in vol-osd.lua the 'max=' value)
maxvol="100"

# Horizontal placement of window
gap_x="0"

# Vertical placement of window
gap_y="-200"

#################################################################################


get_volume () {
  volume=$(amixer -D pulse | awk -F"[][]" '/Left:/ { print $2 }' | head -n1)
  volume=${volume::-1}
  echo "$volume" > ./tempfiles/volume.tmp
}


vol_up () {
  if [ "$(cat ./tempfiles/volume.tmp)" -lt "$maxvol" ]; then
    pactl set-sink-volume @DEFAULT_SINK@ +"$volperc"%
  fi
}


vol_down () {
  pactl set-sink-volume @DEFAULT_SINK@ -"$volperc"%
}


if [ "$1" = "up" ]; then
  vol_up
elif [ "$1" = "down" ]; then
  vol_down
else
  echo
  echo " Conky Volume OSD"
  echo
  echo " Usage: vol-osd.sh up    =  volume up"
  echo "        vol-osd.sh down  =  volume down"
  echo
fi

get_volume

active=$(wmctrl -l | grep 'conky-volume-osd')
if [ "$active" = "" ]; then
  conky -q -y "$gap_y" -x "$gap_x" -i "$delay" -c ./vol-osd.conky &
fi

echo -e "Volume = $volume% \n"