#!/bin/bash
# LIBRICIEL SCOP
# 29/03/2019
# v1.0

function testprogramispresent(){
	PROGRAM=$1
	echo "Controle si le programme ${PROGRAM} est present"
	command -v ${PROGRAM} >/dev/null 2>&1 || { echo >&2 "$(tput setaf 1)Le programme ${PROGRAM} doit etre installe"; exit 1; }
}

function comparehash(){
	echo "Calcul hash $1"
	HASHFILEPRESENT=`sha256sum $1 | awk '{print $1}'`
	echo "Le HASH est ${HASHFILEPRESENT}"
	HASHTOCOMPARE=$2
	if [ $HASHTOCOMPARE == $HASHFILEPRESENT ]
	then
		echo "identique"
		# 0 = true
		return 0
	else
		# 1 = false
		return 1
	fi
}


function applypatch(){
	FILEPRESENT=$1
	COPYFILEPRESENT="${FILEPRESENT}.old"
	echo "Copie du fichier ${FILEPRESENT} vers ${COPYFILEPRESENT}"
	cp -a ${FILEPRESENT} ${COPYFILEPRESENT}
	PATCHFILETMP="/tmp/${SLFILE}.$(date +%s)"
	wget -O ${PATCHFILETMP} $SLFILEPATH 
	cp ${PATCHFILETMP} ${FILEPRESENT}
	cp --attributes-only ${COPYFILEPRESENT} ${FILEPRESENT}
}

################
#  VARIABLES   #
################

SLFILE="s2lowcomponent.php"
SLFILEPATH="https://ressources.libriciel.fr/public/webdelib/patch-pj/$SLFILE"
BACKUPDIR="/data/backup/"
WDPATH="/var/www"
OLDSHA="98cdd40399271cfac1997d64f5348e7b81c4666375d8439a8f3598aeabccaf98"
NEWSHA="bb150e6ecd977d1afe975eaaf6b2e450b649a0eca538c0bca5cd9a4cee634f5b"


#######################
DATE="$(date +%d-%m-%Y)"
BACKUP="$BACKUPDIR/dump-$DBPROD-$DATE.sql"

### test avant lancement du script
if [ $UID -ne 0 ];
  then
  	echo "$(tput setaf 1)Le script doit être lancé en tant qu'utilisateur root!"
        echo "$(tput sgr0)"
        exit 1
fi

        echo ""
        echo "$(tput setaf 1)++---------------------------------------------++"
        echo "++  script de patch PJ actes Webdelib	       ++"
        echo "++---------------------------------------------++"
        echo ""
        echo "$(tput sgr0)"
        echo "$(tput setaf 2)recherche des fichiers a modifier sur le serveur $(hostname)"
        echo "$(tput sgr0)"



LISTEPROGRAMMENECESSAIRES="awk cp sha256sum wget date"
for PROG in ${LISTEPROGRAMMENECESSAIRES}
do
	testprogramispresent $PROG
done

# TODO :
# verification de version : 5.0.x
# remplacement du footer pour indiquer passage du patch en fin de script
# test a finaliser
# mise en place backup sql


for SHAFILES in $(find $WDPATH -name "S2lowComponent.php");
do
	echo $SHAFILES;
	if comparehash $SHAFILES $NEWSHA
	then
        	echo "$(tput setaf 2)Patch déja en place"
	        echo "$(tput sgr0)"
	else
		if comparehash $SHAFILES $OLDSHA
		then
			echo "$(tput setaf 2)Appliquer le patch ?"
			read -p "Continuer (o/n)?" choice
			case "$choice" in
			  o|O ) echo "oui"
				applypatch $SHAFILES
				echo "$(tput setaf 2)Patch appliqué avec succès"
			        echo "$(tput sgr0)";;
			  n|N ) echo "non"
				echo "$(tput setaf 3)Patch non appliqué"
			        echo "$(tput sgr0)";;
			  * ) echo "choix invalide";;
			esac
		else
			echo ""
		        echo "$(tput setaf 1)++------------------------------------------------++"
		        echo "++  Le fichier $SHAFILES n'a pas l'empreinte attendue            ++"
		        echo "++---------------------------------------------------------------++"
		fi

	fi	
done

exit;

