1#!/usr/local/bin/bash
2# requires: calibre installed for ebook-convert
3
4# mk-pdf
5# Modified from mk-epub
6
7# Created by Sensei on Jan 16, 2018
8# Last edited: Aug 18, 2019
9
10usage() {
11   #printf -v spcs "%*s" ${#prgE} " "   # indent
12   #indent example: $spcs  Options MUST appear before message.
13   cat <<EOF
14
15Usage: $prgE [options]
16
17       Create NCID-UserManual.pdf and NCID-API.pdf from markdown files.
18       
19Options:
20       [-h] [-k]
21
22       -h = show this help
23
24       -k = keep work around files when done instead of deleting them
25            
26EOF
27
28exit 1
29}
30
31prog=`basename $0 .sh`
32prgE=`basename $0` # with extension
33
34MacOS_ebook_prog=/Applications/calibre.app/Contents/MacOS/ebook-convert
35
36# Options on command line
37while getopts :hk opt ; do
38    case $opt in
39        h) usage;;
40        k) keep=1;;
41        :) echo "Option -$OPTARG requires an argument."; usage;;
42        *) echo "Invalid option: -$OPTARG"; usage;;
43    esac
44done
45shift $((OPTIND-1)) # skip over command line args (if any)
46
47# main routine
48
49# check for needed files
50[ -f NCID-API-pdf.md -a -f NCID-UserManual-pdf.md ] || make doc
51
52# create NCID-UserManual-pdf-workaround.md
53sed 's/&#x7[Cc];/@PIPE@/g' > NCID-UserManual-pdf-workaround.md < NCID-UserManual-pdf.md
54
55ebook_prog=$MacOS_ebook_prog
56[ -x "${ebook_prog}" ] || \
57{
58    ebook_prog=`type -p ebook-convert` > /dev/null 2>&1
59    [ $? = "1" ] && ebook_prog=""
60
61    [ -x ${ebook_prog} ] || \
62    {
63      echo "Could not find ${ebook_prog}"
64      exit 1
65    }
66}
67
68${ebook_prog} \
69    NCID-UserManual-pdf-workaround.md NCID-UserManual.pdf \
70    --title "NCID User Manual" \
71    --authors "John L Chmielewski & Todd A Andrews" \
72    --tags "setup, admin" \
73    --language English \
74    --max-toc-link 300 \
75    --input-encoding UTF-8 \
76    --pdf-page-numbers \
77    --pdf-page-margin-top=24 \
78    --pdf-page-margin-right=20 \
79    --pdf-page-margin-bottom=36 \
80    --pdf-page-margin-left=30 \
81    --toc-threshold 6 \
82    --preserve-cover-aspect-ratio \
83    --sr1-search @PIPE@ \
84    --sr1-replace "|"
85
86# remove NCID-UserManual-pdf-workaround.md
87[ -z "$keep" ] && rm -f NCID-UserManual-pdf-workaround.md
88
89# create NCID-API-pdf-workaround.md
90sed 's/&#x7[Cc];/@PIPE@/g' > NCID-API-pdf-workaround.md < NCID-API-pdf.md
91
92${ebook_prog} \
93    NCID-API-pdf-workaround.md NCID-API.pdf \
94    --title "NCID API" \
95    --authors "John L Chmielewski" \
96    --tags "API" \
97    --language English \
98    --max-toc-links 200 \
99    --pdf-page-numbers \
100    --pdf-page-margin-top=24 \
101    --pdf-page-margin-right=20 \
102    --pdf-page-margin-bottom=36 \
103    --pdf-page-margin-left=30 \
104    --toc-threshold 6 \
105    --preserve-cover-aspect-ratio \
106    --sr1-search @PIPE@ \
107    --sr1-replace "|"
108
109# remove NCID-API-pdf-workaround.md
110[ -z "$keep" ] && rm -f NCID-API-pdf-workaround.md
111