#!/bin/sh
#
# This script is executed within the debian-installer environment when
# finish-install.d is executed, which is executed at the end of the
# installation after the cfengine-debian-edu script has executed

set -e

# export DEBCONF_DEBUG='developer'
. /usr/share/debconf/confmodule

log() {
    logger -t debian-edu-config-finish-install "$@"
}

# Register changes before and after cfengine is executed, to make it
# easier to track our changes
edu-etcvcs commit

# Try to add entropy when running low
(
   cd /
   while true ; do
       entropy="$(cat /proc/sys/kernel/random/entropy_avail)"
       if [ 100 -gt "$entropy" ] ; then
           log "low on entropy ($entropy), trying to add more"
           # Disk IO add entropy to the kernel.  Flush cache to ensure
           # find and touch/rm causes disk IO.
           sync
           echo 3 > /proc/sys/vm/drop_caches
           find /target > /dev/null 2>&1
           touch /target/var/tmp/foo
           sync
           rm /target/var/tmp/foo
           sync
       fi
       sleep 120
   done
) < /dev/null > /dev/null 2>&1 3>&1 4>&1 5>&1 6>&1 &

# Make the installation look more like a finished system, to make sure
# debconf-get-selections --installer work.
. /usr/lib/finish-install.d/94save-logs

# Update configuration for everything that could not be preseeded
in-target cfengine-debian-edu -D installation || true

edu-etcvcs commit

# Make sure the root password is gone from debconf after the
# installation, in case ldap-debian-edu-install and kerberos-init-kdc 
# scripts failed when cfengine was executed.
db_set debian-edu-config/ldap-password ''
db_set debian-edu-config/ldap-password-again ''
db_set debian-edu-config/kdc-password ''
db_set debian-edu-config/kdc-password-again ''
log "info: Ensuring KDC and LDAP passwords are cleared from debconf database"

