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