#!/bin/bash

TITULO="Utilidad para Unir PDFs"
IMAGEN="pdf"
ICONO="vitalinux"

function mensaje-error {
	MENSAJE="$1"
	yad --title "${TITULO}" \
		--window-icon "${ICONO}" \
		--image "${IMAGEN}" \
		--text " $1 " --center --width="600" --button="Salir":1
	exit
}

# Obtenemos el número de parámetros recibidos: %F son el número de rutas absolutas de los archivos seleccionados
NUMPARAM=$#
if test $NUMPARAM -lt 2 ; then
	mensaje-error "Error!! Deberías seleccionar al menos 2 o más archivos PDFs ..."
fi

DIRECTORIOBASE="$(dirname "${1}")"
FECHA="$(date +%F-%H-%M-%S)"
DESTINO="$DIRECTORIOBASE/resultado-union-pdfs.${FECHA}.pdf"

: '
TEXTO="    \n<b>Nota:</b> Indica el <span foreground='blue'><b>Nombre del Archivo y el Directorio</b></span> donde quieres dejar el Resultado de Unir los PDFs seleccionados...\n"
while test "$DESTINO" == "" -o -d "$DESTINO" -o -f "$DESTINO" ; do
	DESTINO=$(yad --title "Selecciona el Nombre del Archivo y el Directorio" \
	--window-icon vitalinux \
	--file --save \
	--width 800 --height 300 --fullscreen \
	--center \
	--text "$TEXTO" --text-align="center")
	TEXTO="    <b>Importante:</b> Debes indicar <span foreground='red'><b>un archivo que no exita ya en el sistema</b></span> ...\n    <b>¡¡No puedes dejar el nombre en blanco!!</b>"
done

if ! (echo "$DESTINO" | grep "[.][pP][dD][fF]" &> /dev/null) ; then
	NOMBREDESTINO="$DESTINO".pdf
else
	NOMBREDESTINO="$DESTINO"
fi
'

# Creamos una lista con los parámetros recibidos $@.  El primer parametro es el ${PARAM[0]} ...  ${PARAM[*]} sería todos
# La mitad de los parametros recibidos son las rutas absolutas de los archivos, y la otra mitad son sus extensiones
declare -a PARAM
PARAM=($@)

RESULTADO="#A continuación van a unirse los PDFs seleccionados:"
#yad --text "Se han seleccionado $# archivos y sus rutas son $*"
(

PRIMERARCHIVO="$1"
cp "$PRIMERARCHIVO" "/tmp/auxiliar-unir-pdfs.pdf"
shift
RESULTADO="$RESULTADO\n$PRIMERARCHIVO"

until [ -z "$1" ] ; do

	RESULTADO="$RESULTADO\n$1"
	echo "$RESULTADO"

	if /usr/bin/pdftk "/tmp/auxiliar-unir-pdfs.pdf" "$1" output "$DESTINO" ; then
		echo "$RESULTADO\nDe momento ... ¡¡Todo Ok!!"
	fi
	cp "$DESTINO" "/tmp/auxiliar-unir-pdfs.pdf"
	shift

done

RESULTADO="$RESULTADO\n\nEl resultado de la unión lo tienes en: $DESTINO"
echo "$RESULTADO"

) | yad --title "${TITULO}" \
	--window-icon "${ICONO}" \
	--width 600 \
	--center \
	--progress --pulsate

exit 0
