#!/usr/bin/env bash

. /usr/lib/eole/ihm.sh

LOG_LTSP=/var/log/ltsp-cluster-lbserver.log

if [ "$(CreoleGet activer_proxy_client)" = "oui" ]; then
    proxy_client_adresse=$(CreoleGet proxy_client_adresse)
    proxy_client_port=$(CreoleGet proxy_client_port)
    export http_proxy="http://$proxy_client_adresse:$proxy_client_port/"
    export ftp_proxy="http://$proxy_client_adresse:$proxy_client_port/"
fi

option=${1}

container_path_ltspserver=$(CreoleGet container_path_ltspserver)
if [ "${container_path_ltspserver}" = "" ];then
    CHROOT="/"
else
    CHROOT=${container_path_ltspserver}
fi
BASE="/opt/ltsp/i386"
THIN_CLIENT_BASE=${CHROOT}/${BASE}

UPDATE_IMAGE=0
if [ ! -d ${CHROOT}/$BASE/ ];then
    UPDATE_IMAGE=1
elif [ "$option" = 'instance' ];then
    echo
    Question_ouinon "Voulez-vous re-générer l'image des clients légers" 'non' 'warn'
    if [ $? -eq 0 ];then
        rm -rf ${THIN_CLIENT_BASE}
        UPDATE_IMAGE=1
    fi
fi

if [ $UPDATE_IMAGE -eq 1 ];then
    echo
    if [ ! -f ${CHROOT}/var/log/syslog ];then
        #Fake syslog
        touch ${CHROOT}/var/log/syslog
        TMP_FAKE_SYSLOG=1
    fi
    . ${CHROOT}/etc/ltsp/ltsp-build-client.conf
    EchoVert "Vérification des paquets supplémentaires à installer"
    for pkg in $LATE_PACKAGES;do
        apt-get install -s $pkg > /dev/null 2>&1
        if [ $? != 0 ];then
            EchoRouge "Le paquet $pkg n'a pas été trouvé";
            EchoRouge "$pkg est retiré des paquets à installer";
        else
            GOOD="$GOOD $pkg";
        fi;
    done
    echo
    EchoVert "* Génération de l'image des clients légers"
    chroot ${CHROOT} ltsp-build-client --arch i386 --skipimage --late-packages "$GOOD"
    if [ $TMP_FAKE_SYSLOG == '1' ];then
        #Remove fake syslog
        rm ${CHROOT}/var/log/syslog
    fi
fi

#generation du fichier $BASE/etc/ltsp/getltscfg-cluster.conf
if [ ! -d "${THIN_CLIENT_BASE}/etc/ltsp/" ];then
    mkdir -p "${THIN_CLIENT_BASE}/etc/ltsp/"
fi
echo "#Loadbalancer
SERVER=$(CreoleGet adresse_ip_ltsp_link)
PORT=80
ENABLE_SSL=N
INVENTORY=Y
TIMEOUT=3
" > ${THIN_CLIENT_BASE}/etc/ltsp/getltscfg-cluster.conf

#Ajout resolution DNS
cat /etc/resolv.conf > ${THIN_CLIENT_BASE}/etc/resolv.conf

#On cree le fichier /var/lib/tftpboot/ltspi386/lts.conf
#ref #2777
if [ ! -d ${CHROOT}/var/lib/tftpboot/ltsp/i386/ ];then
    mkdir -p ${CHROOT}/var/lib/tftpboot/ltsp/i386/
fi
echo "[default]" > ${CHROOT}/var/lib/tftpboot/ltsp/i386/lts.conf
echo "LOCAL_APPS_EXTRAMOUNTS=/media,/opt,/home" >> ${CHROOT}/var/lib/tftpboot/ltsp/i386/lts.conf

if [ ! -f ${THIN_CLIENT_BASE}/etc/apt/sources.list.d/eole.list ];then
#Ajout des depots EOLE pour gaspscho-client
echo "#Depots EOLE supplementaires
deb http://test-eoleng.ac-dijon.fr/eoleng/ eole-2.3/all/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-security/all/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-security/i386/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-updates/all/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-updates/i386/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-proposed/all/
deb http://test-eoleng.ac-dijon.fr/eoleng eole-2.3-proposed/i386/
" > ${THIN_CLIENT_BASE}/etc/apt/sources.list.d/eole.list
    chroot ${THIN_CLIENT_BASE} apt-get update > /dev/null 2>&1
    chroot ${THIN_CLIENT_BASE} apt-get install -y --force-yes ubuntu-keyring > /dev/null >1
