#!/bin/bash

#Name of the en-US 1.1 directory 
EN11='EN'
#Name of the fr-FR 1.0 directory 
FR10='FR10'
#Name of the en-US 1.0 directory 
EN10='EN10'
#Name of the new fr-FR 1.1 directory 
FR11='l10n/fr/'


#For each directory in the en-US 1.1 locale
for x in  `find $EN11 -type d` 
do

# For each item in the previous directory
	for i in `ls $x`
	do
#If it's a file : 
	if [ -f $x/$i ]
	then
#Find this this in the 1.0 en-US locale
		jj=`find $EN10 -type f -name $i`
		jjj=0

#For each occurence of this file 
		  for j in $jj
		  do

#We incremente a counter jjj
			: $[ jjj = $jjj + 1 ]

#We make a diff of the en-US1.0 and the en-US1.1
			diff -q --strip-trailing-cr -b -B $x/$i  $j >/dev/null 


#If the two files are the same

		        if [ $? -eq 0 ] 
			then


#Search the file on the fr-FR locale
		     	  kk=`find $FR10 -type f -name $i`
			  for k in $kk
			  do
			  : $[ jjj = $jjj + 1 ]
			  if [ "$jjj" -gt 2 ]
			  then
#If there is more than one file in the fr-FR locale with the same name, copy it with the counter attached to the name		
                            cp $k $FR11/$x/$i'_'$jjj
			  else
#If ther is only one file found, copy it on the new directory structure for fr-F			
                            cp $k $FR11/$x/$i
			  fi
			    
			  done
			else

#If the en-US 1.1 and en-US 1.0 are different copy the en-US1.1 in the right place in the fr-FR directory structure.
#Next to this file we copy the fr-FR1.0 file with the '_VF10' extension and the en-US file with the '_VO10' extension. If there is multiple files with the same name, an extension is also attached in the form of the number count.
		          cp $x/$i $FR11/$x/$i
		     	  kk=`find $FR10 -type f -name $i`
			  for k in $kk
			  do
			  : $[ jjj = $jjj + 1 ]
			  if [ "$jjj" -gt 2 ]
			  then
                              cp $k $FR11/$x/$i"_VF10_"$jjj
			  else
                              cp $k $FR11/$x/$i"_VF10"
			  fi
			  done
                          cp $j $FR11/$x/$i"_VO10"
			fi
		done
#Just a verification to give the name of the file witch are more than in one place on the en-USV1.0 locale.
		if [ "$jjj" -gt 2 ]
		then 
		echo $jj
		fi

#If there is no file with the same name in the en-USV1.0 locale, then the en-USV1.1 is copied with the extension '_nouveau' (new in french).
		if [ "$jjj" -eq 0 ]
		then 
		          cp $x/$i $FR11/$x/$i
		          cp $x/$i $FR11/$x/$i"_nouveau"
		fi
			  
	fi
	done
done

