#!/bin/bash
#nécessite les paquets: zenity;

OLDIFS=$IFS

{
projet=`zenity --title "Sélectionner le fichier csv nomenclature" --file-selection --filename="./Bureau"`

if  [ $? != 0 ]; then # si fermeture
	exit 1
fi
[ $? -ne 0 ] && exit 2 # si annulation
}

type_folio=$(zenity 	--list --text="Choisir nom folio:" --height=180 --width=300 --title="Format" --column= label_folio id_projet)


IFS=$'\n'

#*****************************************************
# CONFIGURATION
#*****************************************************
larcol_repere=60
larcol_desi=400
larcol_fournisseur=200
larcol_reference=300
larcol_folio=40
nb_ligne_folio=50
taille_entete=9
taille_liste=7
hautlig_entete=20
hautlig_liste=15
destination="${HOME}/.qet/elements/temp_nomenclature"
#****************************************************
#(1)N° de folio;(2)Titre de folio;(3)Label de folio;(4)Désignation qet;(5)Position;(6)Label;(7)Désignation;(8)Commentaire;(10)Fabricant;(11)Reference;Bloc auxiliaire 1;Bloc auxiliaire 2;Machine-reference;Localisation;Fonction

OLDIFS=$IFS
IFS=$'\n'

mapfile -t nomenclature_non_trie < "$projet"

