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