#!/bin/bash

SOUNDFILE="question.wav"

echo "Audio Tester - V0.1"
echo "Copyright 2009 Bill Giannikos"
echo "  "
echo "For best results you should ensure you have the latest version"
echo "of this utility from:"
echo "http://www.linlap.com/wiki/audio+tester"
echo ""

if [ ! -f "$SOUNDFILE" ]; then
	echo "Error: Could not find the question.wav file."
	exit 1
fi
echo "When running this script you may see occasional error messages"
echo "from 'amixer', these can be ignored."
echo ""

echo "Adjusting sound levels to 100%..."
echo ""
amixer -q -c 0 sset Master 100%
amixer -q -c 0 sset Master on
amixer -q -c 0 sset PCM 100%
amixer -q -c 0 sset PCM on
echo "Testing sound..."
aplay -q $SOUNDFILE
echo "Did you hear a sound [N/y]?"
read result
if [ "$result" == "y" ] || [ "$result" == "Y" ]; then
	echo "For sound to work on this system you just need to adjust the Master and PCM volume levels in your mixer program."
	exit
fi
echo "Trying other options mixer options..."
amixer -q -c 0 sset 'External Amplifier' on
aplay -q $SOUNDFILE
echo "Did you hear a sound [N/y]?"
read result
if [ "$result" == "y" ] || [ "$result" == "Y" ]; then
	echo "For sound to work on this system you just need to turn on the 'External Amplifier' option in your mixer program."
	exit
fi
echo "Trying other options mixer options..."
amixer -q -c 0 sset 'External Amplifier' off
aplay -q $SOUNDFILE
echo "Did you hear a sound [N/y]?"
read result
if [ "$result" == "y" ] || [ "$result" == "Y" ]; then
	echo "For sound to work on this system you just need to turn off the 'External Amplifier' option in your mixer program."
	exit
fi

declare -a MODELTYPES=(
"auto"
"3stack"
"3stack-digout"
"5stack"
"5stack-digout"
"6stack"
"6stack-digout"
"w810"
"z71v"
"asus-w1v"
"asus-dig"
"asus-dig2"
"uniwill"
"fujitsu"
"F1734"
"lg"
"lg-lw"
"tcl"
"clevo"
"medion"
"hp"
"hp-3013"
"hp-dc7600"
"acer"
"will"
"replacer"
"basic"
"hp-bpc"
"hp-bpc-d7000"
"hp-tc-t5735"
"hp-rp5700"
"benq"
"benq-t31"
"hippo"
"hippo_1"
"sony-assamd"
"toshiba-s06"
"toshiba-rx1"
"ultra"
"lenovo-3000"
"nec"
"quanta-il1"
"toshiba"
"acer-dmic"
"acer-aspire"
"dell"
"zepto"
"quanta"
"eeepc-p703"
"eeepc-p901"
"3stack-dig"
"3stack-6ch-dig"
"6stack-dig"
"lenovo-101e"
"eeepc-p701"
"eeepc-ep20"
"ecs"
"m51va"
"g71v"
"h13"
"g50v"
"asus-mode1"
"asus-mode2"
"asus-mode3"
"asus-mode4"
"asus-mode5"
"asus-mode6"
"arima"
"targa"
"asus-a7j"
"asus-a7m"
"macpro"
"mbp3"
"imac24"
"w2jc"
"6stack-dig-demo"
"acer-aspire-4930g"
"medion-md2"
"targa-dig"
"targa-2ch-dig"
"laptop-eapd"
"lenovo-nb0763"
"lenovo-ms7195-dig"
"lenovo-sky"
"haier-w66"
"3stack-hp"
"6stack-dell"
"mitac"
"clevo-m720"
"fujitsu-pi2515"
"fujitsu-xa3530"
"3stack-6ch-intel"
"3stack-660"
"uniwill-m31"
"asus-laptop"
"asus"
"3stack-660-digout"
"lenovo"
"dallas"
"asus-v1s"
"minimal"
"min_fp"
"full"
"full_dig"
"allout"
"desktop"
"laptop"
"mobile"
"thinkpad"
"laptop-automute"
"samsung"
"laptop-dig"
"laptop-hpsense"
"laptop-micsense"
"laptop-hpmicsense"
"laptop-hp"
"ref"
"dell-d21"
"dell-d22"
"dell-d23"
"dell-m21"
"dell-m22"
"dell-m23"
"dell-m24"
"dell-m25"
"dell-m26"
"dell-m27"
"gateway-m4"
"gateway-m4-2"
"panasonic"
"dell-m42"
"dell-m43"
"dell-m44"
"intel-mac-v1"
"intel-mac-v2"
"intel-mac-v3"
"intel-mac-v4"
"intel-mac-v5"
"intel-mac-auto"
"macmini"
"macbook"
"macbook-pro-v1"
"macbook-pro"
"imac-intel"
"imac-intel-20"
"ecs202"
"dell-d81"
"dell-d82"
"dell-m81"
"dell-m82"
"m1"
"m1-2"
"m2"
"m2-2"
"m3"
"m5"
"m6"
"ref-no-jd"
"dell-3stack"
"dell-bios"
"dell-m4-1"
"dell-m4-2"
"dell-m4-3"
"hp-m4"
"no-jd"
"dell-m6-amic"
"dell-m6-dmic"
"dell-m6"
"dell-eq"
"vaio"
"vaio-ar"
)

