| 1 | #!/bin/sh |
| 2 | |
| 3 | # Copyright (C) 2004, Thomas Esser. Public domain. |
| 4 | # 2005-03-08: New option "--crop", Rolf Niepraschk. |
| 5 | # dvips call only if the dvi file exist (RN). |
| 6 | # 2005-06-27: ps2pdf call only if the ps file exist (RN). |
| 7 | # 2006-04-05: "\RequirePackage{pst-pdf}" added to the (pdf)LaTeX calls |
| 8 | # as suggested by Niall Murphy. |
| 9 | # 2006-07-14: Better temp dir handling (suggested by Karl Berry) (RN). |
| 10 | # 2006-07-23: New option --Xps2pdf and code cleanup (thanks to Karl Berry) (RN). |
| 11 | # 2008-08-04: Remove the parameter "-Ppdf" from the dvips call (RN). |
| 12 | # 2016-07-02: New option "--xelatex", (hv) |
| 13 | # "--lualatex" , (hv) |
| 14 | # 2016-07-11: Better tests if xelatex or lualatex (RN) |
| 15 | |
| 16 | # First, work around bugs/limitations in some shells on some systems: |
| 17 | test -f /bin/sh5 && test -z "$RUNNING_SH5" \ |
| 18 | && { UNAMES=`uname -s`; test "x$UNAMES" = xULTRIX; } 2>/dev/null \ |
| 19 | && { RUNNING_SH5=true; export RUNNING_SH5; exec /bin/sh5 $0 ${1+"$@"}; } |
| 20 | unset RUNNING_SH5 |
| 21 | |
| 22 | test -f /bin/bsh && test -z "$RUNNING_BSH" \ |
| 23 | && { UNAMES=`uname -s`; test "x$UNAMES" = xAIX; } 2>/dev/null \ |
| 24 | && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; } |
| 25 | unset RUNNING_BSH |
| 26 | |
| 27 | # hack around a bug in zsh: |
| 28 | test -n "${ZSH_VERSION+set}" && alias -g '${1+"$@"}'='"$@"' |
| 29 | |
| 30 | ############################################################################### |
| 31 | # cleanup() |
| 32 | # clean up the temp area and exit with proper exit status |
| 33 | ############################################################################### |
| 34 | cleanup() |
| 35 | { |
| 36 | rc=$? |
| 37 | $needsCleanup && test -n "$tmpdir" && test -d "$tmpdir" \ |
| 38 | && { rm -f "$tmpdir"/*; cd /; rmdir "$tmpdir"; } |
| 39 | |
| 40 | # set exit code in a portable way; see the autoconf manual for details: |
| 41 | (exit $rc); exit $rc |
| 42 | } |
| 43 | |
| 44 | ############################################################################### |
| 45 | # setupTmpDir() |
| 46 | # set up a temp directory and a trap to remove it |
| 47 | ############################################################################### |
| 48 | setupTmpDir() |
| 49 | { |
| 50 | $needsCleanup && return |
| 51 | |
| 52 | trap 'cleanup' 1 2 3 7 13 15 |
| 53 | needsCleanup=true |
| 54 | (umask 077; mkdir "$tmpdir") \ |
| 55 | || abort "could not create directory \`$tmpdir'" |
| 56 | } |
| 57 | ############################################################################### |
| 58 | # myexec(args) |
| 59 | # execute the command given as "args"; abort on error; redirect messages to |
| 60 | # log in "silent" mode; redirect stdin (</dev/null) in silent mode |
| 61 | ############################################################################### |
| 62 | myexec() |
| 63 | { |
| 64 | if $silent; then |
| 65 | echo "$0: running $@" |
| 66 | "$@" >$log 2>&1 </dev/null |
| 67 | rc=$? |
| 68 | else |
| 69 | "$@" |
| 70 | rc=$? |
| 71 | fi |
| 72 | if test $rc != 0; then |
| 73 | echo "$0: command $@ returned with code $rc" |
| 74 | cat <$log |
| 75 | (exit $rc) # set return value |
| 76 | cleanup |
| 77 | fi |
| 78 | } |
| 79 | |
| 80 | # some initializations |
| 81 | progname=pdflatexps |
| 82 | silent=true # default setting; change if you like |
| 83 | crop=false |
| 84 | needsCleanup=false |
| 85 | ps2pdf_opts="-dAutoRotatePages=/None" |
| 86 | tmpdir=${TMPDIR-${TEMP-${TMP-/tmp}}}/.$progname$$ |
| 87 | log=$tmpdir/log |
| 88 | version="1.0" |
| 89 | |
| 90 | ## the defaults |
| 91 | ENGINE1=latex |
| 92 | OPTIONS= |
| 93 | ENGINE2=pdflatex |
| 94 | DVIPS=dvips |
| 95 | PS2PDF=ps2pdf |
| 96 | FILE=$1 |
| 97 | |
| 98 | # look for optional things first |
| 99 | while |
| 100 | case $1 in |
| 101 | --help|-h) echo "$0 [-v|-q|--crop|--version|--Xps2pdf OPT] FILE" |
| 102 | echo "Process LaTeX document FILE using the pst-pdf package," |
| 103 | echo "including running latex, dvips, and ps2pdf as necessary." |
| 104 | echo "--Xps2pdf OPT passes OPT to ps2pdf." |
| 105 | echo " (-dAutoRotatePages=/None is always passed.)" |
| 106 | echo "--crop runs pdfcrop on ps2pdf output." |
| 107 | echo "--lualatex using the luatex engine for .dvi and .pdf" |
| 108 | echo "--xelatex using the xetex engine for .xdv and .pdf" |
| 109 | exit 0;; |
| 110 | --version) echo "$progname version $version"; exit 0;; |
| 111 | --lualatex) ENGINE1=lualatex; |
| 112 | OPTIONS="--output-format=dvi"; |
| 113 | ENGINE2=lualatex; FILE=$2;; |
| 114 | --xelatex) ENGINE1=xelatex; |
| 115 | OPTIONS="-no-pdf"; |
| 116 | ENGINE2=xelatex; |
| 117 | DVIPS=xdvipdfmx; FILE=$2;; |
| 118 | --crop|-c) crop=true;; |
| 119 | --Xps2pdf|-Xps2pdf) shift; ps2pdf_opts="$ps2pdf_opts $1";; |
| 120 | -q) silent=true;; |
| 121 | -v) silent=false;; |
| 122 | -*) echo "$progname: unknown option $1 ignored";; |
| 123 | *) break;; |
| 124 | esac |
| 125 | do |
| 126 | shift |
| 127 | done |
| 128 | |
| 129 | # remaining option: filename |
| 130 | if test -z "$FILE"; then |
| 131 | echo "$0: no file to process, try --help for more information." >&2 |
| 132 | exit 1 |
| 133 | fi |
| 134 | |
| 135 | # jobname von TeX: Verzeichnisse entfernen, evtl. vorhandene Extension |
| 136 | # entfernen. |
| 137 | job=`echo "x$1" | sed 's@x@@; s@.*/@@; s@\.[^.]*$@@'` |
| 138 | |
| 139 | setupTmpDir |
| 140 | myexec ${ENGINE1} ${OPTIONS} "\AtBeginDocument{\RequirePackage{pst-pdf}} \input{$FILE}" |
| 141 | |
| 142 | if test "$ENGINE1" = "latex" || test "$ENGINE1" = "lualatex"; then |
| 143 | myexec ${DVIPS} -o "$job-pics.ps" "$job.dvi" |
| 144 | else |
| 145 | myexec ${DVIPS} -o "$job-pics.pdf" "$job.xdv" |
| 146 | fi |
| 147 | |
| 148 | test -f "$job-pics.ps" && myexec ${PS2PDF} "$ps2pdf_opts" "$job-pics.ps" "$job-pics.pdf" |
| 149 | |
| 150 | if $crop; then |
| 151 | myexec pdfcrop "$job-pics.pdf" "$job-pics-crop.pdf" |
| 152 | mv "$job-pics-crop.pdf" "$job-pics.pdf" |
| 153 | fi |
| 154 | myexec ${ENGINE2} "\AtBeginDocument{\RequirePackage{pst-pdf}} \input{$FILE}" |
| 155 | true |
| 156 | cleanup |
| 157 |