Make it possible to generate 29tsoc ebooks
[project-aon.git] / common / pdf / build / lw / Makefile.common
1
2 # Root directory
3 AONROOTREL := ../../../../..
4 AONROOTDIR = $(realpath $(AONROOTREL))
5 INSTALLDIR := ../FINAL/
6 # Relative location of the XML files
7 RELXMLDIR := $(AONROOTDIR)/$(BLANG)/xml/
8 XMLFILE=$(patsubst %.tex,$(RELXMLDIR)/%.xml,$(SOURCE))
9 # Relative location of the scripts
10 RELSCRIPT := $(AONROOTDIR)/common/scripts
11 # Convert Script
12 XML2LATEX = $(RELSCRIPT)/gbtolatex.pl
13 # LaTeX stylesheet
14 LATEXXSL = $(AONROOTDIR)/common/xsl/latex.xsl
15
16 # Define ORIGSERIES unless defined
17 ORIGSERIES ?= $(SERIES)
18
19 # Define ILLUSTRATOR if not defined with a sane default
20 ILLUSTRATOR ?= williams
21
22 # Control debug
23 DEBUG ?= 0
24 AT_0 := @
25 AT_1 := 
26 AT = $(AT_$(DEBUG))
27
28 all: $(BASENAME).pdf 
29
30 SOURCE=$(BASENAME).tex
31 ifeq "$(BLANG)" "en"
32 EXTRAFILES=souvenir.sty
33 else
34 EXTRAFILES=ac1.tex crtneg.tex ac2.tex crtpos.tex random.tex ac3.tex \
35         souvenir.sty souverbt.sty
36 endif
37
38
39 # Generate the LaTeX file from the XML file
40 $(SOURCE): $(XMLFILE) $(XML2LATEX) $(LATEXXSL) tweak.diff
41         AONPATH="$(AONROOTDIR)" perl $(XML2LATEX) $(BASENAME) $(BLANG) $@
42         $(AT)if ! test -s $(SOURCE) ; then \
43                 echo "ERROR: Empty file $(SOURCE), check for errors above."  >&2 ; \
44                 rm -f $(SOURCE) ; \
45                 exit 1 ; \
46         fi
47         $(AT)cp $(SOURCE) $(BASENAME)-untweaked.tex
48         $(AT)if test -f tweak.diff; then \
49                 patch $(SOURCE) <tweak.diff ; \
50         fi
51
52 # Extra TeX files needed for the PDFs
53 ifeq "$(BLANG)" "en"
54 # This is for english content:
55 ac1.tex:
56         ln -s ../../shared/$@
57 ac2.tex:
58         ln -s ../../shared/$@
59 ac3.tex:
60         ln -s ../../shared/$@
61 crtneg.tex:
62         ln -s ../../shared/$@
63 crtpos.tex:
64         ln -s ../../shared/$@
65 else
66 # This is for other translated content:
67 ac1.tex:
68         ln -s ../../shared/$(BLANG)/$@
69 ac2.tex:
70         ln -s ../../shared/$(BLANG)/$@
71 ac3.tex:
72         ln -s ../../shared/$(BLANG)/$@
73 crtneg.tex:
74         ln -s ../../shared/$(BLANG)/$@
75 crtpos.tex:
76         ln -s ../../shared/$(BLANG)/$@
77 endif
78 # Shared across all languages
79 random.tex:
80         ln -s ../../shared/$@
81 souvenir.sty:
82         ln -s ../../shared/$@
83 souverbt.sty:
84         ln -s ../../shared/$@
85 # Create empty tweak.diff if it does not exist
86 tweak.diff:
87         $(AT)touch tweak.diff
88
89 # Symlink the images needed for the PDF; unused images and HTML-specific images are ignored
90 link-images:
91         $(AT)[ -n "$(SERIES)" ] || { echo "Error: SERIES variable not defined. Hint: Review the Makefile"; exit 1; }
92         $(AT)[ -n "$(SUBSERIES)" ] || { echo "Error: SUBSERIES variable not defined. Hint: Review the Makefile"; exit 1; }
93         $(AT)[ -n "$(BASENAME)" ] || { echo "Error: BASENAME variable not defined. Hint: Review the Makefile"; exit 1; }
94         $(AT)echo "Linking images for the book to working directory"
95         $(AT)[ -n "$(ILLUSTRATOR)" ] || { echo "Warning: ILLUSTRATOR variable not defined. Some illustrations might be missing. Hint: Review the Makefile"; }
96         $(AT)[ ! -n "$(ILLUSTRATOR)" ] || [ -d "$(AONROOTREL)/common/pdf/$(ORIGSERIES)/common/ill/$(ILLUSTRATOR)" ] || \
97         { echo "Warning: Folder $(AONROOTREL)/common/pdf/$(ORIGSERIES)/common/ill/$(ILLUSTRATOR) not found. Some illustrations might be missing."; }
98         $(AT)[ ! -n "$(ILLUSTRATOR)" ] || [ ! -d "$(AONROOTREL)/common/pdf/$(ORIGSERIES)/common/ill/$(ILLUSTRATOR)" ] || \
99         find $(AONROOTREL)/common/pdf/$(ORIGSERIES)/common/ill/$(ILLUSTRATOR)/ -name "*.pdf" | \
100         while read file ; do \
101                 [ -e "`basename $$file`" ] || ln -s $$file . ; \
102         done
103 ifeq "$(BLANG)" "en"
104         $(AT)for images in jpeg png ; do \
105                 [ ! -d "$(AONROOTREL)/en/$$images/$(SERIES)/$(BASENAME)/" ] || find $(AONROOTREL)/en/$$images/$(SERIES)/$(BASENAME)/ \( -name "*.jpg" -o -name "*.png" \) | \
106                 while read file ; do \
107                         [ -e "`basename $$file`" ] || \
108                         case "`basename $$file`" in \
109                                 back*|bckgrnd*|brdr*|forward*|left*|right*|title*|toc*) ;; \
110                                 ac*|crt*|random*) ;; \
111                                 *) ln -s $$file . ;; \
112                         esac ; \
113                 done ; \
114         done
115         $(AT)[ ! -d "$(AONROOTREL)/en/pdf/$(SERIES)/$(SUBSERIES)" ] || find $(AONROOTREL)/en/pdf/$(SERIES)/$(SUBSERIES)/ -name "*.pdf" | \
116         while read file ; do \
117                 [ -e "`basename $$file`" ] || ln -s $$file . ; \
118         done
119 else
120         $(AT)[ -n "$(ORIGSERIES)" ] || { echo "Error: ORIGSERIES variable not defined. Hint: Review the Makefile"; exit 1; }
121         $(AT)[ -n "$(ORIGBASENAME)" ] || { echo "Error: ORIGBASENAME variable not defined. Hint: Review the Makefile"; exit 1; }
122         $(AT)for images in jpeg png ; do \
123                 [ ! -d "$(AONROOTREL)/$(BLANG)/$$images/$(SERIES)/$(BASENAME)/" ] || find $(AONROOTREL)/$(BLANG)/$$images/$(SERIES)/$(BASENAME)/ \( -name "*.jpg" -o -name "*.png" \) | \
124                 while read file ; do \
125                         [ -e "`basename $$file`" ] || ln -s $$file . ; \
126                 done ; \
127                 [ ! -d "$(AONROOTREL)/en/$$images/$(ORIGSERIES)/$(ORIGBASENAME)/" ] || find $(AONROOTREL)/en/$$images/$(ORIGSERIES)/$(ORIGBASENAME)/ \( -name "*.jpg" -o -name "*.png" \) | \
128                 while read file ; do \
129                         rootname=`basename $$file | sed -e 's/\..*//'`; \
130                         [ -e "$$rootname.jpg" ] || [ -e "$$rootname.png" ] || ln -s $$file . ; \
131                 done ; \
132         done
133         $(AT)[ ! -d "$(AONROOTREL)/$(BLANG)/pdf/$(SERIES)/$(SUBSERIES)" ] || find $(AONROOTREL)/$(BLANG)/pdf/$(SERIES)/$(SUBSERIES)/ -name "*.pdf" | \
134         while read file ; do \
135                 [ -e "`basename $$file`" ] || ln -s $$file . ; \
136         done
137         $(AT)[ ! -d "$(AONROOTREL)/en/pdf/$(ORIGSERIES)/$(SUBSERIES)" ] || find $(AONROOTREL)/en/pdf/$(ORIGSERIES)/$(SUBSERIES)/ -name "*.pdf" | \
138         while read file ; do \
139                 [ -e "`basename $$file`" ] || ln -s $$file . ; \
140         done
141 endif
142         touch $@
143
144 # Convert them using the appropiate script
145 convert-images: link-images
146         $(AT)echo "Converting images to PDF format"
147         $(AT)sh ../../convert-images.sh
148         touch $@
149         
150 # Final install dir
151 install: $(INSTALLDIR) all
152         $(AT)echo "Copying $(BASENAME).pdf to $(INSTALLDIR)"
153         $(AT)cp $(BASENAME).pdf $(INSTALLDIR)
154
155 $(INSTALLDIR):
156         mkdir $@
157
158
159 dvi: $(BASENAME).dvi 
160 %.dvi: %.tex $(EXTRAFILES)
161         latex $<
162         latex -interaction batch $<
163         # Regenerate Contents
164         latex $<
165
166 %.ps: %.dvi
167         dvips -Ppdf $<
168
169 #%.pdf: %.ps
170 #        ps2pdf $< $@
171
172 pdf: $(BASENAME).pdf 
173 %.pdf: %.tex $(EXTRAFILES) convert-images
174 # Convert to UTF-8 if required
175         $(AT)if file $< | grep -q ISO-8859 ; then \
176                 echo "WARNING: $< is encoded in ISO-8859-1, converting to UTF-8" ; \
177                 mv $< $<.lat1;  \
178                 iconv -f latin1 -t utf-8 < $<.lat1  >$<; \
179         fi
180         -xelatex -interaction batchmode $<
181 #       # Remove everything except for chapter entries from the toc file
182         $(AT)[ -e "$(BASENAME).toc" ] || { echo "ERROR: Generated file does not include table of contents file, please review $(BASENAME).log" ; false ; }
183         -$(AT)grep '{chapter}' $(BASENAME).toc >temp.toc && mv -f temp.toc $(BASENAME).toc
184 #       # Run xelatex a second time to get correct page numbers in the toc
185         -xelatex -interaction batchmode $<
186 #       # Remove everything except for chapter entries from the toc file
187         -$(AT)grep '{chapter}' $(BASENAME).toc >temp.toc && mv -f temp.toc $(BASENAME).toc 
188 #       # Tweak the PDF bookmarks file
189         $(AT)[ -e "$(BASENAME).out" ] || { echo "ERROR: Required file $(BASENAME).out does not exist, please review $(BASENAME).log" ; false ; }
190         $(AT)sed 's/\\ellipsis@one =,//' $(BASENAME).out >temp.out && \
191                 echo '\let\WriteBookmarks\relax' >>temp.out && \
192                 mv -f temp.out $(BASENAME).out
193 #       # Run xelatex a third time to get correct PDF bookmarks
194         -xelatex -interaction batchmode $<
195 # Restore the original ISO-8859-1 file if we used iconf
196         $(AT)[ ! -e "$<.lat1" ] || mv $<.lat1 $<
197
198 pdflatex: $(BASENAME).tex $(EXTRAFILES) convert-images
199 #       # Remove old PDF bookmarks file (.out file)
200         -rm -f $(BASENAME).out
201 #       # Use pdflatex for generation:
202         -pdflatex -interaction batchmode $<
203 #       # Remove everything except for chapter entries from the toc file
204         $(AT)[ -e "$(BASENAME).toc" ] || { echo "ERROR: Generated file does not include table of contents file, please review $(BASENAME).log" ; false ; }
205         $(AT)grep '{chapter}' $(BASENAME).toc >temp.toc && mv -f temp.toc $(BASENAME).toc
206 #       # Run pdflatex a second time to get correct page numbers in the toc
207         -pdflatex -interaction batchmode $<
208 #       # Remove everything except for chapter entries from the toc file
209         $(AT)grep '{chapter}' $(BASENAME).toc >temp.toc && mv -f temp.toc $(BASENAME).toc 
210 #       # Tweak the PDF bookmarks file
211         $(AT)[ -e "$(BASENAME).out" ] || { echo "ERROR: Required file $(BASENAME).out does not exist, please review $(BASENAME).log" ; false ; }
212         $(AT)sed 's/\\ellipsis@one =,//' $(BASENAME).out >temp.out && \
213                 echo '\let\WriteBookmarks\relax' >>temp.out && \
214                 mv -f temp.out $(BASENAME).out
215 #       # Run pdflatex a third time to get correct PDF bookmarks
216         -pdflatex -interaction batchmode $<
217
218 clean: clean-images
219         -rm -f $(BASENAME).pdf $(BASENAME).ps 
220         -rm -f *.aux *.toc *.log *.lof *.dvi *.ps *.bbl *.blg *.lot *.out
221
222 distclean: clean
223         -rm -f $(BASENAME).tex $(BASENAME)-untweaked.tex $(BASENAME).tex.orig $(BASENAME).tex.lat1 $(BASENAME).tex.rej
224 # Clean symlinks
225         -rm -f ac1.tex  ac2.tex ac3.tex crtneg.tex  crtpos.tex  random.tex souvenir.sty souverbt.sty    
226         $(AT)for gif in *.gif ; do [ -e $$gif ] && [ -L $$gif ] && rm -f $$gif ; done  || true
227 # Remove tweak file if empty
228         $(AT)test -s tweak.diff || rm -f tweak.diff
229
230 # Clean all images
231 clean-images:
232         -rm -f *.png *.jpeg *.jpg *.pdf
233         -rm -f convert-images link-images
234
235 # Update tweak.diff with the current difference between untweaked and tweaked tex file
236 update-tweakfile:
237         $(AT)if [ -e $(BASENAME)-untweaked.tex -a -e $(BASENAME).tex ] ; then \
238                 if ! diff -q $(BASENAME)-untweaked.tex $(BASENAME).tex; then \
239                         diff -u $(BASENAME)-untweaked.tex $(BASENAME).tex >tweak.diff || echo Updated tweak.diff; \
240                 else  \
241                         echo "Tweak file not updated, no changes";  \
242                 fi; \
243         else \
244                 echo "Tweak file not updated, missing files";  \
245         fi
246                 
247