#! /bin/csh -f
#
# draw2ps --- Translate postscript file generated by MacDrawII to EPS file
#
# usage: draw2ps inputfile > outputfile
#
# created by Katayama (katayama@nacsis.ac.jp), Kurashima (kura@nacsis.ac.jp)
# ver 1.00    17 Dec. 1990
# ver 1.01    14 Mar. 1991
# ver 1.02    12 July 1991 updated by kura@nacsis.ac.jp
#

set LIBDIR = /usr/local/lib/gs/misc
set BINDIR = /usr/local/bin
set prep = $LIBDIR/LaserPrep.ps
set bbfig = $LIBDIR/bb.ps
set gs = $BINDIR/gs

################
set path = (/bin /usr/bin /usr/ucb $BINDIR)

set temp = /usr/tmp/draw2ps.$$
set ps = $temp.ps

set display = -DNODISPLAY
if ($#argv > 0) then
	if ("$1" == "-d") then
		if ($?DISPLAY) then
			set display = ""
		endif
		shift
	endif
endif

if ($#argv != 1) then
	echo 'usage: draw2ps [-d] inputfile > outputfile'
	exit 1
endif

tr '\015' '\012' < $1 | \
sed -e '/^%%Page/d' -e '/^%%EOF/d' -e '/^%%Creat/d' -e '/^%%Title/d' -e 's/^%{/% /' > $temp
set incproc = `grep ^%%IncludeProcSet $temp`
set procset = `grep ^%%BeginProcSet $temp`
if (("$incproc" != "") && ("$procset" == "")) then
	cat $prep $temp > $ps
else
	set ps = $temp
endif
cat $bbfig $ps > $temp.bbfig
if($display != "") then
	$gs $display $temp.bbfig > $temp.gs < /dev/null
else
	echo "Type 'quit' to continue" > `tty`
	$gs $display $temp.bbfig > $temp.gs
endif
set bound = `grep '^%%Bounding' $temp.gs | head -1`
if ("$bound" != "") then
	sed -e "s/^%%Bounding.*/$bound/" $ps
else
	cat $ps
	echo '%'
	echo '% ERROR OCCURED IN GHOSTSCRIPT (draw2ps)'
	echo '%'
	sed -e '/^Loading font file.*done\./d' -e '/^>>showpage, press <return> to continue<</d' -e '/^$/d' -e 's/^/% /' $temp.gs
endif
rm -f $temp $temp.*
exit 0
