#|/bin/bash

echo "OpenAL per-user configuration script"
echo "LGPL licensed (include wherever you want)"

ANSW="x"
while [ "x$ANSW" != "x1" ] && [ "x$ANSW" != "x2" ] && [ "x$ANSW" != "x3" ] && [ "x$ANSW" != "x4" ] ; do
	echo "Please give us your ALSA settings"
	echo ""
	echo "1 hardware	: \"default\""
	echo "2 hardware	: \"default:x\" (will ask x and y later, default is 0,0)"
	echo "3 software-dmix	: \"plughw:x,y\" (will ask x and y later, default is 0,0)"
	echo "4 NO ALSA"
	read ANSW
done

if [ "x$ANSW" == "x1" ] ;then
	LIN1="(define alsa-device \"default\")"
fi

if [ "x$ANSW" == "x2" ] || [ "x$ANSW" == "x3" ] ;then
	if [ "x$ANSW" == "x2" ] ;then
		LIN1="(define alsa-device \"default:"
		ISOK="x"
		while [ "x$ISOK" != "xy" ] ;do
			echo ""
			echo "please insert x,y numbers. Examples: 0,0 or 0 or 1,0 "
			read xynumbers
			echo ""
			echo "you inserted $xynumbers, is this right (y/n)?"
			read ISOK
		done
	else
		LIN1="(define alsa-device \"plughw:"
		ISOK="x"
		while [ "x$ISOK" != "xy" ] ;do
			echo ""
			echo "please insert x,y numbers. Examples: 0,0 or 0 or 1,0 "
			echo "(note: if 0,0 isn't working fine, try just 0)"
			read xynumbers
			echo ""
			echo "you inserted $xynumbers, is this right (y/n)?"
			read ISOK
		done
	fi
	LIN1="$LIN1$xynumbers\")"
fi

if [ "x$ANSW" == "x4" ] ;then
	LIN1="#(define alsa-device \"hw:0\")"
fi

echo ""
echo "last step:"
echo "select your preferred devices"
echo ""
echo "you should now provide a string containing your preferred devices numbers"
echo "from firse to last,separated with spaces, device which you can choose:"
if [ "x$ANSW" != "x4" ] ;then
	echo "0 - alsa"
fi 
echo "1 - esd"
echo "2 - arts"
echo ""
echo "at the end of the string native device will be added automatically"
ISOK="x"
while [ "x$ISOK" != "xy" ] ;do
		echo ""
		echo "please insert the devices,example: 1 2 or 2 1 or just 1"
		read xynumbers
		echo ""
		echo "you inserted $xynumbers, is this right (y/n)?"
		read ISOK
done

GBG=""
for nbb in $xynumbers ;do
	if [ "x$nbb" == "x0" ] ; then
		if [ "x$GBG" != "x" ] ;then
			GBG="$GBG alsa"
		else
			GBG="alsa"
		fi
	fi
	if [ "x$nbb" == "x1" ] ; then
		if [ "x$GBG" != "x" ] ;then
			GBG="$GBG esd"
		else
			GBG="esd"
		fi
	fi
	if [ "x$nbb" == "x2" ] ; then
		if [ "x$GBG" != "x" ] ;then
			GBG="$GBG arts"
		else
			GBG="arts"
		fi
	fi
done

LIN2="(define devices '($GBG native))"
echo ""
echo "$LIN1"
echo "$LIN2"

echo "$LIN1" > ~/.openalrc
echo "$LIN2" >> ~/.openalrc
