#!/bin/bash
#Script d'installation d'ecoStations
#Jérôme Labriet 2019 (jl@ac-besancon.fr)

# ATTENTION ce script est pour information, dans le cas d'une utilisation hors académie de Besançon il faut renseigner les variables d'Initialisation !

######################
# Initialisation
######################
. /usr/share/ciab/proc/FonctionsUSCIAB
FU_VersionEOLE												#EOLE_VERSION : Version EOLE
FU_VarEole numero_etab										#numero_etab : RNE
FU_VarEole domaine_messagerie_etab							#domaine_messagerie_etab : NDE (ex : clg-saint-pierre.ac-besancon.fr)

NDE="$domaine_messagerie_etab"
NOW=`date '+%d/%m/%y %H:%M:%S'`
service="InstEcostations"
logfile="/var/log/ciab/InstEcostations.log"					#Modifier si besoin le chemin du log
ecostations_dir="/var/www/html/outils/ecoStations/"

######################
# Fonction de redirection des logs
######################
LOG(){
    if [ "$silencieux" == "" ]
	then
		echo -e "$1"
	fi
    echo -e "$1" >> $logfile
}

####################
# test de la version
####################
if [ "$EOLE_VERSION" != "2.3" -a "$EOLE_VERSION" != "2.5" -a "$EOLE_VERSION" != "2.6" -a "$EOLE_VERSION" != "2.7" ]
then
	LOG "$service : Version d'EOLE non supportée ou indéterminée ($EOLE_VERSION)"
	LOG "$service : > Abandon de $service"
	exit 1
fi
echo "Le $NOW ($USER) --   Execution de $service" >> /var/log/ciab/ciab.log
LOG "Le $NOW ($USER) --   Execution de $service"

######################
# Nettoyage de /tmp
######################
[ -d /tmp/ecoStations ] && rm -rf /tmp/ecoStations
[ -f /tmp/ecoStations.tar.gz ] && rm -rf /tmp/ecoStations.tar.gz
cd /tmp/

#############
# wakeonlan
#############
LOG "$service : Vérification de la présence de wakeonlan ..."
if ! dpkg -l | grep -q wakeonlan
then
	apt-get install wakeonlan && LOG "$service : ... wakeonlan absent => installé par apt-get"
else
	LOG "$service : ... Déjà installé !"
fi

#################
# Téléchargement
#################
LOG "$service : Téléchargement d'ecoStations"
wget --no-check-certificate --http-user=ciab --http-password=colibri http://scribe2.ac-besancon.fr/scribe2/ZIP/ecoStations.tar.gz
if [ ! -f ecoStations.tar.gz ]
then
	LOG "$service : Problème : ecoStations.tar.gz n'a pas pu être téléchargé !"
	LOG "$service : Abandon de l'installation !"
	exit
else
	LOG "$service : Téléchargement réussi OK"
fi

###############
# Décompression
###############
LOG "$service : Décompression de ecoStations.tar.gz ..."
tar zxf ecoStations.tar.gz
if [ ! -f /tmp/ecoStations/outils/ecoStations/index.php ]
then
  LOG "$service : Problème : le fichier ecoStations.tar.gz n'a pas pu être décompressé !"
  LOG "$service : Abandon de l'installation !"
  exit
fi

####################
# Création de la base mysql
####################
LOG "$service : Création de la base mysql..."
muePASS=`date +%s | sha256sum | base64 | head -c5`
muePASS+=`shuf -i 111-999 -n 1`
RECONF1=$(cat  << RECONF
CREATE DATABASE IF NOT EXISTS ecoStations;
GRANT ALL PRIVILEGES ON ecoStations.* TO ecostations@localhost IDENTIFIED BY '$muePASS';
FLUSH PRIVILEGES;
RECONF
)
mysql --defaults-file=/etc/mysql/debian.cnf mysql -Bse "$RECONF1"
[ ! $? = 0 ] && {
    LOG "$service : PROBLEME : La création de la base ecostations retourne une erreur."
}

