Add script to rsync the electronic books (epub et al) to the
[project-aon.git] / common / epub / Makefile.common
1 #
2 # Root directory
3 AONROOTREL := ../../../
4 AONROOTDIR = $(realpath $(AONROOTREL))
5 # Base directory we will install into
6 INSTALLDIR := $(AONROOTDIR)/../PUBLISH/$(BLANG)/
7 # Relative location of the XML files
8 RELXMLDIR := $(AONROOTDIR)/$(BLANG)/xml/
9 # Relative location of the scripts
10 RELSCRIPT := $(AONROOTDIR)/common/scripts
11 # Relative location of the xsl files
12 RELXSLDIR := $(AONROOTDIR)/common/xsl
13 EPUBSCRIPT := $(RELSCRIPT)/gbtoepub.pl
14 COVERSCRIPT := $(RELSCRIPT)/create-epub-cover.pl
15 COVERIMAGE := $(AONROOTDIR)/$(BLANG)/jpeg/$(SERIES)/$(BASENAME)/skins/ebook/cover.jpg
16 ALTCOVERIMAGE := $(AONROOTDIR)/$(BLANG)/epub/$(SERIES)/$(BASENAME)/OEBPS/cover.jpg 
17
18 # -------------------------------- BEGIN FONT SELECTION -----------------------------------------------------------
19 # Location of the font files, this is adjustable here in order to make it possible to run
20 # the build scripts as not all developers might have the Souvernir fonts.
21
22 # Option 1 (to be used for the Lone wolf and Grey star series) - Souvenir font 
23 # ---------------------------------------------------------------------------
24 # Use this when buidling Lone Wolf books, using Souvenir fonts and make sure
25 # the font is available in the directory you configure here.  Note that the
26 # fontfiles are *not* available in Project Aon's SVN repository as they are not
27 # under a free license. For more information see /common/fonts/README.txt
28 #
29 # If using this configure, please make sure the following fonts are available in this directory:
30 # SouvenirStd-DemiItalic.otf  SouvenirStd-Demi.otf  SouvenirStd-LightItalic.otf  SouvenirStd-Light.otf
31
32 ifeq ($(SERIES), lw)
33 FONTDIR := $(AONROOTDIR)/../fontfiles/
34 endif
35 ifeq ($(SERIES), ls)
36 FONTDIR := $(AONROOTDIR)/../fontfiles/
37 endif
38 ifeq ($(SERIES), gs)
39 FONTDIR := $(AONROOTDIR)/../fontfiles/
40 endif
41
42 # Option 2 (to be used for Freway Warrior series builds) - Carlito font 
43 # ---------------------------------------------------------------------
44 # Use this when running builds of the Freeway Warrior series using the Carlito
45 # fonts.  This will build the documents using the fonts available in the SVN
46 # repository which (at the time of this writting) are the Carlito font, which
47 # is distributed with an Open Font license.
48
49 ifeq ($(SERIES), fw)
50 FONTDIR := $(AONROOTDIR)/common/fonts/
51 endif
52 # --------------------------------- END FONT SELECTION -----------------------------------------------------------
53
54 ifndef FONTDIR
55 $(info FONTDIR is not defined,  defauling to AONROOTDIR/common/fonts/)
56 FONTDIR := $(AONROOTDIR)/common/fonts/
57 endif
58
59 # Sanity checks before going further
60 ifndef SERIES
61 $(error WARNING: Book series is not defined, please define variable 'SERIES')
62 endif
63 ifndef BLANG
64 $(error WARNING: Book language is not defined, please define variable 'BLANG')
65 endif
66 ifndef BASENAME
67 $(error WARNING: Book basic name is not defined, please define variable 'BASENAME')
68 endif
69
70
71 EPUB=$(BASENAME).epub
72 MOBI=$(BASENAME).mobi
73 PDB=$(BASENAME).pdb
74 LRF=$(BASENAME).lrf
75 FB2=$(BASENAME).fb2
76 EBOOKS=$(EPUB) $(MOBI) $(PDB) $(LRF) $(FB2)
77 # To build only epub books:
78 # EBOOKS=$(EPUB)
79
80 # Formats we are compiling to
81 FORMATS=$(subst $(BASENAME).,,$(EBOOKS))
82
83 all: $(EBOOKS)
84
85 formats:
86         @echo $(FORMATS)
87
88 XMLFILE=$(patsubst %.epub,$(RELXMLDIR)/%.xml,$(BOOK))
89
90 # XSL files the ePubs depend on. If these are modified the
91 # ePub file needs to be regenerated
92 XSLFILES=$(RELXSLDIR)/epub-opf-metadata.xsl $(RELXSLDIR)/epub-xhtml.xsl  \
93         $(RELXSLDIR)/epub-ncx.xsl $(RELXSLDIR)/epub-opf-spine.xsl
94
95 # Generate the ePub file from the XML file
96 epub: $(EPUB)
97 $(AONROOTDIR)/$(BLANG)/epub/$(SERIES)/$(BASENAME)/$(EPUB): $(XMLFILE) $(XSLFILES)
98         cd $(AONROOTDIR) && perl $(EPUBSCRIPT) --language=$(BLANG) --font-files=$(FONTDIR) $(BASENAME) 
99 # Create cover page if not existing
100         @if [ ! -e $(COVERIMAGE) ] ; then \
101                 cd $(AONROOTDIR) && perl  $(COVERSCRIPT) --language=$(BLANG) --font-files=$(FONTDIR) $(BASENAME) ; \
102         fi
103
104 $(EPUB): $(AONROOTDIR)/$(BLANG)/epub/$(SERIES)/$(BASENAME)/$(EPUB)  
105         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
106         cp -p $(AONROOTDIR)/$(BLANG)/epub/$(SERIES)/$(BASENAME)/$(EPUB) .
107 # Regenerate to include the covers properly within the Ebook and fix the EPUB
108         @if [ -e $(ALTCOVERIMAGE) ] && [ ! -e $(COVERIMAGE) ]  ; then \
109                 ebook-convert $(EPUB) regen_$(EPUB) --cover $(ALTCOVERIMAGE) ; \
110         fi
111         @if [ -e $(COVERIMAGE) ]  ; then \
112                 ebook-convert $(EPUB) regen_$(EPUB) --cover $(COVERIMAGE) ; \
113         fi
114         @if [ ! -e $(ALTCOVERIMAGE) ] && [ ! -e $(COVERIMAGE) ]  ; then \
115                 ebook-convert $(EPUB) regen_$(EPUB) ; \
116         fi
117                 
118         mv regen_$(EPUB) $(EPUB)
119         -rm -f cover.jpg $(BASENAME).opf  
120
121
122 # Convert to other formats. For a full list of format that we can convert to using 
123 # Calibre see http://manual.calibre-ebook.com/cli/ebook-convert.html
124
125 # Generate the mobi file from the ePub (Mobipocket format)
126 mobi: $(MOBI)
127 %.mobi: %.epub
128         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
129          ebook-convert $< $@
130
131 # Generate the pdb file from the ePub (eReader / Palm Media format)
132 %.pdb: %.epub
133         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
134          ebook-convert $< $@
135
136 # Generate the lrf file from the ePub (Broadband eBooks (BBeB) format)
137 %.lrf: %.epub
138         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
139          ebook-convert $< $@
140
141 # Generate the lit file from the ePub (Microsoft LIT format for Microsoft Reader)
142 %.lit: %.epub
143         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
144          ebook-convert $< $@
145
146 # Generate the pdf file from the ePub 
147 %.pdf: %.epub
148         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
149          ebook-convert $< $@
150
151 # Generate the snb file from the ePub 
152 %.snb: %.epub
153         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
154          ebook-convert $< $@
155
156 # Generate the pml file from the ePub 
157 %.pml: %.epub
158         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
159          ebook-convert $< $@
160
161 # Generate the fb2 file from the ePub  (Fictionbook format used by BeBook, PocketBook, Cybook, Papyre and others)
162 %.fb2: %.epub 
163         @[ -x  `which ebook-convert` ] || { echo "ERROR: Cannot find 'ebook-convert', please install Calibre" >&2; exit 1;  }
164          ebook-convert $< $@
165
166
167 # Alternative build for the ePub format using open source tools instead
168 # of our script.
169 # Sample Makefile code:
170 #
171 #DOCBOOK_XSL=/usr/share/xml/docbook/stylesheet/docbook-xsl/epub/docbook.xsl
172 #$(EPUB): $(XMLFILE) $(DOCBOOK_XSL)
173 #       @[ -x  `which dbtoepub` ] || { echo "ERROR: Cannot find 'dbtoepub', please install this package" >&2; exit 1;  }
174 #       if [ -x  /usr/bin/dbtoepub ] ; then  \
175 #               dbtoepub -o $(EPUB) $(XMLFILE) >$(BOOK).log 2>&1; \
176 #       else \
177 #               xsltproc $(DOCBOOK_XSL) $< >$(BOOK).log 2>&1; \
178 #               echo "application/epub+zip" > mimetype ; \
179 #               zip -0Xq  $@ mimetype; \
180 #               zip -Xr9D $@ META-INF/* OEBPS/* ; \
181 #       fi
182 # TODO:
183 # Embed font in dbtoepub using -f
184
185 epub-check: $(EPUB)
186         java -jar epubcheck.jar $<
187
188
189 # Final installation step of generate ebooks
190 install: $(INSTALLDIR) $(EBOOKS)
191         @for format in $(FORMATS); do \
192                 PUBLISHDIR="$(INSTALLDIR)/$$format/$(SERIES)" ; \
193                 file=$(BASENAME).$$format ; \
194                 [ ! -e "$$PUBLISHDIR" ] && \
195                         mkdir -p "$$PUBLISHDIR" ; \
196                 echo "Copying $$file to $$PUBLISHDIR" ; \
197                 cp -p $$file $$PUBLISHDIR && \
198                 chmod 664 $$PUBLISHDIR/$$file ;  \
199         done
200 publish: install
201
202 $(INSTALLDIR):
203         mkdir -p $@
204
205 clean: 
206         -rm -f $(EBOOKS) $(EPUB).log regen_$(EPUB) cover.jpg $(BASENAME).opf 
207
208 distclean: clean
209         -rm -rf $(AONROOTDIR)/$(BLANG)/epub/$(SERIES)/$(BASENAME)/
210
211 .PHONY: all install publish clean distclean epub mobi ebook-convert