#!/bin/csh -f
#
# Usage:
#    ps2psfrag [-dx <pts>] [-dx <pts>] files...
#
# Ps2psfrag runs GhostScript on one or more postscript files.  For each
# file argument (eg: "example.ps"), a file with the suffix "frag" is
# written (eg: "example.psfrag").  The output file contains LaTeX
# source that gives the text, abs bottom-left coordinates, text bounding
# box, and rotation for each piece of text that is displayed by the
# postscript show operators (show, ashow, kshow etc).
#
# The dx and dy arguments can be used to offset the x and y locations
# of each text fragment.  Some programs that write postscript files like
# to offset all text by a small fixed amount.  For example, matlab puts
# text from a text() command 2.28 pts (4 * 0.57) above a plotted line with
# the same coordinates.  Therefore, to convert a matlab postscript file you
# should use
#
#      ps2psfrag -dy -2.28 matlabplot.ps
#
# Similarly, idraw, with grid gravity turned on, puts text a small distance
# above the grid point.  For 12pt fonts the offset is 8.33 pts (it appears to
# be 0.7574 * (fontSize - 1) in general), so to convert an idraw postscript
# file you should use
#
#      ps2psfrag -dy -8.33 idrawfile.ps
#
# (Note that these position corrections are only relevant if you want the
# LaTeX characters to line up exactly with other geometry such as lines etc).
#
# The output file (<file>.psfrag) is normally included with the
# \epsfbox{} LaTeX marco.  You should include the psfrag and epsf
# document styles to make everything work.
#
# Revision History:
#   20-Sep-92   Craig Barratt	Released version 1.1.
#   17-Sep-92   Craig Barratt	Made PS2FRAG settable by user.
#    9-Jun-92   Craig Barratt	Changed egrep to grep for system V users.
#    1-Jun-92   Craig Barratt	Released version 1.0.
#   28-Feb-92   Craig Barratt	Initial version.
#
# Send comments and bugs to Craig Barratt (craig@isl.stanford.edu)
#

# Installation: PS2FRAG should point at wherever the ps2psfrag.ps
# file is installed (eg: set PS2FRAG = /usr/local/lib/ps/ps2psfrag.ps).
# The "set PS2FRAG" line below should be edited accordingly.
#
# [The user can override this default by setting the environment
#  variable PS2FRAG to point somewhere else; this was suggested
#  by Roque Donizete de Oliveira (oliveria@caen.engin.umich.edu).]
#
if ( ! $?PS2FRAG) then
    set PS2FRAG = /usr/local/lib/tex/ps/ps2psfrag.ps
endif
if ( ! -f $PS2FRAG || ! -r $PS2FRAG ) then
    echo "ps2psfrag: can't open $PS2FRAG; ps2psfrag is not installed correctly"
    exit 1
endif

onintr interrupt

set dx = 0
set dy = 0

while ( $#argv > 0 )
    switch ( $1 )
	case -dx:
	    if ( $#argv < 2 ) goto usage; endif
	    set dx = $2
	    # check for a valid signed real-valued number (no exponent)
	    echo $dx | egrep -s '^-?[0-9]*(\.[0-9]*)?$'
	    if ( $status ) goto usage; endif
	    shift
	    shift
	    breaksw

	case -dy:
	    if ( $#argv < 2 ) goto usage; endif
	    set dy = $2
	    # check for a valid signed real-valued number (no exponent)
	    echo $dy | egrep -s '^-?[0-9]*(\.[0-9]*)?$'
	    if ( $status ) goto usage; endif
	    shift
	    shift
	    breaksw

	default:
	    if ( ! -f $1 || ! -r $1 ) then
		echo "ps2psfrag: can't open $1"
		exit 1
	    endif
	    sed -n -e 1p $1 | egrep -s '^%\!'
	    if ( $status ) then
		echo "ps2psfrag: $1 doesn't look like a postscript file"
		exit 1
	    endif
	    rm -f $1frag
	    echo "($PS2FRAG) run $dx $dy ($1) FragConvert" \
			| gs -dNODISPLAY >& /tmp/gs-out$$
	    #
	    # egrep changed to grep at suggestion of Tim Carlson 6-9-92.
	    # (tim@sinc.oscs.montana.edu): system V unix egrep doesn't
	    # take -i option.
	    #
	    grep -s -i error /tmp/gs-out$$
	    if ( ! $status || ! -f $1frag || ! -r $1frag ) then
		echo "ps2psfrag: ghostscript failed on $1; the command"
		echo '    echo "'"($PS2FRAG) run $dx $dy ($1) FragConvert"'" \'
		echo '          | gs -dNODISPLAY'
		echo "produced the output:"
		sed -e 's/^/    /' /tmp/gs-out$$
		rm -f /tmp/gs-out$$
		exit 1
	    endif
	    rm -f /tmp/gs-out$$
	    shift
	    breaksw
    endsw
end

exit 0

interrupt:
    rm -f /tmp/gs-out$$
    exit 1

usage:
    echo 'usage: ps2psfrag [-dx <pts>] [-dx <pts>] files...'
    exit 1
