#! /bin/sh

# 01scripted-crypto runs quite early in the finish-install phase (hence the 01)
# because it needs to reformat some volumes before anything can write on them
#
# See run-parts(1) for details on ordering

set -e

. /usr/share/debconf/confmodule

db_get scripted-crypto/enable
enable_crypto=$RET
if [ "$enable_crypto" = "false" ] ; then
    log "Crypto is disabled, exiting"
    exit 0
fi

apt-install scibian-crypt
logger "Generating scibian escrow key : $(cat /proc/sys/kernel/random/entropy_avail) bits of entropy available..."
mount -t proc proc /target/proc || true
mkdir -p /target/run/resolvconf
cp /etc/resolv.conf /target/run/resolvconf/
if ! chroot /target /usr/sbin/scibian-crypt-admin -n -k /tmp/tmpfs/temp_escrow_key ; then
       # Stop everything if the script fails
       logger "scibian-crypt-admin FAILED"
       chroot /target pkill -STOP -f /sbin/debian-installer
       chroot /target pkill -KILL -fx /usr/bin/main-menu
       echo -e "\033c\n\n\n   ERROR: The escrow key generation script failed.\n   Please contact support." > /dev/tty1
fi
