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

# Enable debconf
. /usr/share/debconf/confmodule

# Grab dist value for both testing and stable release cases.
if grep -q / /etc/debian_version ; then
	dist=$(cat /etc/debian_version | cut -d/ -f1)
else
	dist=$(lsb_release -sc)
fi
# Set LTSP image name (diskless workstations).
ltspimg="$(uname -m).img"

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."

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

# Create SquashFS image for diskless workstations. This is done here because
# information from some daemons isn't available during installation. It's done
# for a combined server but not for a separate LTSP server because the image
# needs to include the krb5.keytab file which isn't available at this time.
if echo "$PROFILE" | grep -Eq 'Main-Server.*LTSP-Server' && \
	[ ! -f /srv/ltsp/images/$ltspimg ] ; then
	/usr/sbin/debian-edu-ltsp-install --diskless_workstation yes
fi

# Update PXE setup on LTSP servers 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 in case of a combined server.
if echo "$PROFILE" | grep -q LTSP-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 has a 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.

(Sent from the Debian Edu first boot script.)

EOF
	logger -t exim-create-environment -p notice Sent mail to first-user.
fi

# Create first user's Samba account. The smbpasswd command fails inside d-i
# because user information from LDAP is missing at that time. All passwords have
# been cleared from the debconf database inside d-i, except the one needed here.
if echo "$PROFILE" | grep -q Main-Server ; then
	db_get debian-edu-config/first-user-password
	FIRSTUSERPWD="$RET"
	(echo "$FIRSTUSERPWD"; echo "$FIRSTUSERPWD") | smbpasswd -a -s $FIRSTUSER
	info "Added Samba account for '$FIRSTUSER'."
	db_set debian-edu-config/first-user-password ''
	info "First user password cleared from debconf database."
fi

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

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

disable_exception