nbr_nomenclature_non_trie=${#nomenclature_non_trie[*]}

j=4
i=0
#Seuls sont traités les éléments dont référence est non vide
while ((j<nbr_nomenclature_non_trie)); do
	reference=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f11`

	if [ -n "$reference" ] ;then
			
		if [ "$type_folio" = "label_folio" ];then
			folio=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f3`
		else
			folio=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f1`
		fi

		repere=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f6`
		designation=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f7`
		constructeur=`echo "${nomenclature_non_trie[$j]}" | cut -d";" -f10`

		nomenclature_trie[$i]="${repere}:${folio}:${designation}:${constructeur}:${reference}"
		((i+=1))
	fi
	((j+=1))
done

#trie alphabétique -d pas de différence entre majuscule et minuscule -f trie sur séparateur :
nomenclature=( $(printf "%s\n" ${nomenclature_trie[*]} | sort -fV) )

nbr_nomenclature=${#nomenclature[*]}

#echo ${nomenclature[*]} > "/home/alexis/Bureau/nomenclature.txt"


#dossier bibliothèque QET
if [ ! -d "$destination" ];then
	mkdir "$destination"
else
	rm -Rf "$destination"
	mkdir "$destination"
fi

fichier="${destination}/qet_directory"

echo "<qet-directory>" >> $fichier
echo "    <names>" >> $fichier
echo "        <name lang=\"en\">TEMP: nomenclature</name>" >> $fichier
echo "        <name lang=\"fr\">TEMP : nomenclature</name>" >> $fichier
echo "    </names>" >> $fichier
echo "</qet-directory>" >> $fichier

#créer les éléments
#SavedRow=0
CurElmtFile=0
	
nb_fichier=$(((nbr_nomenclature/nb_ligne_folio)+1))
	
i=0
while ((i<nb_fichier)); do

	fichier="${destination}/nomenclature-${CurElmtFile}.elmt"

	longueur_elmt=$((larcol_repere+larcol_desi+larcol_fournisseur+larcol_reference+larcol_folio+8))
	hauteur_elmt=$((hautlig_entete+(nb_ligne_folio*hautlig_liste)+8))
	uuid=($(uuidgen -r))


		
	echo "<definition hotspot_x=\"4\" hotspot_y=\"4\" width=\""${longueur_elmt}"\" height=\""${hauteur_elmt}"\" orientation=\"dyyy\" link_type=\"simple\" type=\"element\">" >> $fichier
	echo "    <uuid uuid=\""${uuid}"\"/>" >> $fichier
	echo "    <names>" >> $fichier
	echo "        <name lang=\"fr\">"Nomenclature-${CurElmtFile}"</name>" >> $fichier
	echo "    </names>" >> $fichier
	echo "    <description>" >> $fichier	


	#[nomenclature]=repère:folio:designation:constructeur:reference

	actu_x=0
	actu_y=0		


	largeur_table=$((larcol_repere+larcol_desi+larcol_fournisseur+larcol_reference+larcol_folio))


	j=0
	while ((j<=nb_ligne_folio)); do

		#******************************************
		#Entête tableau
		#******************************************
		if [ "$j" = 0 ] ;then
			PosX=0
			PosY=0
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${largeur_table}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			PosXTexte=$((PosX+8))
			PoxYTexte=$((PosY+(hautlig_entete/2)+taille_entete/2))
			echo "        <text text=\"Repère\" rotation=\"0\" size=\""${taille_entete}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier


			PosX=$((PosX+larcol_repere))
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			PosXTexte=$((PosX+8))
			echo "        <text text=\"Folio\" rotation=\"0\" size=\""${taille_entete}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier

			PosX=$((PosX+larcol_folio))
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			PosXTexte=$((PosX+8))
			echo "        <text text=\"Désignation\" rotation=\"0\" size=\""${taille_entete}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier

			PosX=$((PosX+larcol_desi))
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			PosXTexte=$((PosX+8))
			echo "        <text text=\"Fournisseur\" rotation=\"0\" size=\""${taille_entete}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier

			PosX=$((PosX+larcol_fournisseur))
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

			PosXTexte=$((PosX+8))
			echo "        <text text=\"Référence\" rotation=\"0\" size=\""${taille_entete}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier


			PosX=$((PosX+larcol_reference))
			echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${hautlig_entete}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier
	
			PosY=$((PosY+hautlig_entete))
			PosX=0
#			((j+=1))
		fi

		x=$((j+memo_ligne))

		#******************************************
		#liste matériel
		#******************************************
		uuid=($(uuidgen -r))
		repere=`echo "${nomenclature[$x]}" | cut -d":" -f1`
		designation=`echo "${nomenclature[$x]}" | cut -d":" -f3`
		folio=`echo "${nomenclature[$x]}" | cut -d":" -f2`
		fournisseur=`echo "${nomenclature[$x]}" | cut -d":" -f4`
		reference=`echo "${nomenclature[$x]}" | cut -d":" -f5`



		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${largeur_table}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier
		
		PosY2=$((PosY+hautlig_liste))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

		PosXTexte=$((PosX+8))
		PoxYTexte=$((PosY+(hautlig_entete/2)+taille_liste/2))
		PosYTexteDyn=$((PoxYTexte-12))
#		echo "        <text text=\""${repere}"\" rotation=\"0\" size=\""${taille_liste}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier
#texte dynamique
		echo "        <dynamic_text z=\"3\" text_from=\"UserText\" rotation=\"0\"  x=\""${PosXTexte}"\" y=\""${PosYTexteDyn}"\" font_size=\""${taille_liste}"\" uuid=\"{"${uuid}"}\">" >> $fichier
            	echo "        	<text>"${repere}"</text>" >> $fichier
            	echo "        	<color>#4a4a4a</color>" >> $fichier
            	echo "        </dynamic_text>" >> $fichier		

		PosX=$((PosX+larcol_repere))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

		PosXTexte=$((PosX+8))
#		echo "        <text text=\""${folio}"\" rotation=\"0\" size=\""${taille_liste}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier
#texte dynamique
		echo "        <dynamic_text z=\"3\" text_from=\"UserText\" rotation=\"0\"  x=\""${PosXTexte}"\" y=\""${PosYTexteDyn}"\" font_size=\""${taille_liste}"\" uuid=\"{"${uuid}"}\">" >> $fichier
            	echo "        	<text>"${folio}"</text>" >> $fichier
            	echo "        	<color>#4a4a4a</color>" >> $fichier
            	echo "        </dynamic_text>" >> $fichier

		PosX=$((PosX+larcol_folio))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

		PosXTexte=$((PosX+8))
#		echo "        <text text=\""${designation}"\" rotation=\"0\" size=\""${taille_liste}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier
#texte dynamique
		echo "        <dynamic_text z=\"3\" text_from=\"UserText\" rotation=\"0\"  x=\""${PosXTexte}"\" y=\""${PosYTexteDyn}"\" font_size=\""${taille_liste}"\" uuid=\"{"${uuid}"}\">" >> $fichier
            	echo "        	<text>"${designation}"</text>" >> $fichier
            	echo "        	<color>#4a4a4a</color>" >> $fichier
            	echo "        </dynamic_text>" >> $fichier

		PosX=$((PosX+larcol_desi))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

		PosXTexte=$((PosX+8))
#		echo "        <text text=\""${fournisseur}"\" rotation=\"0\" size=\""${taille_liste}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier
#texte dynamique
		echo "        <dynamic_text z=\"3\" text_from=\"UserText\" rotation=\"0\"  x=\""${PosXTexte}"\" y=\""${PosYTexteDyn}"\" font_size=\""${taille_liste}"\" uuid=\"{"${uuid}"}\">" >> $fichier
            	echo "        	<text>"${fournisseur}"</text>" >> $fichier
            	echo "        	<color>#4a4a4a</color>" >> $fichier
            	echo "        </dynamic_text>" >> $fichier
		
		PosX=$((PosX+larcol_fournisseur))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier

		PosXTexte=$((PosX+8))
#		echo "        <text text=\""${reference}"\" rotation=\"0\" size=\""${taille_liste}"\" x=\""${PosXTexte}"\" y=\""${PoxYTexte}"\"/>" >> $fichier
#texte dynamique
		echo "        <dynamic_text z=\"3\" text_from=\"UserText\" rotation=\"0\"  x=\""${PosXTexte}"\" y=\""${PosYTexteDyn}"\" font_size=\""${taille_liste}"\" uuid=\"{"${uuid}"}\">" >> $fichier
            	echo "        	<text>"${reference}"</text>" >> $fichier
            	echo "        	<color>#4a4a4a</color>" >> $fichier
            	echo "        </dynamic_text>" >> $fichier

		PosX=$((PosX+larcol_reference))
		echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY2}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${PosX}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier
	
		PosY=$((PosY+hautlig_liste))
		PosX=0

		((j+=1))

	done	
	echo "        <line length2=\"1.5\" antialias=\"false\" y1=\""${PosY}"\" y2=\""${PosY}"\" x1=\""${PosX}"\" end1=\"none\" x2=\""${largeur_table}"\" end2=\"none\" style=\"line-style:normal;line-weight:normal;filling:none;color:black\" length1=\"1.5\"/>" >> $fichier
	echo "    </description>" >> $fichier
	echo "</definition>" >> $fichier
	((CurElmtFile+=1))
	((i+=1))
	memo_ligne=$((memo_ligne+j-1))
done



#-----------------------------------------------------------------------
#FIN
#-----------------------------------------------------------------------



IFS=$OLDIFS

if [ $=0 ];then
	zenity --info --title="Fichiers éléments créés" --height=100 --width=500 --text="$CurElmtFile"" élément(s) créé(s). - Recharger la collection de QET."
else
	zenity --error --title="ERREUR" --height=100 --width=500 --text="ERREUR"
fi
exit