fi

#Ajout de la commande add-apt-key dans l'image embarquée
chroot ${THIN_CLIENT_BASE} apt-get install add-apt-key

chroot ${THIN_CLIENT_BASE} apt-key adv --recv-keys --keyserver keyserver.ubuntu.com CE49EC21
chroot ${THIN_CLIENT_BASE} echo "deb http://ppa.launchpad.net/mozillateam/firefox-next/ubuntu lucid main" > ${THIN_CLIENT_BASE}/etc/apt/sources.list.d/mozilla-ppa.list

#Installation de la partie clients d'epoptes
EchoVert "* Installation du client Epoptes"
#chroot ${THIN_CLIENT_BASE} add-apt-repository ppa:epoptes/ppa > /dev/null
chroot ${THIN_CLIENT_BASE} apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0350B375
chroot ${THIN_CLIENT_BASE} echo "deb http://ppa.launchpad.net/epoptes/ppa/ubuntu lucid main" > ${THIN_CLIENT_BASE}/etc/apt/sources.list.d/epoptes-ppa.list
chroot ${THIN_CLIENT_BASE} apt-get update > /dev/null >1
chroot ${THIN_CLIENT_BASE} apt-get install -y epoptes-client > /dev/null >1
chroot ${THIN_CLIENT_BASE} sed -ri "s/#?SERVER=(.*)$/SERVER=$(CreoleGet adresse_ip_application_link)/" "/etc/default/epoptes-client" > /dev/null >1
chroot ${THIN_CLIENT_BASE} epoptes-client -c > /dev/null >1

#Mise en place du theme de démarrage eole pour le client léger
EchoVert "* Mise en place du thème de démarrage EOLE sur les clients légers"
cp -ar /lib/plymouth/themes/eole-logo ${THIN_CLIENT_BASE}/lib/plymouth/themes/ > /dev/null >1
chroot ${THIN_CLIENT_BASE} /usr/sbin/update-alternatives --install /lib/plymouth/themes/default.plymouth default.plymouth /lib/plymouth/themes/eole-logo/eole-logo.plymouth 100 > /dev/null >1
chroot ${THIN_CLIENT_BASE} /bin/mount -t proc proc /proc > /dev/null >1
chroot ${THIN_CLIENT_BASE} /usr/sbin/update-initramfs -u > /dev/null >1
chroot ${THIN_CLIENT_BASE} /bin/umount /proc > /dev/null >1
chroot ${CHROOT} /usr/sbin/ltsp-update-kernels > /dev/null >1

#Installation du menu des applis embarquées
chroot ${THIN_CLIENT_BASE} ltsp-genmenu install > /dev/null >1

if [ $(CreoleGet activer_gaspacho_agent) = "oui" ];then
    chroot ${THIN_CLIENT_BASE} apt-get install -y --force-yes gaspacho-agent > /dev/null >1
fi
echo
if [ $UPDATE_IMAGE -eq 1 ] || [ "$option" == '--force-update' ];then
    EchoVert "* Génération de l'image compressée (veuillez patienter)"
    chroot ${CHROOT} ltsp-update-image -a i386
else
    EchoOrange " Pas de re-génération de l'image compressée"
    EchoOrange " Pour la re-générer utiliser : Maj-Ltsp-Eole"
    echo
fi
#Fix #3857
chroot ${CHROOT} chmod 644  /var/lib/tftpboot/ltsp/i386/vmlinuz

# FIXME 2.3
##retrait de la dependance sur le reseau pour le demarrage du servide tftp
#sed -i 's/start on (filesystem/start on filesystem/' ${CHROOT}/etc/init/tftpd-hpa.conf > /dev/null >1
#sed -i 's/and net-device-up IFACE!=lo)//' ${CHROOT}/etc/init/tftpd-hpa.conf > /dev/null >1

# FIXME 2.3
#if [ "$(CreoleGet mode_conteneur_actif)" = "oui" ]; then
#    #remonter des logs en mode non-conteneur
#    if [ ! -L ${LOG_LTSP} ]; then
#        rm -rf ${LOG_LTSP}
#        ln -s ${CHROOT}/${LOG_LTSP} ${LOG_LTSP}
#    fi
#fi

# Redémarrage proposé à chaque instance
REBOOT_FILE=/var/run/reboot-required
if [ "$option" = 'instance' ] && [ ! -f ${REBOOT_FILE} ];then
    echo "instance sur Eclair" > ${REBOOT_FILE}
fi

exit 0
