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