#!/usr/bin/env bash # Builds the free (nag) and nag-free editions of both books, PDF and EPUB. # # Usage: ./build.sh [version] (default version: 1.1) # Output: build/, e.g. build/atlas_kicks_the_ball_1_1.pdf (free edition) # and build/atlas_kicks_the_ball_1_1_nagfree.pdf (paid edition). # # PDFs: pdflatex (run twice). If pdflatex isn't on this machine, it runs # inside the `atlas` distrobox container. # EPUBs: pandoc on this machine. # # How the two editions differ: the sources define \nagpage with # \providecommand. The nag-free build pre-defines it as empty before reading # the source, so the same .tex files produce both editions. set -euo pipefail cd "$(dirname "$0")" VERSION="${1:-1.1}" V="${VERSION//./_}" OUT=build mkdir -p "$OUT" if command -v pdflatex >/dev/null 2>&1; then latex() { "$@"; } else latex() { distrobox enter atlas -- "$@"; } fi command -v pandoc >/dev/null 2>&1 || { echo "pandoc not found"; exit 1; } CLEAN='\newcommand{\nagpage}{}' # source file | output base name | EPUB language BOOKS=( "atlas_kicks_the_ball_1_0.tex|atlas_kicks_the_ball_${V}|en-GB" "atlaksen_potku_published.tex|atlaksen_potku_${V}|fi" ) for book in "${BOOKS[@]}"; do IFS='|' read -r SRC NAME LANG <<<"$book" echo "== $NAME" for pass in 1 2; do latex pdflatex -interaction=nonstopmode -halt-on-error \ -output-directory="$OUT" -jobname="$NAME" "$SRC" >/dev/null latex pdflatex -interaction=nonstopmode -halt-on-error \ -output-directory="$OUT" -jobname="${NAME}_nagfree" "${CLEAN}\input{${SRC}}" >/dev/null done pandoc "$SRC" -M lang="$LANG" -o "$OUT/$NAME.epub" printf '%s\n\\input{%s}\n' "$CLEAN" "$SRC" | pandoc -f latex -M lang="$LANG" -o "$OUT/${NAME}_nagfree.epub" done rm -f "$OUT"/*.aux "$OUT"/*.log "$OUT"/*.out "$OUT"/*.toc echo "Done:" ls -1 "$OUT"