1#!/bin/bash
2#
3# usage: build_help_pdf.sh [build_tree]
4#
5#    build the pdf PHD2 documentation from help source files
6#
7# Require:  wkhtmltopdf
8#           available from http://wkhtmltopdf.org/
9#
10
11build=$(cd $(dirname $0); /bin/pwd)
12top="$build/.."
13help="$top"/help
14
15BUILDTREE=$top/tmp
16if [ -n "$1" ]; then
17    BUILDTREE=$1
18    shift
19fi
20
21TMP=/tmp/phd2help$$
22mkdir "$TMP"
23trap "rm -rf '$TMP'" 2 3 15
24
25# Get the list of html files in the right order from PHD2GuideHelp.hhc
26files=$(grep '<param name="Local" value=' $help/PHD2GuideHelp.hhc \
27          | grep 'htm">' | cut -d\" -f4 | awk '!x[$0]++ {print "'$help\/'" $0}')
28
29phdversion=$("$build"/get_phd_version "$top"/phd.h)
30date=$(LC_ALL=C date '+%B %d, %Y')
31
32# create pdf
33if [ -z "$DISPLAY" ]; then
34    useX=''
35else
36    useX='--use-xserver'
37fi
38
39titlepg="$TMP"/00_title.html
40tocxsl="$TMP"/toc.xsl
41output="$BUILDTREE"/PHD2_User_Guide.pdf
42tocxsl_native=$tocxsl
43logo="$top"/icons/phd2_128.png
44
45case $(uname -o) in
46    Cygwin)
47        # The Cygwin port of wkhtmltopdf does not work (2016/1/20)
48        # Under Cygwin we can use the Win32 native port, but we have
49        # to go through some contortions to pass DOS-style paths
50
51        wkhtmltopdf="/c/Program Files/wkhtmltopdf/bin/wkhtmltopdf.exe"
52
53        touch "$titlepg"
54        titlepg=$(cygpath -d "$titlepg")
55
56        touch "$tocxsl"
57        tocxsl=$(cygpath -d "$tocxsl")
58
59        s=""
60        for f in $files; do
61            d=$(cygpath -d "$f")
62            s="$s $d"
63        done
64        files=$s
65
66        touch "$output"
67        output=$(cygpath -d "$output")
68
69        logo=$(cygpath -d "$logo")
70        ;;
71    *)
72        wkhtmltopdf="wkhtmltopdf"
73        ;;
74esac
75
76# generate the title page
77
78cat >"$titlepg" <<EOF
79<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
80<html>
81  <head>
82    <meta content="text/html; charset=utf-8" http-equiv="content-type">
83    <title>Cover_Page</title>
84  </head>
85  <body>
86    <center>
87      <br><br><br><br><br><br><br><br><br><br><br><br>
88      <img src="file:///${logo}">
89      <br><br><br><br><br>
90      <h1>PHD2 v${phdversion}</h1>
91      <h1>User Guide</h1>
92      <br><br><br><br><br><br><br><br><br>
93      <br><br><br><br><br><br><br><br><br>
94      <h2>${date}</h2>
95    </center>
96  </body>
97</html>
98EOF
99
100# generate the default toc
101"$wkhtmltopdf" --dump-default-toc-xsl >"$tocxsl_native"
102
103"$wkhtmltopdf" -q $useX --dpi 96 --enable-toc-back-links --enable-external-links \
104               --enable-internal-links --footer-right '[page]' \
105               cover "$titlepg" toc --xsl-style-sheet "$tocxsl" \
106               $files "$output"
107
108# cleanup
109rm -rf "$TMP"
110