1#!/bin/sh
2
3# This definition is changed on install to match the
4# executable name set in the makefile
5GS_EXECUTABLE=gs
6gs="`dirname \"$0\"`/$GS_EXECUTABLE"
7if test ! -x "$gs"; then
8	gs="$GS_EXECUTABLE"
9fi
10GS_EXECUTABLE="$gs"
11
12# try to create a temporary file securely
13if test -z "$TMPDIR"; then
14	TMPDIR=/tmp
15fi
16if which mktemp >/dev/null 2>/dev/null; then
17	tmpfile="`mktemp $TMPDIR/ps2epsi.XXXXXX`"
18else
19	tmpdir=$TMPDIR/ps2epsi.$$
20	(umask 077 && mkdir "$tmpdir")
21	if test ! -d "$tmpdir"; then
22		echo "failed: could not create temporary file"
23		exit 1
24	fi
25	tmpfile="$tmpdir"/ps2epsi$$
26fi
27trap "rm -rf \"$tmpfile\"" 0 1 2 3 7 13 15
28
29export outfile
30
31if [ $# -lt 1 -o $# -gt 2 ]; then
32	echo "Usage: `basename \"$0\"` file.ps [file.epsi]" 1>&2
33	exit 1
34fi
35
36infile=$1;
37
38if [ $# -eq 1 ]
39then
40	case "${infile}" in
41	  *.ps)		base=`basename "${infile}" .ps` ;;
42	  *.cps)	base=`basename "${infile}" .cps` ;;
43	  *.eps)	base=`basename "${infile}" .eps` ;;
44	  *.epsf)	base=`basename "${infile}" .epsf` ;;
45	  *)		base=`basename "${infile}"` ;;
46	esac
47	outfile=${base}.epsi
48else
49	outfile=$2
50fi
51
52"$GS_EXECUTABLE" -q -dBATCH -dNOPAUSE -P- -sDEVICE=bbox -sOutputFile=/dev/null "${infile}" 2>${outfile}
53
54ls -l "${infile}" |
55awk 'F==1	{
56		cd="%%CreationDate: " $6 " " $7 " " $8;
57		t="%%Title: " $9;
58		f="%%For:" U " " $3;
59		c="%%Creator: Ghostscript ps2epsi from " $9;
60		next;
61		}
62	/^%!/	{next;}
63	/^%%Title:/	{t=$0; next;}
64	/^%%Creator:/	{c=$0; next;}
65	/^%%CreationDate:/	{cd=$0; next;}
66	/^%%For:/	{f=$0; next;}
67	!/^%/	{
68		print "/ps2edict 30 dict def";
69		print "ps2edict begin";
70		print "/epsititle (" t "\\n) def";
71		print "/epsicreator (" c "\\n) def";
72		print "/epsicrdt (" cd "\\n) def";
73		print "/epsifor (" f "\\n) def";
74		exit(0);
75		}
76	' U="$USERNAME$LOGNAME"  F=1 - F=2 "${infile}" >"$tmpfile"
77
78ls -l "${outfile}" |
79awk 'F==1	{
80		b="%%BoundingBox: 0 0 0 0\\n";
81		}
82		/^%%BoundingBox:/	{b=$0; next;}
83		/^%%HiResBoundingBox:/	{
84		hb=$0;
85		print "ps2edict where {pop} {/ps2edict 30 dict def} ifelse";
86		print "ps2edict begin";
87		print "/BBoxString (" b "\\n) def";
88		print "/HiresBBoxString (" hb "\\n) def";
89		print "end";
90		exit(0);
91		}
92	' F=1 - F=2 "${outfile}" >>"$tmpfile"
93
94"$GS_EXECUTABLE" -q -dNOPAUSE -P- -r72 -sDEVICE=bit -sOutputFile=/dev/null "$tmpfile" ps2epsi.ps "$tmpfile" <"${infile}" 1>&2
95rm -f "$tmpfile"
96rm -rf "$tmpdir"
97
98(
99cat << BEGINEPS
100save countdictstack mark newpath /showpage {} def /setpagedevice /pop load def
101%%EndProlog
102%%Page 1 1
103BEGINEPS
104
105cat "${infile}" |
106LC_ALL=C \
107sed -e '/^%%BeginPreview:/,/^%%EndPreview[^!-\~]*$/d' -e '/^%!PS-Adobe/d'\
108	-e '/^%%[A-Za-z][A-Za-z]*[^!-\~]*$/d' -e '/^%%[A-Za-z][A-Za-z]*: /d'
109
110cat << ENDEPS
111%%Trailer
112cleartomark countdictstack exch sub { end } repeat restore
113%%EOF
114ENDEPS
115
116) >> "${outfile}"
117
118exit 0
119