#!/bin/sh
# NOTE: Actual install (ltsp-update-sshkeys + ltsp-update-image +
# ltsp-update kernsl) is done through the ltsp-server package.
. /usr/share/debconf/confmodule

# Check that it's an LTSP install
db_get ubiquity/edu-target_target
INSTALL="$RET"

# Get LTSP interface
db_get ubiquity/edu-auth_ltsp_interface
INTERFACE="$RET"

# Get desktop session
db_get ubiquity/edu-packages_desktop
DESKTOP="$RET"

if [ "$INSTALL" = "termserver" ]; then
    # Install the required packages
    apt-install ltsp-server-standalone

    # Copy the chroot
    mkdir -p /target/opt/ltsp/ /opt/ltsp/i386
    mount /cdrom/casper/ltsp.squashfs /opt/ltsp/i386 -o loop
    cp -r /opt/ltsp/i386 /target/opt/ltsp

    # Copy host settings to chroot
    if [ -f /etc/resolv.conf ]; then
        cp /etc/resolv.conf /opt/ltsp/i386/etc/
    fi
    cp /etc/apt/sources.list /opt/ltsp/i386/etc/apt/

    # Configuring network
    cat >> /target/etc/network/interfaces <<EOF
auto $INTERFACE
iface $INTERFACE inet static
  address 192.168.0.1
  netmask 255.255.255.0
EOF

    # Set initial lts.conf:
    # The directory doesn't exist yet, since ltsp-update-kernels have
    # not run yet, so we need to do so here
    mkdir -p /target/var/lib/tftpboot/ltsp/i386
    cat >> /target/var/lib/tftpboot/ltsp/i386/lts.conf << EOF
# lts.conf, provided by Edubuntu installer
# see http://www.edubuntu.org/documentation for more information

[default]
    LDM_THEME=edubuntu
    LANG=en_US.UTF-8
    LANGUAGE=$LANG
    LDM_LANGUAGE=$LANG
    LDM_SESSION=$DESKTOP
EOF

    # Let ltsp-server's postinst know what to do
    touch /target/etc/edubuntu_ltsp_install

    # Cleaning up
    umount -l /opt/ltsp/i386
fi
