#!/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."

# 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

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

# Update Squid to use all the available space (aka 80% of the partition)
if echo "$PROFILE" | grep -q Main-Server ; then
    /usr/share/debian-edu-config/tools/squid-update-cachedir
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 ! echo "$http_proxy" | grep -q webcache ; 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

# Send mail to the first user to avoid the Dovecot permission pitfall
# also in this special case. It doesn't seem to work during installation,
# because Exim4 needs to grab information from LDAP which fails at that time.
FIRSTUSER=$(grep -1 first-user-name /var/cache/debconf/config.dat | grep Value | cut -d' ' -f2)

if [ ! -d /var/mail/"$FIRSTUSER" ] ; then
    cat << EOF | /usr/lib/sendmail $FIRSTUSER
Subject: Welcome to the mail-system

Hello $FIRSTUSER,

welcome to the mail-system.

Your userID is $FIRSTUSER, and your email address is:

    $FIRSTUSER@postoffice.intern

Regards,

    Debian-Edu SysAdmin

EOF
    logger -t exim-create-environment -p notice Sent mail to first-user.
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
