summaryrefslogtreecommitdiff
path: root/Makefile
blob: c9580ee299b822a9ed6092c38bd2b936077ddf00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
define pages
$(if $(TITLE),title.txt break)
way-to-be-great.txt space lost-pen.txt space authenticated-counterfeit.txt break
argument.txt space why.txt space con-job.txt break
introduction.txt space introjoke.txt break
human-communication.txt break
$(if $(TITLE),break break break)
endef
pages := $(pages:=)

define pages
$(if $(TITLE),title.txt break)
way-to-be-great.txt space lost-pen.txt
break authenticated-counterfeit.txt space argument.txt break
introduction.txt space break
$(if $(TITLE),break break break)
endef
pages := $(pages:=)

output_name = poetrybook
pdf_viewer = evince

.DEFAULT_GOAL = view
output_txt = $(output_name).txt
output_pdf = $(output_name).pdf

SIDES = two-sided-short-edge

LP = lp
LP += $(if $(SIDES), -o sides=$(SIDES))

LPDF = lpdf
NUMBER_UP = 2
LEFT_MARGIN = 100
TOP_MARGIN = 20
MARGIN_ARGS = left=$(LEFT_MARGIN) top=$(TOP_MARGIN)
SCALING = none

LPDF += $(if $(PAGES), -P $(PAGES))
LPDF += $(addprefix -o page-, $(MARGIN_ARGS))
LPDF += $(if $(SCALING),-o print-scaling=$(SCALING))
LPDF += $(if $(NUMBER_UP),-o number-up=$(NUMBER_UP))
LPDF += $(if $(SIDES),-o sides=$(SIDES))

define concatenate_with_terminator
@($(foreach f,$(+), cat < $(f) && printf $(1);)) > $@.tmp
	@$(done)
endef

newprint: NUMBER_UP=1
newprint: SIDES=


dirs = build
$(dirs):
	@mkdir -p $@

done = mv -T -- $@.tmp $@

.INTERMEDIATE: build/$(output_txt)

build/break: | build
	printf \\f > $@

build/space: | build
	printf \\n\\n\\n > $@

build/$(output_txt): $(addprefix build/,$(pages)) | build
	cat $+ > $@.tmp
	$(done)

%.pdf: %.txt
	$(LPDF) $< > $@.tmp
	$(done)

%.ps: %.pdf
	pdf2ps -dLanguageLevel=3 $< $@

build/%: % | build
	cp --reflink -T -- $< $@

clean:
	-rm -rf build

.PHONY: view print viewall printall realview realprint booklet pages

booklet: clean build/booklet.pdf
	-$(pdf_viewer) $(word 2, $+) &
pages: clean build/pages.ps
	-$(pdf_viewer) $(word 2, $+) &

newprint: clean newrealprint
newrealprint: NUMBER_UP=1
newrealprint: SIDES=
newrealprint: build/pages.pdf
	-$(pdf_viewer) $< &

%.pdf: %.ps
	ps2pdf $< $@

build/pages.ps: NUMBER_UP=1
build/pages.ps: SIDES=
build/pages.ps: build/$(output_pdf:pdf=ps)
	@cp -T -- $^ $@

build/booklet.ps: build/pages.ps
	psbook $< $@.tmp
	psnup -2 $@.tmp $@
	@rm $@.tmp

view: clean realview
print: clean realprint

realview: build/pages.pdf
	-$(pdf_viewer) $< &
realprint: build/booklet.ps
	$(LP) $^

.PHONY: install
install:
	apt install --no-upgrade printer-driver-cups-pdf evince ghostscript