#!/bin/bash
it_dir=/home/xavier/projets/qet/wd/0.22/elements/capteurs/sondes
find $it_dir -name "qet_directory" | while read it_elmt; do
	# extract italian translation
	export it_translation=$(perl -nle 'print $1 if /name lang="it" ?>(.+)</' $it_elmt)
	#echo $it_elmt $it_translation
	
	# find matching element
	rel_path=$(echo $it_elmt | sed "s,$it_dir/,./,")
	candidates_elmt=$(find . -path $rel_path)
	candidates_elmt_count=$(echo $candidates_elmt | grep . | wc -w)
	if [ $candidates_elmt_count -eq 1 ]; then
		perl -i -plE 'say sprintf(q[        <name lang="it">%s</name>], $ENV{q[it_translation]}) if /<\/names>/' $candidates_elmt
	else
#		candidates_elmt=$(find . -name "$(basename $rel_path)")
#		candidates_elmt_count=$(echo $candidates_elmt | grep . | wc -w)
#		if [ $candidates_elmt_count -eq 1 ]; then
#			perl -i -plE 'say sprintf(q[        <name lang="it">%s</name>], $ENV{q[it_translation]}) if /<\/names>/' $candidates_elmt
#		else
			echo "$candidates_elmt_count candidate element(s) found for $rel_path"
#		fi
	fi

done
