#!/bin/bash

# alsavolume raises the alsa master volume by 2 points (of 32) per default
# or else by the number specified as second parameter if called with the
# parameter "up" and lowers it upon being called with "down".
# When called with the parameter "mute" it mutes the master output,
# with "unmute" it unmutes it at 50% (16 points).

# Copyright Markus Wernig markus at wernig dot net 2003

# This is free software. You may use it under the terms of the 
# GNU General Public License.

CURVOL=`amixer get Master | grep Front.*Playback | sed -e s/".*Playback "//g | sed -e s/" \[.*"//g | head -n 1`
NOTMUTE=`amixer get Master | grep Front.*Playback | sed -e s/".*\["//g | sed -e s/"\].*"//g | head -n 1`

DELTA=2
if [ $# -gt 1 -a -n "$2" ]; then
	DELTA=$2
fi

if [ $DELTA -gt $CURVOL ]; then
	DELTA=1
fi

case $1 in
	up)
	NEWVOL=`expr $CURVOL + $DELTA`
	if [ $NEWVOL -gt 31 ]; then
		NEWVOL=31
	fi
	NEWVOL="$NEWVOL,$NEWVOL unmute"
	;;
	down)
	NEWVOL=`expr $CURVOL - $DELTA`
	if [ $NEWVOL -lt 0 ]; then
		NEWVOL=0
	fi
	NEWVOL="$NEWVOL,$NEWVOL unmute"
	;;
	mute)
	case $NOTMUTE in
		on)
			NEWVOL="mute 16,16"
			;;
		off)
			if [ $CURVOL -le 0 ]; then
				CURVOL=16
			fi
			NEWVOL="unmute ${CURVOL},${CURVOL}"
			;;
		*)
			NEWVOL="$CURVOL,$CURVOL"
			;;
	esac
	;;
	unmute)
	if [ $CURVOL -le 0 ]; then
		CURVOL=16
	fi
	NEWVOL="${CURVOL},${CURVOL} unmute"
	;;
	*)
	NEWVOL=$CURVOL,$CURVOL
	;;
esac

amixer set Master ${NEWVOL} >/dev/null

