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

# 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
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:-wheezy-test}"

# 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

setup_tasksel_overrides() {
    # Modify tasksels desktop test so the normal Debian desktop
    # profiles are not installed. Use dpkg-divert to to switch to our
    # version
    sed -e 's/if desktop_hardware/# Do not install a desktop - added by debian-edu-install\nunmark\n\n&/' \
        < /usr/lib/tasksel/tests/desktop \
        > /usr/lib/tasksel/tests/desktop.edu
    chmod 755 /usr/lib/tasksel/tests/desktop.edu
    dpkg-divert --package debian-edu-install --rename --quiet \
        --add /usr/lib/tasksel/tests/desktop
    ln -sf ./desktop.edu /usr/lib/tasksel/tests/desktop

    # And for the standard system task too
    sed -e 's/^case/# Do not install standard system task - added by debian-edu-install\nexit 3\n\n&/' \
        < /usr/lib/tasksel/tests/new-install \
        > /usr/lib/tasksel/tests/new-install.edu
    chmod 755 /usr/lib/tasksel/tests/new-install.edu
    dpkg-divert --package debian-edu-install --rename --quiet \
        --add /usr/lib/tasksel/tests/new-install
    ln -sf ./new-install.edu /usr/lib/tasksel/tests/new-install
}

remove_tasksel_overrides() {
    for test in desktop new-install ; do
        file=/usr/lib/tasksel/tests/$test
        if [ -x $file.edu ] ; then
            rm $file
            dpkg-divert --package debian-edu-install \
                --rename --quiet --remove $file
            rm $file.edu
        else
            error "Missing divert for $file."
        fi
    done
}

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

# 1. Add skolelinux related APT sources.
apt-get install -y debian-edu-archive-keyring
cat <<EOF >/etc/apt/sources.list.d/debian-edu.list
deb http://ftp.skolelinux.org/skolelinux $EDUSUITE local
deb-src http://ftp.skolelinux.org/skolelinux $EDUSUITE local
EOF
apt-get update

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

DEBIAN_FRONTEND=noninteractive
export DEBIAN_FRONTEND

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

# 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
if DEBIAN_PRIORITY=critical $aptcmd ; then
    :
else
    remove_tasksel_overrides
    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
