#!/bin/sh
#
# This script is run by finish-install after the kernel was chosen. 
# It installs a corresponding loop-aes-$KVERS module if any loop-AES
# encrypted partitions have been configured.
#

. /usr/share/debconf/confmodule

loop_AES=no

for dev in /var/lib/partman/devices/*; do
	[ -d "$dev" ] || continue
	cd $dev
	for id in *; do
		[ -f $id/crypto_type ] || continue
		[ -f $id/crypt_active ] || continue

		type=$(cat $id/crypto_type)		
		if [ "$type" = "loop-AES" ]; then
			loop_AES=yes
		fi
	done
done

if [ $loop_AES = yes ]; then
	cat > /target/etc/default/checkfs-loop <<END
# Check loop-backed file systems at startup?
CHECKFS_LOOP_ENABLE=yes
END

	db_get base-installer/kernel/image
	KVERS=${RET#*-image-}

	failed=yes
	for package in loop-aes-$KVERS loop-aes-modules-$KVERS; do
		if apt-install $package; then
			failed=no
			break
		fi
	done
	
	if [ "$failed" = "yes" ]; then
		templ="partman-crypto/module_package_missing"
		db_fset $templ seen false
		db_subst $templ PACKAGE loop-aes-modules-$KVERS
		db_input critical $templ
		db_go
		exit 1
	fi
fi

