#!/bin/bash

. /usr/lib/eole/ihm.sh

FOG_INSTALLER_DIR=/opt/fog_installer
FOG_VERSION="1.5.9"
FOG_MD5="6b0ec40db0225759bea81f037fad1506"

download_source(){
    rm -rf ${FOG_INSTALLER_DIR}
    mkdir -p ${FOG_INSTALLER_DIR}/src
    wget --no-check-certificate https://github.com/FOGProject/fogproject/archive/${FOG_VERSION}.tar.gz -O ${FOG_INSTALLER_DIR}/src/${FOG_VERSION}.tar.gz > /dev/null
    if [ $? -eq 0 ];then
        EchoVert "FOG ${FOG_VERSION} téléchargé avec succès"
    else
        EchoRouge "Problème de téléchargement de FOG ${FOG_VERSION}"
        exit 1
    fi

    echo "${FOG_MD5} ${FOG_VERSION}.tar.gz" > ${FOG_INSTALLER_DIR}/src/hash
    cd ${FOG_INSTALLER_DIR}/src
    md5sum --check hash > /dev/null 2>&1

    if [ $? -eq 0 ];then
        EchoVert "Somme md5sum validée"
        cd ${FOG_INSTALLER_DIR}
	    tar -xvzf ${FOG_INSTALLER_DIR}/src/${FOG_VERSION}.tar.gz 2>&1 > /dev/null
    else
        EchoRouge 'Problème avec le fichier téléchargé'
        exit 1
    fi
}

_install(){
    if [ $(CreoleGet activer_proxy_client) = "oui" ]; then
        PROXY="$(CreoleGet proxy_client_adresse):$(CreoleGet proxy_client_port)"
        sed -i "s/curl --silent/curl -x ${PROXY} --silent/" ${FOG_INSTALLER_DIR}/fogproject-${FOG_VERSION}/lib/common/functions.sh
    fi
    ${FOG_INSTALLER_DIR}/fogproject-${FOG_VERSION}/bin/installfog.sh -y > ${FOG_INSTALLER_DIR}/installer.log
}

modifier_init(){
    /usr/share/eole/sbin/modify_init
    if [ $? -eq 0 ];then
        EchoVert "init.xz modifiée avec succès"
    else
        EchoRouge "Impossible de modifier init.xz"
        exit 1
    fi
}

installer(){
    EchoOrange "Installation de FOG en cours, le processus est long"
    echo "(details de l'installation ${FOG_INSTALLER_DIR}/installer.log)"
    _install
    if [ $? -eq 0 ];then
        EchoVert "FOG installé avec succès"
    else
        EchoRouge "Problème lors de l'installation"
        exit 1
    fi
}
reconfigurer(){
    EchoOrange "Reconfiguration de FOG en cours, le processus est long"
    echo "(details de la reconfiguration  ${FOG_INSTALLER_DIR}/installer.log)"
    _install
    if [ $? -eq 0 ];then
        EchoVert "FOG reconfiguré avec succès"
    else
        EchoRouge "Problème lors de la reconfiguration"
        exit 1
    fi
}

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

if [ "$1" = "instance" ];then
    if ! [ -d ${FOG_INSTALLER_DIR} ];then
        download_source
    else
        if [ -f ${FOG_INSTALLER_DIR}/fogproject-${FOG_VERSION}/bin/installfog.sh ];then
            Question_ouinon 'FOG semble déjà installé. Voulez-vous le réinstaller ?'
	        if [ $? -eq 0 ];then
	        	download_source
	        fi
        else
            download_source
	    fi
    fi
    installer
    modifier_init

elif [ "$1" = "reconfigure" ]; then
    reconfigurer
    modifier_init
fi

unset http_proxy https_proxy

exit 0


