#! /bin/bash

if [ ! "$(ls '/usr/bin/tiffcp')" == "/usr/bin/tiffcp" ]; then 
	echo  program na konverzi pdf to tiff neni nainstalovany
	echo  sudo apt-get install libtiff-tools
	sudo  apt-get install libtiff-tools
fi

clear
echo "Konvertuje vsechny soubory TIFF do jednoho souboru PDF"
echo "------------------------------------------------------"
echo

if [ -z $1 ]; then
	koncovka=tiff
	echo "	jktiff2pdf.sh [extension | jeden_soubor]"
echo "		if extension argument is ommited, extension \"tiff\" is assumed."
echo " 		if no argument or only extension is given, "
echo "		then all the directory of *.tiff is converted."; echo		
read -p 	"Press CTRL-C to break"

else
	if [ -e $1 ]; then
		soubor="$(basename -s.tiff "$1" ).pdf"
		if [ ! -e "$soubor" ]; then
			tiff2pdf -F  -f $1 -o "$soubor"
		else
			echo "$soubor already exists. Canceling and exiting.."; echo
			exit
		fi
		echo "The file $soubor is ready.";echo
		
		exit
	fi	
	koncovka=$1
fi
vystup=vystup_$(date +'%y-%m-%d-%H-%M').pdf
docasny=docasny_$(date +'%y-%m-%d-%H-%M').tiff

# tiffcp -c g4 -w 1700 *.$koncovka $docasny
tiffcp -x *.$koncovka $docasny
# -x     Force the output file to be written with PAGENUMBER value in sequence.
tiff2pdf -z -f -F -o $vystup $docasny #  -u m -w 20 
# -F     Cause the tiff to fill the PDF page.
# -f     Set PDF ``Fit Window'' user preference.
# -z     Compress with Zip

rm $docasny
echo $vystup is ready.
echo
exit