summaryrefslogtreecommitdiff
path: root/graphics/epix/elaps.in
blob: 6e8041f3d3c96ddb29f1db2b9eb3b9b08e0749bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#! /bin/bash
# 
# elaps: Convert an epix file to encapsulated Postscript, i.e. epix2eps
# 
# Options: --help for usage, --version for version and license
#
# Compiler flags may be specified on the command line or in the config
# file ~/.epixrc. The script attempts to deal intelligently with missing
# or multiple extensions. If the input file has a recognized extension,
# there's no issue. If only the root name <infile> is given, the script
# searches for completions in the order of preference dictated by the
# variable EPIX_EXTENSIONS. A warning is issued for multiple matches.
#
# Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
# Andrew D. Hwang   <For address, do "elaps -V">
# 

. "@pkglibdir@/epix-lib.sh"

EPIX_CONFIG_FILE="$HOME/.epixrc"
# Search these extensions in order of preference
ELAPS_EXTENSIONS="eepic xp flx cc c C cpp" # or NULL
ELAPS_DOT_EXT=".${ELAPS_EXTENSIONS// /|.}" # for usage message

# default options
ELAPS_PKGS="latexsym,amsmath,rotating"
declare ELAPS_PSTCOL
ELAPS_SIZE=12 # figure compiled in [12pt]{article}

ELAPS_PDF="no"    # Create PDF file?
ELAPS_PS="no"     # Create PS file instead of EPS?

declare -a EPIX_OPTS
declare -a DVIPS_OPTS

LATEX_BIN=latex  # elaps only uses [huge]latex, not pdflatex or pslatex
LATEX_INTERACT="scrollmode"

# auxiliary programs
PS2EPS="ps2epsi"

# internal filenames
PID=$$
ELAPS_TEMP_EEPIC="$PROG-$PID".eepic
ELAPS_TEMP_LATEX="$PROG-$PID".tex
ELAPS_TEMP_DVI="$PROG-$PID".dvi
ELAPS_TEMP_PS="$PROG-$PID".ps
ELAPS_TEMP_EPSI="$PROG-$PID".epsi

# globals to pass tokens
ELAPS_USER_OUT="no" # user specified output file?

declare -a ELAPS_FILE_LIST
declare -a TMP_CMD

declare -a TEMPFILES
trap '[ -n "${TEMPFILES[*]}" ] && rm -f "${TEMPFILES[@]}"' 0 1 2 3 7 10 13 15

TEMPFILES=("$ELAPS_TEMP_EEPIC" "$ELAPS_TEMP_LATEX" "$ELAPS_TEMP_DVI" \
    "$ELAPS_TEMP_PS" "$ELAPS_TEMP_EPSI" )

function elaps_help()
{
cat <<"HELP"
Options:
    -h, --help
      Show this message and exit

    -V, --version, --gpl
      Show version and license

    -v, --verbose
      Show success/failure messages

    -H, --huge
      Use hugelatex (if available)

    -i, --interactive
      Run LaTeX interactively (implies verbose)

    -n, --no-config
      Ignore $(HOME)/.dvipsrc

    -o, --output
      Specify output file (ONLY for use with a single input file)

    --pst
      Write eepic file using PSTricks macros

    --tikz
      Write eepic file using tikz macros

    --eepic
      Write eepic file using eepic macros

    -p, --pkg <LaTeX package>
      Use an additional LaTeX package in the temporary LaTeX file, e.g. 
      "-p floatflt". Multiple packages are specified one per "-p" flag.

    -pdf, --pdf
      Create PDF file instead of EPS

    -ps, --ps
      Create standalone PostScript file (overrides --pdf)

    --pstcol
      Use (obsolete) pstcol package instead of pstricks

    -P<printer>
      dvips printer options, e.g. "-Pamz", may be given on the command 
      line. They are also read from ~/.dvipsrc if this file exists.

    --size
      Set default font size: 10, 11, or 12 (default)

    Other options are passed to epix (q.v.).

HELP
ePiX_bugreport

} # End of elaps_help