hdacheck=`lsmod | grep snd_hda_intel`
if [ "$hdacheck" == "" ]; then
	echo "Sorry, this script wasn't able to help with your sound problem."
	exit 0
fi

echo "Attempting different model settings for HDA audio."
echo "You need to be running this script as the 'root' user for this to work." 
echo "You must also exit any program which has attached to the audio device."
echo ""
echo "The following will go through all available model options."
echo "This will take a while and you may get a few error messages however you should just contrinue until the end."
echo ""
echo "Would you like to continue? [Y/n]"
read result

if [ "$result" == "n" ] || [ "$result" == "N" ]; then
	exit 0
fi

for model in ${MODELTYPES[*]} ; do
	echo "Testing model type $model"
	echo ""
	rmmod snd_hda_intel
	modprobe snd_hda_intel model=$model index=0
	sleep 1
	amixer -q -c 0 sset Master 100%
	amixer -q -c 0 sset Master on
	amixer -q -c 0 sset PCM 100%
	amixer -q -c 0 sset PCM on
	aplay -q $SOUNDFILE
	echo "Did you hear a sound [N/y]?"
	read result
	if [ "$result" == "y" ] || [ "$result" == "Y" ]; then
		echo "The model type you need is '$model'."
		echo "Would you like to set this permanently? [N/y]"
		read result
		if [ "$result" == "y" ] || [ "$result" == "Y" ]; then
			declare -a ALSACONFIG=(
			"/etc/modprobe.d/alsa-base"
			"/etc/modprobe.d/alsa"
			"/etc/modprobe.d/sound"
			"/etc/modprobe.d/audio"
			"/etc/modprobe.conf"
			)
			for aconfig in ${ALSACONFIG[*]} ; do
				if [ -f "$aconfig" ]; then
					NEWCONFIG=`grep -v "options snd_hda_intel" $aconfig`
					echo "$NEWCONFIG" > $aconfig
					NEWCONFIG=`grep -v "options snd-hda-intel" $aconfig`
					echo "$NEWCONFIG" > $aconfig
					echo "options snd_hda_intel index=0 model=$model" >> $aconfig
					echo "Added 'options snd_hda_intel model=$model' to your $aconfig file."
					echo "Restart your computer now."
					exit 0
				fi
			done

			echo "Sorry, the correct file to insert the configuration information into could not be found."
			echo "You will need to add the following line to your /etc/modprobe.conf (or your"
			echo "distributions equivalent) file:"
			echo "options snd_hda_intel model=$model"
			exit 0
		else
			echo "No changes made, you will need to add the following line"
			echo "to your /etc/modprobe.conf (or your distributions equivalent) file:"
			echo "options snd_hda_intel model=$model"
			exit 0
		fi
	fi
done

echo "Sorry, this script was not able to help with your sound problem."

