#!/bin/sh

set -e

modulesdb=/var/lib/alsa-base/modules/_MAJORVERSION_

case "$1" in
	configure)
		if [ ! -f "$modulesdb" ]; then
	   	    echo "alsa-modules-_KVERS_" > "$modulesdb"
		elif ! grep -Eq "^alsa-modules-_KVERS_$" "$modulesdb"; then
	  	    echo "alsa-modules-_KVERS_" >> "$modulesdb"
		fi 
		;;
	abort-upgrade|abort-remove|abort-deconfigure)
		exit 0
	;;
	*)
		echo "postinst called with unknown argument: $1" >&2
		exit 0
	;;
esac

if [ -s /etc/alsa/modutils/_MAJORVERSION_ ]; then
	echo "Configuration file for ALSA _MAJORVERSION_ exists."
	if [ $(uname -r) = "_KVERS_" ]; then
		echo "Attempting to start."
		echo
		depmod -a
		invoke-rc.d alsa restart
	fi
else
	echo "No configuration file found for ALSA _MAJORVERSION_."
	egfile="/usr/share/alsa-base/modules-snippet.conf"
	if [ -f "$egfile" ]; then
		echo "Copying example file to /etc/alsa/modutils/_MAJORVERSION_."
		cp "$egfile" /etc/alsa/modutils/_MAJORVERSION_
	fi
fi

exit 0

