#!/bin/sh
#
# This script is called by /etc/init.d/xdebian-edu-firstboot in
# debian-edu-install on the first boot after installation.

if [ -f /etc/debian-edu/config ] ; then
    . /etc/debian-edu/config
fi

log() { logger -t debian-edu-config "$@"; }
info() { log "info: $@"; }
error() { log "error: $@"; }
at_exit() {
    error "script $0 terminated unexpectedly."
}
disable_exception() { trap - INT TERM EXIT; }
trap at_exit INT TERM EXIT

info "Executing run-at-firstboot script."

# fix for skolelinux bug #1355:
# make sure /opt/ltsp/*/etc/ssh/ssh_known_hosts is created
if [ -x /usr/sbin/ltsp-update-sshkeys ]; then
    ltsp-update-sshkeys
fi

# Make sure the LTSP kernels are updated too, in case the initrd was
# upgraded during installation (for example for removing usplash)
if [ -x /usr/sbin/ltsp-update-kernels ]; then
    ltsp-update-kernels
fi

# Make sure resolv.conf in LTSP chroot are updated also on the main
# server where the DNS resolver do not change that often.
if [ -x /usr/share/debian-edu-config/tools/ltsp-update-resolvconf ] ; then
    /usr/share/debian-edu-config/tools/ltsp-update-resolvconf
fi

# Enable all relevant munin plugins now the machine is completely
# configured.  Some of the plugins are not enabled when munin-node is
# installed, because their service is installed after munin-node.
if [ -x /usr/sbin/munin-node-configure ] ; then
    /usr/sbin/munin-node-configure -shell 2>/dev/null | sh || true
    invoke-rc.d munin-node restart || true
fi

# Update sitesummary and munin configuration quickly
if [ -x /usr/sbin/sitesummary-client ] ; then
    info "submitting sitesummary information to server"
    sitesummary-client
    if [ -x /etc/cron.daily/sitesummary ] ; then
	info "updating sitesummary server information"
	/etc/cron.daily/sitesummary

	# Update the munin web pages too
	if [ -x /usr/bin/munin-cron ] ; then
	    info "updating munin web pages"
	    su munin -s /usr/bin/munin-cron
	fi

	# Nagios need a restart to find the new configuration
	if [ -x /etc/init.d/nagios3 ] ; then
	    info "restarting nagios3 service"
	    service nagios3 restart
	fi
    fi
fi

# Fix squid setup after first boot (cf.squid seems to do it only randomly).
if echo "$PROFILE" | grep -q Main-Server && \
    ! [ -d /var/spool/squid/00 ] ; then
    /usr/sbin/dpkg-reconfigure squid
# Update Squid to use all the available space (aka 80% of the partition)
    /usr/share/debian-edu-config/tools/squid-update-cachedir /etc/squid/squid-debian-edu.conf
fi

# Update PXE setup on Main-server with proxy values set in environment
# to make sure the proxy values are passed on to clients.  When set up
# from cfengine, the proxy values are not passed on and missing from
# /etc/debian-edu/www/debian-edu-install.dat
if echo "$PROFILE" | grep -q Main-Server && \
    [ -x /usr/sbin/debian-edu-pxeinstall ] ; then
    if [ -e /etc/environment ] ; then
	. /etc/environment
	export http_proxy ftp_proxy
    fi

    # Try to set from wpad file, as this normally is called from
    # dhcp, and the main-server have static IP setup.
    if [ -z "$http_proxy" ] ; then
	/usr/share/debian-edu-config/tools/update-proxy-from-wpad
	. /etc/environment
	export http_proxy ftp_proxy
    fi

    /usr/sbin/debian-edu-pxeinstall
fi

# Make sure goplay/golearn index is generated on first boot, in case
# the indexing did not complete during the installation.  Otherwise,
# we would have to wait for the weekly cron job to run before golearn
# was usable.
if [ -x /usr/sbin/update-apt-xapian-index ] ; then
    dpkg-reconfigure apt-xapian-index
else
    info "apt-xapian-index/goplay is not installed"
fi

if [ -x /usr/bin/etckeeper ] ; then
    etckeeper commit "End of first boot" > /dev/null 2>&1 || true
fi

for f in /opt/ltsp/*/usr/bin/etckeeper ; do
    if [ -x "$f" ] ; then
	arch=$(echo $f | cut -d/ -f4)
	ltsp-chroot -a "$arch" etckeeper commit "End of first boot" > /dev/null 2>&1 || true
    fi
done

info "done executing run-at-firstboot script."

disable_exception
