PSTOPDF=ruby /usr/share/texmf/scripts/context/ruby/pstopdf.rb

all: intercalated.pdf explicacion.html sources.pdf 3.675mm.mask.pdf inline-images

frames=1-80x160.pgm 2-80x160.pgm 3-80x160.pgm 4-80x160.pgm 5-80x160.pgm 6-80x160.pgm 7-80x160.pgm 8-80x160.pgm 9-80x160.pgm 
colorframes=1.ppm 2.ppm 3.ppm 4.ppm 5.ppm 6.ppm 7.ppm 8.ppm 9.ppm
sources=Makefile intercalate.py pscut.py pnm.py intercalate.py simplify.py explicacion 3.675mm.mask.ps
sourcesps=$(foreach source,$(sources),$(source).source.ps)

%.pdf: %.ps
	$(PSTOPDF) $<

intercalated.pgm: $(frames)
	./intercalate.py $(frames) > $@

intercalated.ppm: $(colorframes)
	./intercalate.py $(colorframes) > $@

%-80x160.jpg: %.jpg
	convert -geometry 80x160 -colorspace Gray $< $@

%.pgm: %.jpg
	convert $< $@

%.ppm: %.jpg
	convert $< $@

# 300 mm × 450 mm, minus 3 mm on each side except the top
# (* (/ 72 25.4) 3) = 8.503937007874017
# (* (/ 72 25.4) 294) = 833.3858267716537
# (* (/ 72 25.4) 447) = 1267.0866141732286
%.ps: %.pgm pscut.py
	./pscut.py 833.4x1267.0+8.5+8.5 .25 .75 < $< > $@

explicacion.html: explicacion
	python -m markdown $< > $@

# paps only supports one input file
%.source.horked.ps: %
	paps --header $< > $@

# enscript doesn’t support Unicode
#enscript -C -p- -2 -G -r $(sources) > $@

# paps output files are hairy enough that psjoin can’t handle them
# properly, so we convert them to PDF and back.
%.source.ps: %.source.horked.pdf
	pdftops $< $@

sources.1up.ps: $(sourcesps)
	psjoin $(sourcesps) > $@

# pstopdf fucks up on this filename:
3.675mm.mask.ps.source.horked.pdf:
	paps --header 3.675mm.mask.ps > 3.675mm.mask.ps.source.horked.ps
	$(PSTOPDF) 3.675mm.mask.ps.source.horked.ps
	mv 3.675mm.mask.source.horked.pdf.pdf $@

# Override default rule invoking pstopdf because it doesn’t work for
# some reason
sources.1up.pdf: sources.1up.ps
	ps2pdf $< $@

sources.ps: sources.1up.pdf
	pdftops $< - | psnup -4 > $@

inline-images: 15068951_10206069995063443_6893621633418693549_o-inline.jpg \
	15101838_10206069917981516_541141303_o-inline.jpg \
	15111061_10206069934381926_403275401038866474_o-inline.jpg \
	15128967_10206069995543455_1033863974201214518_o-inline.jpg \
	15129595_10206070109186296_3215242109209239541_o-inline.jpg \
	15138532_10206070114786436_3186154385340719651_o-inline.jpg \
	15152320_10206069915981466_921242786_o-inline.jpg \
	15156880_10206070114626432_6226895603494168452_o-inline.jpg \
	15167441_10206069935101944_3884984228770350693_o-inline.jpg

%-inline.jpg: %.jpg
	convert -geometry 768x768 $< $@