####################
# Copie des fichiers
####################
LOG "$service : Copie des fichiers de l'application ..."
cp -f /tmp/ecoStations/sudoers.d/* /etc/sudoers.d/
if [ ! -f /etc/sudoers.d/ecostations ]
 then
    LOG "$service : Problème de copie du sudo !"
    LOG "$service : Abandon de l'installation !"
    exit
 fi
LOG "$service : ... Copie du sudo : OK"

if [ -d /var/www/html/outils ]
then
	LOG "$service : Le répertoire /var/www/html/outils existe déjà : OK"
else
    mkdir /var/www/html/outils
    LOG "$service : Création du répertoire /var/www/html/outils : OK"
fi

cp -Rf /tmp/ecoStations/outils/* /var/www/html/outils/
if [ ! -f "${ecostations_dir}index.php" ]
then
    LOG "$service : Problème de copie de l'application web !"
	LOG "$service : Abandon de l'installation"
 	exit
fi
LOG "$service : Copie de l'application : OK"

# if [ "$EOLE_VERSION" == "2.3" ]
# then
	# cp -f /tmp/ecoStations/23/copieFicMachinesDB.pl /var/www/html/outils/ecoStations/
# fi
# if [ "$EOLE_VERSION" == "2.5" ]
# then
	# cp -f /tmp/ecoStations/25/copieFicMachinesDB.pl /var/www/html/outils/ecoStations/
# fi
# if [ ! -f /var/www/html/outils/ecoStations/copieFicMachinesDB.pl ]
# then
    # LOG "$service : Problème de copie de copieFicMachinesDB.pl !"
	# LOG "$service : Abandon de l'installation !"
	# exit
# fi
# LOG "$service : Copie de copieFicMachinesDB.pl : OK"

cp -f /tmp/ecoStations/sites-enabled/* /etc/apache2/sites-enabled/
if [ ! -f /etc/apache2/sites-enabled/apache-outils.conf ]
then
    LOG "$service : Problème de copie de la config d'outils !"
	LOG "$service : Abandon de l'installation !"
	exit
fi
LOG "$service : Copie de la config apache d'outils : OK"

if [ "$EOLE_VERSION" != "2.3" ]
then
	cp -f /tmp/ecoStations/netlogon/* /home/netlogon/
	if [ ! -f /home/netlogon/annule-ecostation.exe ]
	then
		LOG "$service : Problème de copie de l'exe d'annulation !"
		LOG "$service : Abandon de l'installation !"
		exit
	fi
	LOG "$service : Copie de l'exe d'annulation : OK"
fi

cp -f /tmp/ecoStations/icone/* /usr/share/ciab/install/param/icones/rac/
cp -f /tmp/ecoStations/icone/* /home/netlogon/icones/grp_eole/DomainAdmins/Bureau/
if [ ! -f /usr/share/ciab/install/param/icones/rac/ecoStations.url ]
then
    LOG "$service : Problème de copie de l'icône de l'application ecoStations !"
else
    LOG "$service : Copie de l'icone de l'application ecoStations : OK"
fi

cp -f /tmp/ecoStations/images/ecoStations.png /var/www/html/posh-profil/images/icon/
cp -f /tmp/ecoStations/images/ecoStations.ico /usr/share/ciab/install/param/icones/ico/
if [ ! -f /var/www/html/posh-profil/images/icon/ecoStations.png ]
then
    LOG "$service : Problème de copie de l'image de l'application ecoStations !"
else
    LOG "$service : Copie de l'image de l'application ecoStations : OK"
fi

###################
# Remise des droits
###################
LOG "$service : Application des droits sur les dossiers ou fichiers de l'application ..."

chown -R root:www-data $ecostations_dir
chmod -R 750 $ecostations_dir
chown -R root:www-data "${ecostations_dir}log/"
chmod -R 770 "${ecostations_dir}log/"
chown -R root:www-data "${ecostations_dir}cron/"
chmod -R 770 "${ecostations_dir}cron/"
chown -R root:www-data "${ecostations_dir}config/"
chmod -R 770 "${ecostations_dir}config/"

chown root:professeurs /home/netlogon/icones/grp_eole/DomainAdmins/Bureau/*
chmod 770 /home/netlogon/icones/grp_eole/DomainAdmins/Bureau/*
chown root:www-data /var/www/html/posh-profil/images/icon/*
chmod 640 /var/www/html/posh-profil/images/icon/*
chmod 755 /usr/share/ciab/install/param/icones/rac/*

chmod 440 /etc/sudoers.d/ecostations

if [ "$EOLE_VERSION" != "2.3" ]
then
	chmod 644 /home/netlogon/annule-ecostation.exe
fi
	
#####################
# Redémarrage de cron
#####################
/usr/bin/crontab -l|crontab -
#/etc/init.d/cron restart
service cron restart
LOG "$service : Redémarrage du cron : OK"

########################
# Mise du fuseau horaire
########################
echo Euroge/Paris > /etc/timezone
/bin/cp -rf /usr/share/zoneinfo/Europe/Paris /etc/localtime
LOG "$service : Fuseau Euroge-Paris : OK"

####################
# Création et association du nouvel item
####################
if [ "$1" != "-noitem" ]
then
	if [ "$EOLE_VERSION" == "2.3" ]
	then
		/usr/share/ciab/enoe/proc/Item_add ecoStations ecoStations /outils/ecoStations ecoStations.png 2
	fi
	if [ "$EOLE_VERSION" == "2.5" ]
	then
		/usr/share/ciab/enoe/proc/EpItem_add ecoStations ecoStations /outils/ecostations ecoStations.png 2
	fi
fi

####################
# Finalisation
####################
#Insertion du pwd aléatoire (par étab) pour le user ecostations
sed -i '/pass/ c\$pass="'"$muePASS"'";'  /var/www/html/outils/ecoStations/config/db.inc.php

#Nettoyages
LOG "$service : Nettoyage."
rm -rf /tmp/ecoStations
rm -rf /tmp/ecoStations.tar.gz

######################
# That's all folks
######################
LOG "$NOW ($USER) -- Fin de $service -- Consultez $logfile si besoin"