# Generate US letter-friendly LaTeX file
function elaps_make_latex ()
{
    ALL_LATEX_PKGS="$ELAPS_PKGS"

# Get LATEX_PKGS from input file
    ePiX_usepackages $2
    if [ ! -z "$LATEX_PKGS" ]; then # should always be true...
	ALL_LATEX_PKGS="$ALL_LATEX_PKGS,$LATEX_PKGS"
    else # compatibility fallback
	ALL_LATEX_PKGS="$ALL_LATEX_PKGS,epic,eepic,pstricks"
    fi

# Clear global
    unset LATEX_PKGS

# No effect unless file requires pstricks
    if [ ! -z "$ELAPS_PSTCOL" ]; then
	ALL_LATEX_PKGS="${ALL_LATEX_PKGS/pstricks/$ELAPS_PSTCOL/}"
    fi

cat <<LaTeX >> $1
\documentclass[${ELAPS_SIZE}pt]{article}
\usepackage{$ALL_LATEX_PKGS}
\setlength{\textwidth}{7.5in}
\setlength{\textheight}{9.5in}
\setlength{\oddsidemargin}{-0.5in}
\setlength{\topmargin}{0in}
\pagestyle{empty}

\begin{document}
\vfil
\begin{center}
\input{$2}
\end{center}
\vfil
\end{document}

LaTeX
} # End of elaps_make_latex


function elaps_parse_options ()
{
    while [ "$1" != "${1#-}" ]; do 
        case "$1" in

        -h|--help)
	    ePiX_usage elaps $ELAPS_DOT_EXT
	    elaps_help
	    exit 0
	    ;;

        -H|--huge)
            if [ $(which hugelatex 2> /dev/null) ]; then
                LATEX_BIN=hugelatex
            else
                ePiX_warn "  hugelatex not available, ignoring \"$1\" option"
            fi
            shift
            ;;

        -i|--interactive)
            EPIX_STDOUT="$SYSTEM_STDOUT"
            LATEX_INTERACT="errorstopmode"
            shift
            ;;

        -n|--no-config) # do not use ~/.dvipsrc
            export DVIPSRC="$SYSTEM_DEVNULL"
            shift
            ;;

        -o|--output)
            if [ "$2" = "${2%%.eps}.eps" ]; then
                EPIX_OUTROOT="${2%%.eps}"

            elif [ "$2" = "${2%%.pdf}.pdf" ]; then
                ELAPS_PDF="yes"
                EPIX_OUTROOT="${2%%.pdf}"

            else
                EPIX_OUTROOT="$2"
            fi
            ELAPS_USER_OUT="yes"
            shift 2
            ;;

	--pst)
	    EPIX_OPTS=("${EPIX_OPTS[@]}" "-DEPIX_FMT_PSTRICKS")
	    shift; continue
	    ;;

	--tikz)
	    EPIX_OPTS=("${EPIX_OPTS[@]}" "-DEPIX_FMT_TIKZ")
	    shift; continue
	    ;;

	--eepic)
	    EPIX_OPTS=("${EPIX_OPTS[@]}" "-DEPIX_FMT_EEPIC")
	    shift; continue
	    ;;

        -P)
            DVIPS_OPTS=("${DVIPS_OPTS[@]}" "-P$2")
            shift 2
            ;;

        -P*)
            DVIPS_OPTS=("${DVIPS_OPTS[@]}" "$1")
            shift
            ;;

        -pdf|--pdf)
            ELAPS_PDF="yes"
            shift
            ;;

        -ps|--ps)
            ELAPS_PS="yes"
            ELAPS_PDF="no" # redundant; enforced by logic later
            shift
            ;;

        --pstcol)
            ELAPS_PSTCOL="pstcol,xcolor"
            shift
            ;;

        -p|--pkg)
            if [ "$2" != "${2#-}" ]; then
                ePiX_warn "\"$1\" cannot be followed by \"$2\"; skipping"
                shift 1
            else
                ELAPS_PKGS="$ELAPS_PKGS,$2"
                shift 2
            fi
            ;;

        -p*)
            ELAPS_PKGS="$ELAPS_PKGS,${1#-p}"
            shift
            ;;

        # Deliberately undocumented; run silently, no log file
        -q|--quiet)
            EPIX_STDOUT="$SYSTEM_DEVNULL"
            EPIX_STDERR="$SYSTEM_DEVNULL"
            LATEX_INTERACT="batchmode"
            shift; continue
            ;;

        --size)
            ELAPS_SIZE="$2"
            shift 2
            ;;

        -V|--version|--gpl)
            ePiX_version elaps
            ePiX_license
            exit 0
	    ;;

        -v|--verbose)
            if [ -z "$2" ]; then 
                echo "Please use -V for version"
                exit 0
            fi
            EPIX_STDOUT="$SYSTEM_STDOUT"
            shift
            ;;

        -vv)
            echo "Discontinued option -vv; please use -v for verbose output"
            exit 0
            ;;


        # pass other options to epix
        -i*|-u|-x|-I|-L|-l|-W)
	    EPIX_OPTS=("${EPIX_OPTS[@]}" "$1" "$2")
            shift 2
            ;;

        -b) 
            ePiX_warn "Skipping option \"$1 $2\""
            shift 2
            ;;

        *)
            EPIX_OPTS=("${EPIX_OPTS[@]}" "$1")
            shift
            ;;
        esac
    done

    # Assume remaining parameters are input files
    INFILE_COUNT=$#

    if [ "$ELAPS_USER_OUT" = "yes" -a $INFILE_COUNT -gt 1 ]; then
        shift $(($INFILE_COUNT - 1))
        ePiX_warn "Output file specified with multiple input files. Skipping all but \"$*\""
    fi

    ELAPS_FILE_LIST=("$@")
} # end of elaps_parse_options()


