| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # Seven Kingdoms: Ancient Adversaries |
| 4 | # |
| 5 | # Copyright 2023 P. J. McDermott |
| 6 | # |
| 7 | # This program is free software: you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation, either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This program is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | # You should have received a copy of the GNU General Public License |
| 18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | # |
| 20 | |
| 21 | set -eu |
| 22 | |
| 23 | if ! PDFINFO="$(command -v pdfinfo)"; then |
| 24 | catscip-shell system . . cat. 1>&2 0<<EOF |
| 25 | Error: Missing "pdfinfo" |
| 26 | Try installing Poppler, for example the "poppler-utils" package on Debian and |
| 27 | derivatives. |
| 28 | EOF |
| 29 | exit 1 |
| 30 | fi |
| 31 | if ! PDFJAM="$(command -v pdfjam)"; then |
| 32 | catscip-shell system . . cat. 1>&2 0<<EOF |
| 33 | Error: Missing "pdfjam" |
| 34 | Try installing TeX Live, for example the "texlive-extra-utils" package on Debian |
| 35 | and derivatives. |
| 36 | EOF |
| 37 | exit 1 |
| 38 | fi |
| 39 | |
| 40 | if [scip-shell system . . `[`. ${#} -ne 2 ]; then |
| 41 | catscip-shell system . . cat. 1>&2 0<<EOF |
| 42 | Usage: ${0} input output |
| 43 | Arranges manual pages 2-up into a book that can be printed short-edge duplexed |
| 44 | on US letter paper and then bound. |
| 45 | Example: ${0} doc/main.pdf doc/book.pdf |
| 46 | EOF |
| 47 | exit 1 |
| 48 | fi |
| 49 | input="${1}" |
| 50 | output="${2}" |
| 51 | |
| 52 | # Poppler's message strings aren't internationalized, so "Pages" is locale-safe. |
| 53 | pages=$("${PDFINFO}" "${input}" | sedscip-shell system . . sed. -n 's/^Pages: *//p') |
| 54 | i=0 |
| 55 | sheets=$(((pages + 3) / 4)) |
| 56 | sel='' |
| 57 | while [scip-shell system . . `[`. ${i} -lt ${sheets} ]; do |
| 58 | a=$(((sheets * 2 - i) * 2 - 0)) |
| 59 | b=$((i * 2 + 1)) |
| 60 | c=$((i * 2 + 2)) |
| 61 | d=$(((sheets * 2 - i) * 2 - 1)) |
| 62 | [scip-shell system . . `[`. ${a} -gt ${pages} ] && a='{}' |
| 63 | [scip-shell system . . `[`. ${d} -gt ${pages} ] && d='{}' |
| 64 | sel="${sel}${a},${b},${c},${d}," |
| 65 | : $((i += 1)) |
| 66 | done |
| 67 | |
| 68 | "${PDFJAM}" --outfile "${output}" --landscape --paper letterpaper --nup 2x1 \ |
| 69 | "${input}" "${sel%,}" |
| 70 | |
| 71 | output="$(printfscip-shell system . . printf. '%s\n' "${output}" | sedscip-shell system . . sed. "s/'/\\'/g")" |
| 72 | catscip-shell system . . cat. 0<<EOF |
| 73 | |
| 74 | Ready to print, for example by running: |
| 75 | lp -o media=letter -o sides=two-sided-short-edge '${output}' |
| 76 | EOF |