#!/bin/bash
cierralanzador

TMP="/tmp/enmarcado"
rm -r "$TMP"
mkdir -p "$TMP"

nombre="marco"
ruta=`dirname "$0"`
echo "$ruta"
cd "$ruta"
cp plantilla1.svg plantilla2.svg plantilla3.svg $TMP
cd $TMP
cp "$1" $TMP/UNO
cp "$2" $TMP/DOS
cp "$3" $TMP/TRES

#¿QUÉ NOS ARRASTRAN IMÁGEN LOCAL O WEB?

echo "$@" >$TMP/arrastrado
rpl %2520 %20 $TMP/arrastrado
if [ "`cat $TMP/arrastrado|grep http`" = "" ];then
	echo "no viene de la web"
else	
	
	echo "sí viene de la web"
	echo "¿Es una imagen directa o un enlace de google images?"
	if [ "`cat $TMP/arrastrado|grep www.google`" = "" ];then
		
		echo "No es de googleimages"		
		wget `cat $TMP/arrastrado` -O $TMP/UNO
		
	else	
	
		echo "Sí es de googleimages"
		awk -F 'imgurl=' '{print $2}' $TMP/arrastrado |cut -d'&' -f1>$TMP/url		
		wget `cat $TMP/url` -O $TMP/UNO
	fi
fi



#¿SON LAS IMÁGENES DEMASIADO GRANDES?

for file in "$TMP/UNO" "$TMP/DOS" "$TMP/TRES";
do
ancho=`identify "$file"|cut -d " " -f3|cut -d"x" -f1`
alto=`identify "$file"|cut -d " " -f3|cut -d"x" -f2`
if (( $ancho > 500 ));then
	#reduce la imagen a 500

	convert "$file" -alpha remove -resize 500 "$file"
fi
if (( $alto > 500 ));then
	#reduce la imagen a 500
	convert "$file" -alpha remove -resize 500 "$file"
fi
done


# ESCALAMOS PARA QUE SE MANTENGAN LAS PROPORCIONES CUADRADAS

for file in "$TMP/UNO" "$TMP/DOS" "$TMP/TRES";
do
	#Quitamos el borde blanco o transparente que pueda tener la imagen
	convert "$file" -background white -trim +repage "$file"
	ancho=`identify "$file"|cut -d " " -f3|cut -d"x" -f1`
	alto=`identify "$file"|cut -d " " -f3|cut -d"x" -f2`

if (( $ancho > $alto ));then 
	#la imagen es horizontal

	mogrify -gravity center -background white -extent $anchox$ancho "$file"

fi
if (( $alto > $ancho ));then
	#la iamgen es vertical
	mogrify -gravity center -background white -extent $altox$alto "$file"
fi
	

done
# PROCEDEMOS AL MONTAJE CON LAS IMÁGENES QUE TENGAMOS


if [ -f "TRES" ];then
convert plantilla3.svg $nombre.png| zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
#inkscape plantilla3.svg --export-png /$nombre.png | zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
inkscape -f plantilla3.svg -A $nombre.pdf | zenity --progress --pulsate --auto-close
destino=`zenity --file-selection --save --confirm-overwrite --filename="$nombre" --title "¿Dónde guardamos el archivo PDF y la imagen PNG?"`
echo "$destino" >/tmp/destino
cp "$nombre.pdf" "$destino.pdf"
cp "$nombre.png" "$destino.png"
atril "$destino.pdf"
exit 0
fi

if [ -f "DOS" ];then
convert plantilla2.svg $nombre.png| zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
#inkscape plantilla2.svg --export-png /$nombre.png | zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
inkscape -f plantilla2.svg -A $nombre.pdf | zenity --progress --pulsate --auto-close
destino=`zenity --file-selection --save --confirm-overwrite --filename="$nombre" --title "¿Dónde guardamos el archivo PDF y la imagen PNG?"`
echo "$destino" >/tmp/destino
cp "$nombre.pdf" "$destino.pdf"
cp "$nombre.png" "$destino.png"
atril "$destino.pdf"
exit 0
fi

if [ -f "UNO" ];then
convert plantilla1.svg $nombre.png| zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
#inkscape plantilla1.svg --export-png /$nombre.png | zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
inkscape -f plantilla1.svg -A $nombre.pdf | zenity --progress --pulsate --auto-close
destino=`zenity --file-selection --save --confirm-overwrite --filename="$nombre" --title "¿Dónde guardamos el archivo PDF y la imagen PNG?"`
echo "$destino" >/tmp/destino
cp "$nombre.pdf" "$destino.pdf"
cp "$nombre.png" "$destino.png"
atril "$destino.pdf"
exit 0
fi


#inkscape $TMP/fotos$grupo/plantilla.svg --export-png $TMP/fotosresultados/pag$grupo.png | zenity --progress --pulsate --auto-close --title="Creando página..." --text="Página $grupo"
#convert ~/marco-simple.png ~/marco-simple.pdf | zenity --progress --pulsate --auto-close
