#!/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 text that is displayed with the show operator.
#
# 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.
#
# Craig Barratt, Fri Feb 28 12:12:08 PST 1992
# craig@isl.stanford.edu
#

# Installation: this should point at wherever the ps2psfrag.ps script
# is intalled.
set PS2FRAG = /home/craig/tex/psfrag/ps2psfrag.ps
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 -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
