#!/bin/sh
###########################################################################
# Creates a virtual disk for LTSP clients.
#
# Copyright (C) 2010 Alkis Georgopoulos <alkisg@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
###########################################################################

# Source the sch-scripts common functions and variables.
if [ ! -f ./scripts/common/common.sh ]; then
    cd /usr/share/sch-scripts
fi
. ./scripts/common/common.sh

ltsp_version="$(dpkg-query -W -f '${Version}' ltsp-server 2>/dev/null)" || true

if [ -z "$ltsp_version" ]; then
    confirm "Το πακέτο ltsp-server δεν είναι εγκατεστημένο. Θέλετε να το εγκαταστήσετε [Y/N]; "
    apt-get update
    apt-get --yes install ltsp-server
    ltsp_version="$(dpkg-query -W -f '${Version}' ltsp-server 2>/dev/null)" || true
fi

if dpkg --compare-versions "$ltsp_version" lt "5.1.99"; then
    die "Η έκδοση του πακέτου ltsp-server είναι $ltsp_version. Απαιτείται τουλάχιστον η 5.1.99."
fi

if [ ! -f /etc/ltsp/ltsp-build-client.conf ]; then
    die "Το αρχείο /etc/ltsp/ltsp-build-client.conf δε βρέθηκε, παρακαλώ δημιουργήστε το από το αντίστοιχο μενού των sch-scripts."
else
    . /etc/ltsp/ltsp-build-client.conf || die "Συντακτικό σφάλμα στο αρχείο /etc/ltsp/ltsp-build-client.conf."
fi

# The following variables are just for local use, they're not exported
# in the environment, so they don't interfere with ltsp-build-client. 
ARCH=${ARCH:-$(dpkg --print-architecture)}
CHROOT=${CHROOT:-$ARCH}

if [ -d "/opt/ltsp/$CHROOT" ]; then
    confirm "Ο κατάλογος /opt/ltsp/$CHROOT υπάρχει ήδη, θα πρέπει να διαγραφεί εάν θέλετε να φτιάξετε νέο εικονικό δίσκο. Θέλετε να διαγράψετε τον κατάλογο /opt/ltsp/$CHROOT [Y/N]; "
    rm -rf --one-file-system "/opt/ltsp/$CHROOT"
fi

echo "\
Θα δημιουργηθεί ένας εικονικός δίσκος με τις παρακάτω επιλογές:
Αρχιτεκτονική: $ARCH
Κατάλογος: /opt/ltsp/$CHROOT"

if [ -n "$FAT_CLIENT_DESKTOPS" ]; then
    PACKAGE_LISTS=$(echo $PACKAGE_LISTS)
    echo "Τύπος: εικονικός δίσκος για fat clients
Περιβάλλοντα εργασίας: $FAT_CLIENT_DESKTOPS
Λίστες πακέτων που θα εγκατασταθούν: $PACKAGE_LISTS"
else
    echo "Τύπος: εικονικός δίσκος για thin clients"
fi

confirm "Θέλετε σίγουρα να δημιουργήσετε τον εικονικό δίσκο [Y/N]; "
echo "Η διαδικασία δημιουργίας του εικονικού δίσκου ξεκίνησε στις $(date +%H:%M:%S)"
ltsp-build-client
echo "Η διαδικασία δημιουργίας του εικονικού δίσκου ολοκληρώθηκε στις $(date +%H:%M:%S)"
