#!/bin/sh
# $Id: volctl,v 1.2 2008/04/20 23:22:32 abs Exp $

# Trivial script to control volume

if [ $# = 2 ] ; then
    DEV="-d /dev/mixer$1"
    shift
fi

toggle_mute()
    {
    case $(mixerctl $DEV -n outputs.master.mute) in
	off) mixerctl $DEV -w outputs.master.mute=on ;;
	on) mixerctl $DEV -w outputs.master.mute=off ;;
    esac
    }

getvol()
{
mixerctl $DEV -n $VAR | sed 's/,.*//'
}

eval $(mixerctl $DEV -a | awk -F= /master=/'{print "VAR="$1}')
VOL=$(getvol)
case "$1" in
    "")
	echo $VOL
	exit
	;;
    +)
	VOL=$(($VOL + 3)) ;;
    -)
	VOL=$(($VOL - 3)) ;;
    +* | -*)
	VOL=$(($VOL + $1)) ;;
    mute)
	toggle_mute; exit ;;
    *)
	VOL=$1 ;;
esac
if [ $VOL -lt 0 ] ; then
    VOL=0
elif [ $VOL -gt 255 ] ; then
    VOL=255
fi
mixerctl $DEV -w $VAR=$VOL > /dev/null

# If we have a valid X display, try to show the current volume
if [ -n "$DISPLAY" ]; then
  PERCENTAGE=$(( 100 \* $(getvol) / 255 ))
  pkill osd_cat
  osd_cat -d 1 -p bottom -b percentage -o 22 -c green -P $PERCENTAGE
fi
