|
| 1 | +#! /bin/sh |
| 2 | +# |
| 3 | +# Script to convert LaTeX2e documents to & from having a \documentclass{} |
| 4 | +# option. |
| 5 | + |
| 6 | +VERBOSE=false |
| 7 | +SETOPTION=true |
| 8 | +FILES=`echo ???.tex` |
| 9 | + |
| 10 | +usage() { |
| 11 | + exec >&2 |
| 12 | + echo "usage: $0 [-d] [-v] option [files...]" |
| 13 | + echo |
| 14 | + echo "\t-d disable option, if present" |
| 15 | + echo "\t-v tell which files are being edited, and how" |
| 16 | + echo |
| 17 | + echo "\tBy default, files... will be '???.tex'." |
| 18 | + echo "\tThis will match each of the Python manuals." |
| 19 | + echo |
| 20 | + exit 2 |
| 21 | +} |
| 22 | + |
| 23 | +editing() { |
| 24 | + # tell the user what we're doing |
| 25 | + if $VERBOSE ; then |
| 26 | + echo $1 $FILE... |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +addoption() { |
| 31 | + # add an option not already present |
| 32 | + editing "adding to" |
| 33 | + (sed 's/^\(\\documentclass[[].*\)]/\1,'$OPTION']/ |
| 34 | +s/^\\documentclass{/\\documentclass['$OPTION']{/' $FILE >temp-$$ \ |
| 35 | + && mv temp-$$ $FILE) || exit $? |
| 36 | +} |
| 37 | + |
| 38 | +remoption() { |
| 39 | + # remove an option currently on |
| 40 | + editing "removing from" |
| 41 | + (sed 's/^\(\\documentclass[[].*\),'$OPTION'\([],]\)/\1\2/ |
| 42 | +s/^\\documentclass[[]'$OPTION']/\\documentclass{/ |
| 43 | +s/^\\documentclass[[]'$OPTION',/\\documentclass[/' $FILE >temp-$$ \ |
| 44 | + && mv temp-$$ $FILE) || exit $? |
| 45 | +} |
| 46 | + |
| 47 | +chkoption() { |
| 48 | + # return true iff the option is already on |
| 49 | + egrep '^\\documentclass[[]([A-Za-z0-9]*,)*'$OPTION'[],]' $FILE >/dev/null |
| 50 | + return $? |
| 51 | +} |
| 52 | + |
| 53 | +# parse the command line... |
| 54 | +while [ "$#" -gt 0 ] ; do |
| 55 | + case "$1" in |
| 56 | + -d) |
| 57 | + SETOPTION=false |
| 58 | + shift |
| 59 | + ;; |
| 60 | + -v) |
| 61 | + VERBOSE=true |
| 62 | + shift |
| 63 | + ;; |
| 64 | + -*) |
| 65 | + usage |
| 66 | + ;; |
| 67 | + *) |
| 68 | + break; |
| 69 | + ;; |
| 70 | + esac |
| 71 | +done |
| 72 | +if [ -z "$1" ] ; then |
| 73 | + usage |
| 74 | +fi |
| 75 | + |
| 76 | +# setup variables |
| 77 | +OPTION="$1" |
| 78 | +shift |
| 79 | +FILES=${1:+$*} |
| 80 | +if [ "$FILES" = '' ] ; then |
| 81 | + FILES=`echo ???.tex` |
| 82 | +fi |
| 83 | + |
| 84 | +# check each file and do the work as required |
| 85 | +for FILE in $FILES ; do |
| 86 | + if chkoption ; then |
| 87 | + if $SETOPTION ; then |
| 88 | + : |
| 89 | + else |
| 90 | + remoption |
| 91 | + fi |
| 92 | + elif $SETOPTION ; then |
| 93 | + addoption |
| 94 | + fi |
| 95 | +done |
0 commit comments