#!/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 "$@"
}

# Clean up changes done to tasksel in pre-pkgsel and go back to the
# default behavior.
remove_tasksel_overrides() {
    for test in desktop new-install ; do
	file=/usr/lib/tasksel/tests/$test
	if [ -x /target$file.edu ] ; then
	    rm /target$file
	    chroot /target dpkg-divert --package debian-edu-install \
		--rename --quiet --remove $file
	    rm /target$file.edu
	else
	    error "Missing divert for $file."
	fi
    done
}

# Make sure any dhclient processes started in pre-pkgsel is stopped to
# make sure /var/ is umounted and clean on first boot.
deconfigure_network() {
    if [ -e /tmp/debian-edu-nonetwork ] ; then
	in-target /bin/sh -c "/etc/init.d/networking stop" || true
    fi
}

# 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 [ 130 -gt "$entropy" ] ; then
           log "low on entropy, pool is $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
           entropy="$(cat /proc/sys/kernel/random/entropy_avail)"
           log "entropy pool is $entropy after trying to add"
       fi
       sleep 120
   done
) < /dev/null > /dev/null 2>&1 3>&1 4>&1 5>&1 6>&1 &
epid=$!

# 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

remove_tasksel_overrides
deconfigure_network

edu-etcvcs commit

# Try to submit to sitesummary at the end of the installation, to try
# to avoid one extra boot to update hostname from DNS after updating
# GOsa.
if [ -x /target/usr/sbin/sitesummary-client ] ; then
    in-target /usr/sbin/sitesummary-client || true
fi

# 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.
# Ignore errors as these templates do not exist on standalone installs
db_set debian-edu-config/ldap-password '' || true
db_set debian-edu-config/ldap-password-again '' || true
db_set debian-edu-config/kdc-password '' || true
db_set debian-edu-config/kdc-password-again '' || true
log "info: Ensuring KDC and LDAP passwords are cleared from debconf database"

# Ignore errors in case the entropy gathering is no longer running
if kill $epid ; then
    :
else
    log "error: killing the entropy gathering job failed - exited?"
fi

echo "info: processes using mount point below /target"
mountpoints="$(grep " /target" /proc/mounts | cut -d" " -f2 | sed s%/target%%g)"
LANG=C chroot /target fuser -mv $mountpoints 2>&1 | sed 's/^/info: /'

if LANG=C chroot /target fuser -mv $mountpoints 2>&1 | egrep -qv 'USER|mount |Cannot open ' ; then
    log "error: some processes blocking d-i from umounting /target/"
fi
