summaryrefslogtreecommitdiff
path: root/script/epub_font_stuffer.sh
blob: fe9939f365e71c9b1cab47b99a434d085f300194 (plain) (blame)
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
#!/usr/bin/env bash
#
# epub_font_stuffer
# Charlotte Koch <charlotte@magentastripe.com>
#
# By default, the EPUB generated by asciidoctor-epub3(1) includes only a
# base set of fonts. This script augments the EPUB by including *all* the
# fonts from the given directory.
#
# This file is part of WilloraPDF.
#
# REQUIREMENTS: zip(1), unzip(1), rsync(1)
# 

set -e

INPUT=""
OUTPUT=""
FONTDIR=""

# Parse and verify command line arguments.

die() {
  echo "FATAL: $1"
  exit 1
}

while [ $# -gt 0 ]; do
  case "$1" in
    --input)
      INPUT="$2"
      shift 2
      ;;
    --output)
      OUTPUT="$2"
      shift 2
      ;;
    --fontdir)
      FONTDIR="$2"
      shift 2
      ;;
    *)
      die "Unknown option: $1"
      ;;
  esac
done

test -n "${INPUT}" || die "Missing argument: --input"
test -n "${OUTPUT}" || die "Missing argument: --output"
test -n "${FONTDIR}" || die "Missing argument: --fontdir"

test -f "${INPUT}" || die "Can't find input file: ${INPUT}" 
test -d "${FONTDIR}" || die "Can't find font directory: ${FONTDIR}"

# Work around the need to change directories in the subsequent zip(1)
# command
real_output="$(pwd)/${OUTPUT}"

# Extract the EPUB to a temporary location, copy the additional fonts into
# it, then zip it back up.
workdir="$(mktemp -d)"

function cleanup() {
  rm -rf ${workdir}
}

trap cleanup EXIT

unzip -q -d ${workdir} ${INPUT}
rsync -avr ${FONTDIR}/ ${workdir}/EPUB/fonts/
cd ${workdir}
zip -r ${real_output} .
cd -