#!/bin/sh

# sqp_JogglerAudio.sh v1.01 (21st April 2012) by Andy Davison
#  Called by the JogglerAudio applet to mess with the Joggler's audio settings.

PLATFORM=`cat /tmp/sqp_platform`
[ "$PLATFORM" = "native" ] || export PATH=$PATH:`dirname $0`

if [ "$1" = "" ]; then
	echo "Usage: $0 <option> <action>"
	exit 0
fi

if [ "$PLATFORM" = "native" ]; then
	INSTALL_DIR="/media/opt"
	SUDO=""
else
	INSTALL_DIR="/opt"
	SUDO="sudo"
fi

CRNTIFACE=`grep card /etc/asound.conf | head -1 | awk -F\card\  {'print $2'} | awk '{print substr($1,1,1)}'`

## Set audio interface.
if [ "$1" = "iface" ]; then
	if [ "$2" = "$CRNTIFACE" ]; then
		echo "Interface is already correctly configured as card $CRNTIFACE."
		exit 0
	else
		[ "$CRNTIFACE" = "0" ] && $SUDO sed -i 's/card\ 0*/card\ 1/g' /etc/asound.conf
		[ "$CRNTIFACE" = "1" ] && $SUDO sed -i 's/card\ 1*/card\ 0/g' /etc/asound.conf
	fi
fi


## Swapping the asound.conf files.
if [ "$1" = "mixer" ]; then
	if [ "$2" = "" ]; then
		echo "Usage: $0 $1 <simple|mixer>"
		exit 0
	else
		if [ -f $INSTALL_DIR/squeezeplay/config/asound.conf_$2 ]; then
			$SUDO cp $INSTALL_DIR/squeezeplay/config/asound.conf_$2 /etc/asound.conf
			$0 iface $CRNTIFACE
		else
			echo "The asound.conf file 'config/asound.conf_$2' has not been found."
			exit 0
		fi
	fi
fi

exit 0