-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake.sh
executable file
·351 lines (303 loc) · 8.19 KB
/
make.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/sh
### module info ###
# The build environment expects to live in a subdir build/
# It expects to find book information in the parent dir
# ./books/
# ./images/
# ./modules/
# ./build/
### settings ###
OUTPUTDIR="./output"
redirfile="$OUTPUTDIR/debug.txt"
HTMLDIR="$OUTPUTDIR/html"
HTMLIMGDIR="$HTMLDIR/images"
IMAGESDIR="./images"
MODULESDIR="./modules"
BUILDDIR="./build"
LIBDIR="$BUILDDIR/lib"
XSLFILE="$LIBDIR/lt.xsl"
FOPDIR="$LIBDIR/fop"
export FOP_OPTS="-Xms512m -Xmx512m"
export BOOKSDIR=./books
### initialisation ###
V=""
CHAPTERS=""
APPENDIX=""
DATECODE=$(date +%y%m%d | sed s/^0//)
PUBDATE=$(date +%c)
YEAR=$(date +%Y)
### functions ###
help() {
echo
echo "linux-training book build script\t\thttp://linux-training.be"
echo
echo $0 [OPTION] command [book]
echo
echo "Options"
echo " -d 0,1,2,3,4 Set debug level, 1 is default"
echo " 0 No output"
echo " 1 Standard output"
echo " 2 Verbose output"
echo " 3 Extra verbose output"
echo " 4 Debug output"
echo " -h Help"
echo
echo "Commands"
echo " clean clean output dir"
echo " build [BOOK] build book"
echo " html [BOOK] generate html"
echo
echo "Available books:" $books
echo
}
set_JAVA() {
# use the correct java runtime for fop on Ubuntu
# according to http://linuxmafia.com/faq/Admin/release-files.html
if [ -f /etc/lsb-release ]
then #Ubuntu
export JAVA_HOME=/usr/lib/jvm/java-6-sun/jre
elif [ -f /etc/debian_version ]
then # use the correct java runtime for fop on Debian Lenny
export JAVA_HOME=/usr/lib/jvm/default-java/jre
else echor Could not set JAVA_HOME, something unexpected happened in $0
fi
}
check_ROOTDIR() {
if [ -x make.sh -a -x buildheader.pl ]
then echor "We are in the build directory, changing to parent directory."
cd ..
fi
if [ -d $BOOKSDIR -a -d $BOOKSDIR -a -d $MODULESDIR -a -d $BUILDDIR ]
then echor "Current dir is book project root directory."
else echor "Please run this script from the book root directory."
return 1
fi
}
add_mod() {
MODULES=${MODULES}" "$MODULESDIR/$1
}
add_chapt() {
CHAPTERS=${CHAPTERS}" "$1
}
add_appen() {
APPENDIX=${APPENDIX}" "$1
}
echor() { # echo error
echo $* >&2
}
echod() { # echo debug
[ $OPTDEBUG -ge 2 ] && echo $*
}
clean() {
echo "Cleaning up $OUTPUTDIR directory"
# We don't need the .xml files
rm -rf $V $OUTPUTDIR/*.xml
# We don't need the previous errors.txt
[ -f $redirfile ] && rm -rf $V $redirfile
# Symlink creation fails unless we remove this symlink first
[ -h $OUTPUTDIR/book.pdf ] && rm -rf $V $OUTPUTDIR/book.pdf
# Clean $HTMLDIR
[ -d $HTMLDIR ] && rm -rf $V $HTMLDIR/*.xml
}
check_book() {
if [ ! -z $book ]
then # check if $book parameter is one of the available books
echo -n "Checking if $book.cfg exists in ./books directory ... "
check=0
for entry in $books ; do
if [ $entry = $book ]
then check=1
fi
done
if [ $check = 1 ]
then echo "Selected book $book"
else echo "$book is not available"; exit
fi
else
echo "No book specified, assuming default book"
book="default"
fi
}
build_header() {
cat modules/header/doctype.xml | sed s@LIBDIR@../$LIBDIR@g > $headerfile
echo "<book>" >> $headerfile
echo "<bookinfo>" >> $headerfile
echo "<title>$BOOKTITLE</title>" >> $headerfile
$BUILDDIR/buildheader.pl \
"modules/header/abstract.xml" \
"$BOOKSDIR/$book/copyrights" \
"$BOOKSDIR/$book/authors" \
"$BOOKSDIR/$book/contributors" \
"$BOOKSDIR/$book/reviewers" \
"$PUBDATE" \
"$YEAR" \
"$VERSIONSTRING" \
"$TEACHER" >> $headerfile
echo "</bookinfo>" >> $headerfile
}
build_footer() {
cat modules/footer/footer.xml >$footerfile
}
build_body() {
for chapter in $CHAPTERS; do
echod -n "Building chapter $chapter "
modfile=$OUTPUTDIR/mod_$chapter.xml
# load the chapter specific settings
echod -n "\t.. loading settings chapt_$chapter"
eval chapt_$chapter
# Generate the end chapter tag
echo "<chapter><title>"$chaptitle"</title>" > $modfile
# Generate all the sections
for module in $MODULES
do
echod -n "\t.. adding module $module"
cat $module >> $modfile
done
echod
# Generate the end chapter tag
echo "</chapter>" >> $modfile
cat $modfile >> $bodyfile
done
for appendix in $APPENDIX; do
echod -n "Building appendix $appendix .. "
modfile=$OUTPUTDIR/mod_$appendix.xml
# load the chapter specific settings
echod " .. loading settings chapt_$appendix"
eval chapt_$appendix
# Generate the end chapter tag
echo "<appendix><title>"$chaptitle"</title>" > $modfile
# Generate all the sections
for module in $MODULES; do
echod " adding module $module"
cat $module >> $modfile
done
# Generate the end chapter tag
echo "</appendix>" >> $modfile
cat $modfile >> $bodyfile
done
}
build_xml() {
echo -n "Parsing config $BOOKSDIR/$book/config ... "
. $BOOKSDIR/$book/config
. $BOOKSDIR/$book/version
VERSIONSTRING=lt-$MAJOR.$MINOR
echo "Generating book $book (titled \"$BOOKTITLE\")"
[ -d $OUTPUTDIR ] || mkdir $OUTPUTDIR
BOOKTITLE2=$(echo $BOOKTITLE | sed -e 's/\ /\_/g' -e 's@/@-@g' )
filename=$BOOKTITLE2-$VERSIONSTRING-$DATECODE
xmlfile=$OUTPUTDIR/$filename.xml
pdffile=$OUTPUTDIR/$filename.pdf
headerfile=$OUTPUTDIR/section_header.xml
footerfile=$OUTPUTDIR/section_footer.xml
bodyfile=$OUTPUTDIR/section_body.xml
# make header
build_header
# make body
build_body
# make footer
build_footer
# build master xml
echo "Building $xmlfile"
cat $headerfile > $xmlfile
cat $bodyfile >> $xmlfile
cat $footerfile >> $xmlfile
}
build_book() {
set_JAVA
echo
echo "---------------------------------"
echo "Generating $pdffile"
eval $(echo $FOPDIR/fop -xml $xmlfile -xsl $XSLFILE -pdf $pdffile $EXECDEBUG) >&2
ln -s $V $filename.pdf $OUTPUTDIR/book.pdf
echo "---------------------------------"
}
build_html() {
[ -d $HTMLDIR ] && rm -rf $V $HTMLDIR
mkdir $HTMLDIR || ( echor Error creating $HTMLDIR; exit 1 )
mkdir $HTMLIMGDIR || ( echor Error creating $HTMLIMGDIR; exit 1 )
# We only need the one xml file
cp $xmlfile $HTMLDIR || ( echor error copying $xmlfile ; exit 1 )
# Locate the used images in the xml file
images=`grep imagedata $HTMLDIR/*.xml | cut -d/ -f2 | cut -d\" -f1`
# Copy all the used images
for img in $images
do
echo Copying $img to $HTMLIMGDIR ...
cp $V "$imgdir/$img" $HTMLIMGDIR/ || echor Error copying $img
done
# Run xmlto in $HTMLDIR to generate the html
echo "Converting xml to html ..."
( cd $HTMLDIR && xmlto html *.xml 2>&1 | grep -v "Writing" ) || ( echor Error generating the html $HTMLDIR ; exit 1 )
# don't need the xml anymore in the $HTMLDIR
rm $HTMLDIR/*.xml
}
while getopts "d: h" option
do
case $option in
d ) OPTDEBUG=$OPTARG
shift 2
;;
h ) help
shift 1
exit 0
;;
esac
done
command=$1
book=$2
case $OPTDEBUG in
0) # DEBUG 0 is zero output (except help message)
REDIR=">$redirfile 2>&1"
;;
"" | 1) # DEBUG 1 is default, only STDOUT
REDIR="2>$redirfile"
;;
2) # DEBUG 2 is all output we get normally
REDIR=""
;;
3) # DEBUG 3 is all output we get normally + verbose flag everywhere
REDIR=""
V="-v"
;;
4) # DEBUG 4 is all output we get normally + fop exec debug on + verbose flag everywhere
REDIR=""
EXECDEBUG="--execdebug"
V="-v"
;;
*) help
exit 0
;;
esac
##############
check_ROOTDIR || exit 1
books=$( cd $BOOKSDIR ; find * -maxdepth 1 -type d )
mkdir -p $OUTPUTDIR
# Redirect everything according to REDIR var from now on.
eval "exec $REDIR"
# Main loop
case "$command" in
clean)
clean
;;
build)
clean
check_book
echo "Building '$book' book."
build_xml
build_book
echo "Done generating pdf $OUTPUTDIR/book.pdf -> $pdffile"
;;
html)
[ -x "$(which xmlto)" ] || echor "xmlto not installed." || exit 1
clean
check_book
echo "Building '$book' book."
build_xml
echo "Generating html for '$book' book."
build_html
echo "Done Generating html for '$book' book."
;;
*)
help
;;
esac