#!/bin/sh

set -e

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

if [ "$1" = "remove" ]; then
	if [ $(uname -r) = "_KVERS_" ]; then
		invoke-rc.d alsa stop
	fi
fi

if [ "$1" = "purge" ]; then
	if [ -f "$modulesdb" ]; then
    	grep -v alsa-modules-_KVERS_ "$modulesdb" > "$modulesdb.bak" || true
    	mv "$modulesdb.bak" "$modulesdb"
    	if [ ! -s "$modulesdb" ]; then
			rm -rf /var/lib/alsa-base/modules
		fi
	fi

	if [ -f /etc/alsa/modutils/_MAJORVERSION_ -a ! -f "$modulesdb" ]; then
		rm /etc/alsa/modutils/_MAJORVERSION_
	fi
fi

exit 0

