#!/bin/bash
### Este script renombra archivos y carpetas a distintos idiomas.
###
### Cada carpeta debe tener un archivo llamado .translations con un contenido
### que siga el siguiente esquema:
###
### [en_EN];[es_ES];[gl_ES];[ca_CA]
### table;mesa;táboa;taula
### 

ruta="$1"

idioma=`echo $LANG|cut -d"." -f1`
echo "Traducir a $idioma"


function renombra(){
#Buscamos en qué carpetas están las tradus
#find $ruta -name ".translations*" >/tmp/tradus
find $ruta \( ! -regex '.*/\..*/..*' \) -type f -name ".translations*" >/tmp/tradus

#Revertimos las rutas donde hay ficheros de tradus para hacer la recursividad
tac /tmp/tradus |awk -F ".translations" '{print $1}'>/tmp/rutatradus

while read line
do
	echo "entra en" "$line"
	cd "${line}"
		    #FIXME Ahora que lo vuelvo a remirar después de un tiempo, veo más lógico recorrer únicamente el
		    #      .translations y buscar la carpeta, que comprobar todas las carpetas y mirar luego en el
		    #      .translations si hay que traducirla.
	for i in *;
	do
		case $idioma in
			en_*)
				newname=`cat .translations|grep "$i;"|cut -d";" -f1`
				if [ "$newname" != "" ];then
					if [ "$newname" != "$i" ];then
					
						echo "Renombramos a $idioma" "$i" "con" "$newname"
						if [ -e "$newname" ];then
						    echo "Ya existe la carpeta destino, así que la copiamos."
						    rsync -vah "$i"/ "$newname"/
							#FIXME si la carpeta está vacía moverla/borrarla
						    if [ $? -eq 0 ];then
							echo "borrando/moviendo"
							mv "$i" /tmp/"${i}movido"
							rm -r /tmp/"${i}movido"
						    fi
						
						else    
						    echo "$newname no existe"
						    mv "$i" "$newname"
						fi
					fi
				fi
				
			;;
			es_ES)
				newname=`cat .translations|grep "$i;"|cut -d";" -f2`
				if [ "$newname" != "" ];then
					if [ "$newname" != "$i" ];then
					
						echo "Renombramos a $idioma" "$i" "con" "$newname"
						if [ -e "$newname" ];then
						    echo "Ya existe, así que hay que copiar su contenido."
						    rsync -vah "$i"/ "$newname"/
						    if [ $? -eq 0 ];then
							#rm -r "$i"
							mv "$i" /tmp/"${i}movido"
							
						    fi
						
						else    
						    mv "$i" "$newname"
						fi
					fi
				fi
				
;;
			gl_ES)
				newname=`cat .translations|grep "$i;"|cut -d";" -f3`
				if [ "$newname" != "" ];then
					if [ "$newname" != "$i" ];then
					
						echo "Renombramos a $idioma" "$i" "con" "$newname"
						if [ -e "$newname" ];then
						    echo "$newname ya existe, así que hay que copiar su contenido."
						    rsync -vah "$i"/ "$newname"/
						    if [ $? -eq 0 ];then
							mv "$i" /tmp/"${i}movido"
						    fi
						
						else    
						    mv "$i" "$newname"
						fi
					fi
				fi
				

			;;
			ca_ES)
				newname=`cat .translations|grep "$i;"|cut -d";" -f4`
				if [ "$newname" != "" ];then
					if [ "$newname" != "$i" ];then
					
						echo "Renombramos a $idioma" "$i" "con" "$newname"
						if [ -e "$newname" ];then
						    echo "$newname ya existe, así que hay que copiar su contenido."
						    rsync -vah "$i"/ "$newname"/
						    if [ $? -eq 0 ];then
							mv "$i" /tmp/"${i}movido"
						    fi
						
						else    
						    mv "$i" "$newname"
						fi
					fi
				fi
				

			;;
		esac
	
	done
		

done </tmp/rutatradus
}


#Repetir hasta cuándo ?? --> invento cutre, pero que parece que funciona

renombra > /tmp/terminado
cat /tmp/terminado|grep "Renombramos"
terminado=`cat /tmp/terminado|grep "Renombramos"`

while [ "$terminado" != "" ]
do
	renombra >/tmp/terminado
	cat /tmp/terminado|grep "Renombramos"
	terminado=`cat /tmp/terminado|grep "Renombramos"`

done

# CASO PARTICULAR DE DESKTOP QUE LO RECREA ALGÚN PROGRAMA A POSTERIORI

case $idioma in
	en*)
	     find ~/Escritorio -maxdepth 0 -empty -exec rm -r Escritorio \; &
	     find ~/Escriptori -maxdepth 0 -empty -exec rm -r Escriptori \; &
;;
	es*)
	     find ~/Desktop -maxdepth 0 -empty -exec rm -r Desktop \;&
	     find ~/Escriptori -maxdepth 0 -empty -exec rm -r Escriptori \;&
;;
	gl*)
	     find ~/Desktop -maxdepth 0 -empty -exec rm -r Desktop \;&
	     find ~/Escriptori -maxdepth 0 -empty -exec rm -r Escriptori \;&
;;
	ca*)
	     find ~/Escritorio -maxdepth 0 -empty -exec rm -r Escritorio \;&
	     find ~/Desktop -maxdepth 0 -empty -exec rm -r Desktop \;&
;;
esac

