#!/usr/bin/env bash # # epub_font_stuffer # Charlotte Koch # # 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 -