| 1 | #!/bin/bash |
| 2 | # Copyright (C) 2021 by Robert J Lee |
| 3 | # |
| 4 | # This file may be distributed and/or modified under the |
| 5 | # conditions of the LaTeX Project Public License, either |
| 6 | # version 1.3 of this license or (at your option) any later |
| 7 | # version. The latest version of this license is in: |
| 8 | # |
| 9 | # http://www.latex-project.org/lppl.txt |
| 10 | # |
| 11 | # and version 1.3 or later is part of all distributions of |
| 12 | # LaTeX version 2005/12/01 or later. |
| 13 | # |
| 14 | # A utility script to find the best seed number for a particular gamebook |
| 15 | # |
| 16 | # Prerequisites: bash, tee, tail, egrep, pdflatex, perl with Sort::Naturally |
| 17 | # |
| 18 | # To install Sort::Naturally, you can simply type "cpan install Sort::Naturally" |
| 19 | # assuming Perl is installed. |
| 20 | # |
| 21 | # First keep a backup of your .tex file, just in case. |
| 22 | # |
| 23 | # Next, ensure that you have a line containing "seed=<numerals>," |
| 24 | # (including the comma) in your .tex file. |
| 25 | # |
| 26 | # The first occurence will be replaced with a sequence of numbers in turn, |
| 27 | # the PDF regenerated, and the number of output pages with each number reported |
| 28 | # on, first by seed number, then by reversed number of pages (smallest last). |
| 29 | # It's then up to you to choose the seed number appropriate for your work. |
| 30 | # |
| 31 | # Use this script with caution as it will edit your source file. |
| 32 | # |
| 33 | # The script may be placed on the system path or copied to the working directory. |
| 34 | |
| 35 | file=${1} |
| 36 | |
| 37 | if [[ -z $file ]]; then |
| 38 | echo "Usage: $0 <file>.tex" |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | egrep -q "seed=[0-9]+," $file || ( |
| 43 | echo "$1 Does not contain \\usepackage[seed=123,quiet]{lcg}" |
| 44 | exit 2 |
| 45 | ); |
| 46 | |
| 47 | pdf="pdflatex -interaction=nonstopmode $file" |
| 48 | |
| 49 | cp $file $file~ || exit 3 |
| 50 | |
| 51 | # FOR seed in (1..1000) |
| 52 | for seed in $(perl -e 'for (1..1000){print "$_\n"}'); do |
| 53 | # edit $file and replace any seed values |
| 54 | perl -i -pe "s/seed=[0-9]*,/seed=$seed,/" $file |
| 55 | # Run it through LaTeX, stripping out the line of output containing the number of pages, and then |
| 56 | # add the seed value |
| 57 | echo $($pdf 2>&1 | grep 'pages' | tail -n 1 | perl -pe "chomp") "; seed=$seed"; |
| 58 | done | \ |
| 59 | # report on the output as we get it, and report on the sorted pages at the end. |
| 60 | # (sort numerically by first numeral on lines with identical prefix) |
| 61 | perl -MSort::Naturally -pe \ |
| 62 | '{push @lines, $_}; END{print "SORTED:"; print reverse nsort(@lines)}' |