#!/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. It includes running cf-agent for final configuration.

set -e

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

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

# 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
}

# Prevent d-i to delete configured target interface file
preserve_network_interfaces() {
    file=/usr/lib/finish-install.d/55netcfg-copy-config
    if [ -x $file ] ; then
	log "Keep already configured interface file"
	cat /dev/null > $file
    fi
}

db_get debian-edu-install/profile
PROFILE="$RET"

# Register changes before and after cf-agent 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 || true
           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 20
   done
) < /dev/null 2>&1 3>/dev/null 4>&3 5>&3 6>&3 | logger -t edu-entropy-add &
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

# Setup Cfengine3 environment.
if in-target /usr/share/debian-edu-config/tools/setup-cfengine3 ; then
    log "info: Running 'setup-cfengine3' succeeded"
else
    log "error: '/usr/share/debian-edu-config/tools/setup-cfengine3' is missing"
fi

# Update configuration via cf-agent for everything that could not be preseeded.
if in-target /bin/sh -c "cf-agent -I 2>&1 > /var/log/installer/cfengine-edu-install.log" ; then
    :
else
    log "error: Running 'cf-agent -I' failed to work at some point"
fi

edu-etcvcs commit

# Clean up changes done to tasksel in pre-pkgsel and go back to the
# default behavior.
in-target /usr/lib/education-tasks/edu-tasksel-setup teardown

preserve_network_interfaces
deconfigure_network

# Adjust /etc/network/interfaces eth0 entry once available (d-e-c postinst is
# run too early). Adds 'post-up /usr/sbin/update-hostname-from-ip' conditionally
# via the postinst script to the eth0 entry.
in-target /usr/sbin/dpkg-reconfigure -u --no-reload debian-edu-config || true

edu-etcvcs commit

# For non-Main-Server profiles, 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.  Main-Server will become its
# own collector, so no need to try to submit here.
if echo $PROFILE | grep -q Main-Server ; then
    :
elif [ -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 cf-agent 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

