#---*- Makefile -*-------------------------------------------------------
#$Author: saulius $
#$Date: 2019-03-07 15:48:47 +0000 (Thu, 07 Mar 2019) $
#$Revision: 190 $
#$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/makefiles/scanned-text-TIFs-to-landscape-PDF/Makelocal-scanned-text-TIFs-to-landscape-PDF $
#------------------------------------------------------------------------

SCAN_DIR    = scans
PWD        := $(shell pwd)
DIR_NAME   := $(notdir ${PWD})
PDF_NAME   := ${DIR_NAME}

TIF_INPUTS := ${wildcard ${SCAN_DIR}/*.tif}
PDF_FILES   = ${TIF_INPUTS:${SCAN_DIR}/%.tif=%.pdf}

FINAL_PDF_FILE = ${PDF_NAME}.pdf

.PHONY: all clean cleanAll distclean

.INTERMEDIATE: ${PDF_FILES}

all: ${FINAL_PDF_FILE}

#------------------------------------------------------------------------------

${PDF_NAME}.pdf: ${PDF_FILES}
	pdfcat $^ > $@

# The "/PageSize" PDF directive to set the landscape PDF format was
# taken from:

# http://www.troubleshooters.com/linux/gs.htm#_Making_a_Landscape_PDF

# The A4 page sizes were taken from the Ghostscript manual, table
# "Paper sizes known to Ghostscript/ISO standard":

# http://www.ghostscript.com/doc/7.07/Use.htm

%.pdf: ${SCAN_DIR}/%.tif
	tifftopnm $< \
	| pnmflip -r90 \
	| pnmcut -top 150 -bottom -300 -left 430 -right -1300 \
	| pnmnorm -wpercent 90 \
	| pnmtops -noturn -width 11.7 -height 8.3 \
	| gs -q -dBATCH -dNOPAUSE \
		-sOutputFile=$@ \
		-sDEVICE=pdfwrite \
		-c "<< /PageSize [842 595]  >> setpagedevice" \
		-

## 	| pnmpad -left 20 -right 20 -top 20 -bottom 20 \

clean cleanAll distclean:
	rm -f ${PDF_FILES}
	rm -f ${FINAL_PDF_FILE}
