#! /bin/sh

set -e

TITLE="Advanced Linux Sound Architecture"

Initialize() {
	TMPFILE=`tempfile`
	CFGFILE=`tempfile`
	cardcount=0
}

CleanUp() {
	rm $TMPFILE $CFGFILE
}

Abort() {
	CleanUp
	exit 1
}


CheckConfig() {
	if [ `id -u` != "0" ]; then
		whiptail --backtitle "$TITLE" --fb --nocancel --title "Error" \
			--msgbox "Sorry, you need to be root to configure ALSA." 9 60
		exit 1
	fi

	if [ -s /etc/modutils/alsa ]; then
		if [ "$1" != "--force" ]; then
			whiptail --backtitle "$TITLE" --fb --nocancel --title "Error" \
				--msgbox "ALSA already configured. Use --force to reconfigure." 9 70
			exit 1
		fi
	fi
}


ShowHello() {
	cat <<-	EOF > $TMPFILE
		Welcome to the ALSA configuration script!

		I'll ask a couple of question about your audio hardware
		which we need to configure ALSA.
EOF
	whiptail --backtitle "$TITLE" --fb --nocancel --title "Welcome" \
		--msgbox "`cat $TMPFILE`" 12 60
}


SoundVerify() {
	if ! whiptail --backtitle "$TITLE" --fb --nocancel --title "Introduction" \
		--yesno "Do you have one or more soundcards installed?" 12 60 ; then
		whiptail --backtitle "$TITLE" --fb --nocancel --title "Oops" \
			--msgbox "Sorry, you need a soundcard to use ALSA." 10 60
		Abort
	fi
}


ReallyAddCard() {
	echo "alias snd-card-`expr $cardcount - 1` $1" >> $CFGFILE
	echo "options $1 snd_index=$cardcount" >> $CFGFILE
}

AddCard() {
	cardcount=`expr $cardcount + 1`
	whiptail --backtitle "$TITLE" --fb --nocancel --title "Soundcard type" \
		--noitem --radiolist "Please select the type of the card:" 20 60 11 \
			"AMD InterWave" 1 \
			"AMD InterWave with TEA6330T" 0 \
			"CS4232/CS4232A chips" 0 \
			"CS423[5-7]* chips" 0 \
			"ESS Audiodrive ESx688" 0 \
			"Ensoniq AudioPCI" 0 \
			"Gravis UltraSound Classic" 0 \
			"Gravis UltraSound Extreme" 0 \
			"Gravis UltraSound MAX" 0 \
			"Mozart (OAK OTI-601)" 0 \
			"S3 SonicVibes" 0 \
			"Soundblaster 1.0/2.0/Pro" 0 \
			"Soundblaster 16/AWE32/AWE64" 0 \
			"Yamaha OPL3-SA" 0 2>$TMPFILE

	result="`cat $TMPFILE`"
	case $result in *CS423\[*)
		ReallyAddCard snd-card-cs4236 ;;
	esac
	case $result in *CS4232A*)
		ReallyAddCard snd-card-cs4232 ;;
	esac
	case $result in *SonicVibes*)
		ReallyAddCard snd-sonicvibes ;;
	esac
	case $result in *Ensoniq*)
		ReallyAddCard snd-audiopci ;;
	esac
	case $result in *TEA6330T*)
		ReallyAddCard snd-interwave-stb ;;
	esac
	case $result in *ESS*)
		ReallyAddCard snd-audiodrive1688 ;;
	esac
	case $result in *Classic*)
		ReallyAddCard snd-gusclassic ;;
	esac
	case $result in *Extreme*)
		ReallyAddCard snd-gusextreme ;;
	esac
	case $result in *MAX*)
		ReallyAddCard snd-gusmax ;;
	esac
	case $result in *InterWave*)
		ReallyAddCard snd-interwave ;;
	esac
	case $result in *Mozart*)
		ReallyAddCard snd-mozart ;;
	esac
	case $result in *Pro*)
		ReallyAddCard snd-sb8 ;;
	esac
	case $result in *AWE32*)
		ReallyAddCard snd-sb16 ;;
	esac
}


WriteConfiguration() {
	cat <<-EOF > $TMPFILE
		### BEGIN ALSA SETUP ###
		# Everything in this block is managed by the alsaconfig script.
		options snd snd_major=14 snd_cards_limit=$cardcount
		alias char-major-14 snd
		alias snd-minor-oss-0 snd-mixer
		alias snd-minor-oss-3 snd-pcm1-oss
		alias snd-minor-oss-4 snd-pcm1-oss
		alias snd-minor-oss-5 snd-pcm1-oss
		alias snd-minor-oss-12 snd-pcm1-oss
		`cat $CFGFILE`
		### END ALSA SETUP ###
EOF

# Note: we can't just move conf.modules, since it might be a symlink
	if [ -f /etc/modutils/alsa ]; then
		mv /etc/modutils/alsa /etc/modutils/alsa~
	fi
	cat $TMPFILE > /etc/modutils/alsa

	cat <<-	EOF > $TMPFILE
		We're finished. I have updated the configuration in
		/etc/modutils/alsa. The old file is still available as
		/etc/modutils/alsa~
EOF

	whiptail --backtitle "$TITLE" --fb --nocancel --title "Finished" \
		--msgbox "`cat $TMPFILE`" 11 60
}


CheckConfig $@
Initialize
ShowHello
SoundVerify

while : ; do
	AddCard
	if ! whiptail --backtitle "$TITLE" --fb --nocancel --title "Continue?" \
		--yesno "Do you want to add another card?" 12 60 ; then
		break
	fi
done

WriteConfiguration
CleanUp

