#!/bin/sh

set -e
		
if [ "$1" = "configure" ]; then
    if [ ! -f /etc/default/alsa ]; then
	if [ -f /etc/alsa/alsa-base.conf ]; then
	    printf "Note: Moving /etc/alsa/alsa-base.conf to /etc/default/alsa\n"
	    mv /etc/alsa/alsa-base.conf /etc/default/alsa
	elif [ -f /etc/alsa-base.conf ]; then
	    printf "Note: Moving /etc/alsa-base.conf to /etc/default/alsa\n"
	    mv /etc/alsa-base.conf /etc/default/alsa
	fi
    fi
fi

. /usr/share/debconf/confmodule
db_version 2.0

case "$1" in
	configure)
		db_get alsa-base/card_list || true
		cards="$RET"
		counter=0
		tempfile="$(tempfile)"
		for i in $cards ; do
		    if [ "$i" != "done" ]; then
			echo "alias snd-card-$counter snd-$i" >> "$tempfile"
			counter=$(( $counter + 1 ))
		    fi
		done
		echo "" >> "$tempfile"
		if [ "$counter" -gt 0 ]; then
			counter=$(( $counter - 1 ))
			for i in $(seq 0 $counter) ; do
		    	echo "alias snd-slot-$i snd-card-$i" >> "$tempfile"
			echo "alias sound-slot-$i snd-slot-$i" >> "$tempfile"
			done
		fi
		debconfbit="$(tempfile)"
		printf "### DEBCONF MAGIC\n# This file was automatically generated by alsa-base's debconf stuff\n\n" > "$debconfbit"
		filetodumpto="$(tempfile)"
		cat "$debconfbit" /usr/share/alsa-base/modules-snippet.conf "$tempfile" > "$filetodumpto"
		if [ ! -f /etc/alsa/modutils/0.9 ] || grep -Eq \
            "^[[:space:]]*### DEBCONF MAGIC$" /etc/alsa/modutils/0.9 ; then
			if [ -f /etc/alsa/modutils/0.9 ]; then
		    	mv /etc/alsa/modutils/0.9 /etc/alsa/modutils/0.9.debconf-backup
			fi
		    cp $filetodumpto /etc/alsa/modutils/0.9
		    chmod 644 /etc/alsa/modutils/0.9
			echo "Wrote ALSA configuration to /etc/alsa/modutils/0.9"
		else
			echo "/etc/alsa/modutils/0.9 does not have an autoconfiguration tag, if you would"
			echo "like this file to be automatically generated, please read point #5 of"
			echo "/usr/share/doc/alsa-base/README.Debian"
		fi
		rm -f "$tempfile" "$debconfbit" "$filetodumpto"
		if [ ! -f /etc/default/alsa ]; then
			cp /usr/share/alsa-base/alsa-base.conf /etc/default/alsa
		fi
		if [ -f /etc/default/alsa ]; then
			db_get alsa-base/start_oss_layer
			start_oss_layer="$RET"
			db_get alsa-base/force_stop_alsa_before_suspend
			force_stop_modules_before_suspend="$RET"
			db_get alsa-base/alsactl_store_on_shutdown
			alsactl_store_on_shutdown="$RET"
			sed -e "s/startosslayer=.*/startosslayer=$start_oss_layer/" \
			-e "s/force_stop_modules_before_suspend=.*/force_stop_modules_before_suspend=$force_stop_modules_before_suspend/" \
			-e "s/alsactl_store_on_shutdown=.*/alsactl_store_on_shutdown=$alsactl_store_on_shutdown/" /usr/share/alsa-base/alsa-base.conf > \
			/etc/default/alsa.tmp
			if [ -f /etc/default/alsa ]; then
				mv /etc/default/alsa /etc/default/alsa.debconf-backup
			fi
			mv /etc/default/alsa.tmp /etc/default/alsa
		fi
		if dpkg --compare-versions "$2" lt "0.9+0beta10-1"; then
			if [ -f /etc/alsa/modutils/1.0 -a ! -f /etc/alsa/modutils/0.9 ]; then
				echo "Moving ALSA 1.0 configuration file back to ALSA 0.9."
				mv /etc/alsa/modutils/1.0 /etc/alsa/modutils/0.9
			fi
		fi
		if [ -x /usr/sbin/update-devfsd ]; then
			/usr/sbin/update-devfsd >&2
		fi
		if [ ! -c /dev/audio -a ! -c /dev/dsp -a ! -c /dev/amixer ] || [ -h /dev/snd -o ! -d /dev/snd ]; then
			if [ ! -e /dev/.devfsd ]; then
				/usr/share/alsa-base/snddevices > /dev/null
			fi
		fi
		;;
	abort-upgrade|abort-remove|abort-deconfigure)
		exit 0
		;;
	*)
		echo "postinst called with unknown argument \`$1'" >&2;
		exit 0;
	;;
esac

if [ -z "$alsactl_store_on_shutdown" -o "$alsactl_store_on_shutdown" = 'true' ]; then
	alsactl store >/dev/null 2>&1 || true
fi

if [ ! -e /etc/modutils/alsa ]; then
	ln -sf /etc/alsa/modutils/0.9 /etc/modutils/alsa
fi
/sbin/update-modules >/dev/null 2>&1

#DEBHELPER#

if [ $1 = "configure" -a -n "$2" ]; then
	if dpkg --compare-versions $2 lt 0.9.2-7; then
		for i in 0 1 6 ; do
			if [ -h /etc/rc$i.d/K20alsa ]; then
				mv /etc/rc$i.d/K20alsa /etc/rc$i.d/K21alsa
			fi
		done
	fi
fi

# Stolen from ripped out code to stop this fuckage once and for all.
# Reason that debhelper is above: we want the driver running when we try 
# this.

case "$1" in
	configure)
		if dpkg --compare-versions "$2" lt "0.9.0rc5-0.4"; then
			sound_devs_dev=`find /dev -type c 2> /dev/null | \
			xargs ls -l 2> /dev/null | awk -F' ' '{print $5,$10;}' | \
			awk '/^14,/ || /^116,/ {printf "%s\n", $2;}'| sort | uniq | \
			tr '\n' ' '`
			for file in $sound_devs_dev; do
				if [ -O /var/lib/alsa-base/$file -a -e $file ]; then
					perm=$(cat /var/lib/alsa-base/$file)
					rm /var/lib/alsa-base/$file
					chmod $perm $file
				fi
			done
		fi
	;;
esac

