#!/bin/bash

. /usr/lib/eole/ihm.sh

FOG_INSTALLER_DIR=/opt/fog_installer
FOG_VERSION="dev-branch"
FOG_RELEASE="fog-20221006"

download_source(){
    EchoVert "Téléchargement de FOG $FOG_RELEASE"
    rm -rf "${FOG_INSTALLER_DIR}"
    mkdir -p "${FOG_INSTALLER_DIR}/src"
    wget "http://eole.ac-dijon.fr/workstation/fog/${FOG_RELEASE}.tar.gz" -O "${FOG_INSTALLER_DIR}/src/${FOG_RELEASE}.tar.gz" #> /dev/null
    if [ $? -ne 0 ];then
        EchoRouge "Problème de téléchargement de FOG ${FOG_RELEASE}"
        exit 1
    fi
    wget "http://eole.ac-dijon.fr/workstation/fog/${FOG_RELEASE}.tar.gz.sha256sum" -O "${FOG_INSTALLER_DIR}/src/${FOG_RELEASE}.tar.gz.sha256sum" #> /dev/null
    if [ $? -ne 0 ];then
        EchoRouge "Problème de téléchargement du fichier sha256sum de FOG ${FOG_RELEASE}"
        exit 1
    fi

    cd "${FOG_INSTALLER_DIR}/src"
    sha256sum -c "${FOG_RELEASE}.tar.gz.sha256sum"

    if [ $? -eq 0 ];then
        EchoVert "Somme sha256 validée pour ${FOG_RELEASE}"
        cd "${FOG_INSTALLER_DIR}"
	    tar -xvzf "${FOG_INSTALLER_DIR}/src/${FOG_RELEASE}.tar.gz" >/dev/null 2>&1
    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

grep -q "php_admin_flag" /etc/apache2/sites-available/001-fog.conf
if [ $? -eq 1 ]; then
    sed -i '/<\/Directory>/i\        \php_admin_flag allow_url_fopen On' /etc/apache2/sites-available/001-fog.conf
    /etc/init.d/apache2 reload
fi

unset http_proxy https_proxy

exit 0