function elaps_process_files() 
{
    if [ -z "$1" ]; then
        ePiX_die "No input file specified"
    fi

    # file counts
    local processed=0
    local success=0
    local failure=0

    for EPIX_INFILE in "${ELAPS_FILE_LIST[@]}"; do

        # sets EPIX_INROOT, EPIX_SUFFIX, EPIX_NOTFOUND, touches EPIX_LOGFILE
        epix_parse_filename "$EPIX_INFILE" "$ELAPS_EXTENSIONS"
        let processed=processed+1

        if [ "$EPIX_NOTFOUND" = "yes" ]; then
            let failure=failure+1
            continue
        fi

        : ${EPIX_OUTROOT:=$EPIX_INROOT}
        EPIX_OUTFILE="$EPIX_OUTROOT".eepic

        # Create eepic file if necessary
        if [ "$EPIX_SUFFIX" != "eepic" ]; then
	    if [ -f "$EPIX_OUTFILE" ]; then # back up existing file
		mv -f "$EPIX_OUTFILE"  "${EPIX_OUTFILE}~"
	    fi

            TMP_CMD=(@bindir@/epix "${EPIX_OPTS[@]}" -o "$EPIX_OUTFILE" "$EPIX_INFILE")

            ePiX_command "${TMP_CMD[@]}"
	    echo "---" | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"

	    if [ "$EPIX_INFILE" -nt "$EPIX_OUTFILE" ]; then
                ePiX_warn "\"${TMP_CMD[*]}\" failed"
		unset EPIX_OUTFILE
		unset EPIX_OUTROOT
                let failure=failure+1
                continue

	    else
		ln -s "$EPIX_OUTFILE" "$ELAPS_TEMP_EEPIC"
	    fi

        else # we were passed an eepic file name
            ln -s "$EPIX_INFILE" "$ELAPS_TEMP_EEPIC"
        fi

        elaps_make_latex "$ELAPS_TEMP_LATEX" "$ELAPS_TEMP_EEPIC"

        # Create $ELAPS_TEMP_PS
        TMP_CMD=($LATEX_BIN -interaction=$LATEX_INTERACT "$ELAPS_TEMP_LATEX")

        ePiX_command "${TMP_CMD[@]}"

        if [ "$ELAPS_TEMP_LATEX" -nt "$ELAPS_TEMP_DVI" ]; then
	    # Report informative (but fake) command line
	    ePiX_warn "\"$LATEX_BIN -interaction=$LATEX_INTERACT ${EPIX_INROOT}.tex\" failed"
	    rm -f "$ELAPS_TEMP_LATEX" "$ELAPS_TEMP_EEPIC" # clean temps
	    unset EPIX_OUTFILE
	    unset EPIX_OUTROOT
            let failure=failure+1
            continue
	else
	    echo "---" | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
        fi

        TMP_CMD=(dvips "${DVIPS_OPTS[@]}" -f -o "$ELAPS_TEMP_PS" "$ELAPS_TEMP_DVI")

        ePiX_command "${TMP_CMD[@]}"

        if [ "$ELAPS_TEMP_DVI" -nt "$ELAPS_TEMP_PS" ]; then
	    # Report informative (but fake) command line
	    ePiX_warn "\"dvips ${DVIPS_OPTS[@]} -f -o ${EPIX_INROOT}.ps ${EPIX_INROOT}.dvi\" failed"

	    unset EPIX_OUTFILE
	    unset EPIX_OUTROOT
            let failure=failure+1
            continue
	else
	    echo "---" | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
        fi

        if [ "$ELAPS_PS" = "yes" ]; then
            # fix title; can't conveniently put sed command into ePiX_command
            echo -n "sed \"s/$PROG-$PID/$EPIX_INROOT/g\" \"$ELAPS_TEMP_PS\"" \
                | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
            echo " > \"$EPIX_OUTROOT.ps\"" \
                | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"

            sed "s/$PROG-$PID/$EPIX_INROOT/g" "$ELAPS_TEMP_PS" \
                > "$EPIX_OUTROOT.ps"

        else
            # Create eps file,
            TMP_CMD=("$PS2EPS" "$ELAPS_TEMP_PS")
            ePiX_command "${TMP_CMD[@]}"

            # fix title,
            echo -n "sed \"s/$PROG-$PID/$EPIX_INROOT/g\" \"$ELAPS_TEMP_EPSI\""\
                | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
            echo " > \"$EPIX_OUTROOT.eps\"" \
                | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"

            sed "s/$PROG-$PID/$EPIX_INROOT/g" \
                "$ELAPS_TEMP_EPSI" > "$EPIX_OUTROOT.eps" &&
            rm -f "$ELAPS_TEMP_EPSI"

            # create pdf if requested,
            if [ "$ELAPS_PDF" = "yes" ]; then
                echo -n "grep -v \"^%%Orientation:\" \"$EPIX_OUTROOT.eps\"" \
                    | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
                echo " | epstopdf --filter --outfile=\"$EPIX_OUTROOT.pdf\"" \
                    | tee -a "$EPIX_LOGFILE" > "$EPIX_STDOUT"
                # avoid ghostscript rotation bug
                grep -v "^%%Orientation:" "$EPIX_OUTROOT.eps" \
                    | epstopdf --filter --outfile="$EPIX_OUTROOT.pdf"
                rm -f "$EPIX_OUTROOT.eps" # clean up eps
            fi
        fi

        let success=success+1
	unset EPIX_OUTFILE
	unset EPIX_OUTROOT

        # and clean up
        rm -f "$ELAPS_TEMP_LATEX" "$ELAPS_TEMP_DVI" "$ELAPS_TEMP_PS" \
            "$ELAPS_TEMP_EEPIC" "$PROG-$PID.aux" "$PROG-$PID.log"

        echo -e "\nTranscript written on $EPIX_LOGFILE" > "$EPIX_STDOUT"

    done # for EPIX_INFILE in $INFILE_LIST

    if [ $processed -gt 1 ]; then
        echo "$processed files processed: $success successfully, $failure failed" > "$EPIX_STDERR"
    fi
} # elaps_process_files()


## Script proper starts here ##

if [ $# -eq 0 ]; then 
    ePiX_usage elaps $ELAPS_DOT_EXT
    elaps_help
    exit 0
fi

elaps_parse_options "$@"

elaps_process_files "${ELAPS_FILE_LIST[@]}"

exit 0