#!/bin/sh
#set -x
#
# Title: pstoedit, Version 2.3.3, November 1995
#
# Script to convert PostScript(TM) files to tgif or FrameMaker(TM) input for editing
#
# Copyright (C) 1993,1994,1995 Wolfgang Glunz, Wolfgang.Glunz@zfe.siemens.de
#
#
#   This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# Changes:
# Version 2.3.3
# added escaping of () for pure PostScript backend
# added a debug backend
# added the -dis option
# added color support 
# pstoedit can now be run as filter reading from stdin and writing to stdout
# Version 2.3.2
# no change in this script
# Version 2.3.1
# allow also -f xfig for xfig backend
# Version 2.3
# xfig backend by Ian MacPhedran <Ian_MacPhedran@engr.USask.CA>
# Version 2.2
# no change in this script
# Version 2.1
# better handling of setgray and setlinewidth
# Version 2.0
# support for MIF and TGIF format
# Version 1.1
# changes to work with bash under LINUX 
#
# Change these lines according to the local environment
#
# Where the pstoedit.pro and makeedit is found
LIBDIR=/usr/lib/pstoedit
BINDIR=/usr/bin
# Where GhostScript is found
GHOSTSCRIPT=/usr/bin/gs
#GHOSTSCRIPT=/usr/local/bin/gs

# You shouldn't have to change anything below this line

USAGE="pstoedit [-dt] [-dis] [-m magnification] [-f format] psfiles"
REDEF="/textastext true def"
DOESCAPE="/escapetext false def"
MAG=1
BACKEND=tgif
# default is not to open a display
DISPLAYOPTION="-dNODISPLAY"
SUFFIX=obj
FILES=""
while  [ x"$*" != x ]
do
	case $1 in
	-dt 	)
		REDEF="/textastext false def"
		shift
		;;
	-m	)
		shift
		MAG=$1
		shift
		;;
	-f	)
		shift
		BACKEND=$1
		shift
		;;
	-dis	)
		DISPLAYOPTION=""
		shift
		;;
	*	)
		FILES="$FILES $1"
		shift
		;;
	esac
done

case $BACKEND in
t*	)
	SUFFIX=obj
	;;
m*	)
	SUFFIX=mif
	;;
ps*	)
	SUFFIX=ps
	DOESCAPE="/escapetext true def"
	;;
pd*	)
	SUFFIX=pdf
	;;
cgm*	)
	SUFFIX=cgmc
	;;
f*|xf*	)
	SUFFIX=fig
	BACKEND=fig
	;;
debug	)
	SUFFIX=ps
	;;
esac

TMPFILE1=/tmp/pstoedit.1.$$
TMPFILE2=/tmp/pstoedit.2.$$
TMPFILE3=/tmp/pstoedit.3.$$.ps
if [ "x$FILES" = "x" ]; then
# no files given as arguments, so use stdin and write to stdout
	FILES="$TMPFILE3"
# pipe stdin into a file and use that file later on as input
	cat - > $FILES
	PIPE=1
else
	PIPE=0
fi

trap 'rm -f $TMPFILE1 $TMPFILE2 $TMPFILE3 ' 2 3 15

for FILE in $FILES 
do
	FNAME=`basename $FILE`
	/bin/cat - $LIBDIR/pstoedit.pro << EOF > $TMPFILE1;
%!
$REDEF
$DOESCAPE
EOF
$GHOSTSCRIPT -dWRITESYSTEMDICT $DISPLAYOPTION -q $TMPFILE1 $FILE quit.ps < /dev/null > $TMPFILE2
# unfortunately gs does not return and error code, so check output file for error messages
if { egrep '^Error' $TMPFILE2; } then
	echo "Ghostscript seems to have failed on file $FILE"
	echo "Look into $TMPFILE2 for error messages from GhostScript"
	rm -f $TMPFILE1
	exit 1;
else
	case $BACKEND in
	ps* | debug	)
		# PostScript does not need any further processesing with makeedit
		;;
	*	)
		$BINDIR/makeedit -m $MAG -f $BACKEND -o $TMPFILE1 < $TMPFILE2
# do some postprocessing if needed
		case $BACKEND in
		t*	)
				# insert page number
				PAGENR=`egrep "^%pstoeditpagenumber: PAGENR" $TMPFILE1 | awk '{ print $3 }'` ;
				sed -e "s/PSTOTGIFPAGENR/$PAGENR/" $TMPFILE1 > $TMPFILE2 ;
			;;
		m*	)
				/bin/cat << EOF > $TMPFILE2
<MIFFile 4.00>
<Units Upt >
<ColorCatalog
EOF
		 		grep pstoeditcolorentry $TMPFILE1 |\
				awk '-F#' '{ print $2 }'     |\
				sed 's/pstoeditcolorentry//' | sort -u >> $TMPFILE2
				echo '>' >> $TMPFILE2
				cat $TMPFILE1 >> $TMPFILE2
			;;
		ps*	)
			# PostScript already handled in enclosing case
			;;
		pd*	)
			# no further processing needed just use output from makeedit
			mv $TMPFILE1 $TMPFILE2
			;;
		cgm*	)
			# no further processing needed just use output from makeedit
			mv $TMPFILE1 $TMPFILE2
			;;
		f*|xf*	)
			# must move color entries to top
			head -5 $TMPFILE1 > $TMPFILE2
			grep '^#color' $TMPFILE1 | sed 's/^#color //' >> $TMPFILE2
			ed $TMPFILE1 << EDINPUT 2> /dev/null
1,5d
wq
EDINPUT
			grep -v '^#color' $TMPFILE1 >> $TMPFILE2
			;;
		esac
		;;
	esac
	# output is now in $TMPFILE2
	if [ $PIPE = 1 ]; then
		cat $TMPFILE2
	else
		mv $TMPFILE2 $FNAME.$SUFFIX
		echo Output file written to $FNAME.$SUFFIX
	fi
fi
rm -f $TMPFILE1 $TMPFILE2 $TMPFILE3 ;

done
