#!/bin/sh
#
# This is an example script that explain what need to be done to
# transform a freshly installed Debian machine (hopefully without any
# of the Debian Edu related packages already installed) to a Debian
# Edu client.  The packages that was already installed before running
# this script might not get the configuration they would have in a
# normal Debian Edu installation, due to preseeding happening too
# late.
#
# To customize the settings, set environment variables when calling
# the script, for example like this:
#   PROFILE=Main-Server,Workstation \
#     DESKTOP=gnome,lxde \
#     EDUSUITE=squeeze-test
#     ./debian-edu-bless

set -e
set -x

# Abort if the current locale isn't working, the perl warning do not
# show up in /etc/ldap/ldap.conf and similar.
# An alternative is to set 'PERL_BADLANG=0' to quiet down perl.
if perl -e 'print' 2>&1 |grep -q 'locale failed' ; then
    echo "The current locale is invalid.  Please fix before running debian-edu-bless again."
    exit 1
fi

# Allow sudo to be replaced by sudo-ldap in the tasksel step, even if
# no root password is set.  This fixes a installation problem on
# Raspbian.
export SUDO_FORCE_REMOVE=yes

# Set up this (or these) profile(s)
# Possible values, can be combined using comma
#   Main-Server, Workstation, Roaming-Workstation, Thin-Client-Server,
#   Minimal, Standalone
PROFILE="${PROFILE:-Workstation}"

# Desktop environment to use.  Can include several comma separated values.
# Valid values are currently kde, gnome, lxde, xfce
DESKTOP="${DESKTOP:-kde}"

# LANG is the locale to use (should be configured using
# dpkg-reconfigure locales), and LANGUAGE is the ordered set of
# languages to show when showing translated strings.  Here is how
# nb_NO should be using both (the values used by d-i):
# LANG="nb_NO.UTF-8"
# LANGUAGE="nb_NO:nb:no_NO:no:nn_NO:nn:da:sv:en"

# The Debian Edu suite to use to fetch the Debian Edu packages
EDUSUITE="${EDUSUITE:-jessie}"

# Use american english if no language is set in the host system
if [ -z "$LANG" ] ; then
    LANG="en_US.UTF-8"
    export LANG
fi

# Any version string will do.  If an old and official version string
# like terra_alpha is used, it will be replaced by debian-edu-install
# during installation to the current version.
VERSION="terra_alpha"

# Set this if you want to run the testsuite after first boot, and use
# the test version of the APT repositories.
TESTINSTALL="true"

if [ 0 -ne $(id -u) ] ; then
    echo "error: this script need to run as root."
    exit 1
fi

apt_get_harder() {
    aptcmd="$1"
    # First download only, to detect problems with the network.
    if $aptcmd --download-only ; then
	:
    else
	echo "warning: downloading needed packages failed. trying again."
	if $aptcmd --download-only ; then
	    :
	else
	    echo "error: unable to download needed packages, tried twice"
	fi
    fi
    $aptcmd
}

# 0. Install etckeeper, to keep track of changes in /etc/ (optional,
#    highly recommended)
apt-get update
apt_get_harder "apt-get install -y etckeeper"

# 1. Add skolelinux related APT sources if it exist
MIRROR=ftp.skolelinux.org
DIRECTORY=/skolelinux
sourcefile=/etc/apt/sources.list.d/debian-edu.list
if wget --timeout=30 --tries=3 -qO - http://$MIRROR$DIRECTORY/dists/$EDUSUITE/Release | grep -q " local/binary" ; then 
    echo "info: Enabling our local APT source from $MIRROR."
    apt_get_harder "apt-get install -y debian-edu-archive-keyring"
    cat <<EOF > $sourcefile
deb http://$MIRROR$DIRECTORY $EDUSUITE local
deb-src http://$MIRROR$DIRECTORY $EDUSUITE local
EOF
    apt-get update
fi

# 2. Create /etc/debian-edu/config with the wanted configuration.
mkdir -p /etc/debian-edu
cat <<EOF > /etc/debian-edu/config
VERSION="$VERSION"
PROFILE="$PROFILE"
LANGCODE="$LANGUAGE"
LOCALE="$LANG"
EOF

if [ "true" = "$TESTINSTALL" ] ; then
    echo 'TESTINSTALL="true"' >> /etc/debian-edu/config
fi
# 3. Install debian-edu-install to load preseeding values and pull in
#    our configuration.
apt_get_harder "apt-get install -y debian-edu-install"

# 4. Preseed debconf database with profile setup in
#    /etc/debian-edu/config, and run tasksel to install packages
#    according to the profile specified in the config above,
#    overriding some of the Debian automation machinery.
(
    echo debian-edu-install debian-edu-install/profile multiselect "$PROFILE"
    echo tasksel            tasksel/desktop            string      "$DESKTOP"
) | debconf-set-selections
/usr/lib/education-tasks/edu-tasksel-setup setup

DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

echo info: enabled tasksel tasks
echo
tasksel --new-install --list-tasks

aptcmd=$(tasksel --new-install -t | sed 's/debconf-apt-progress -- //')

if DEBIAN_PRIORITY=critical apt_get_harder "$aptcmd" ; then
    /usr/lib/education-tasks/edu-tasksel-setup teardown
else
    /usr/lib/education-tasks/edu-tasksel-setup teardown
    echo "error: installing packages failed"
    exit 1
fi

# 5. Run debian-edu-cfengine-D installation to configure everything
#    that could not be done using preseeding.
cfengine-debian-edu -D installation
etckeeper commit "/etc/ state after running cfengine-debian-edu."

# 6. Ask for a reboot to enable all the configuration changes.
echo "It is now time to reboot. For example by running"
echo "  shutdown -r now 'Into the great wide open'"
exit 0
