#! /bin/sh
# Put the word "DRAFT" (or a specified word) on each page of a postscript
# document.
# Usage:
#      psdraft [-s draftstring] < file ...
# Author: Spencer W. Thomas #        Computer Science Dept.
#        University of Utah #        thomas@cs.utah.edu
# Modified by LA Carr, Electronics & Computer Science,
# University of Southampton, Southampton S09 5NH (lac@uk.ac.soton.cm)
#
# Insert header after first line that does not begin %% or %!
#
trap "rm -f /tmp/psd$$.*" 0 1 2 15

if [ "x$1" = "x-s" ] ; then
       draftstring=$2
       shift
       shift
else
       draftstring=DRAFT
fi

if [ "x$*" = "x" ] ; then
       echo "Usage: psdraft [-s draftstring] files ..."
fi

# Create sed script file

sed "s/(DRAFT)/($draftstring)/" <<'EOF' >/tmp/psd$$.sed
1,/^[^%]/{
s/^%/%/
s/^$//
t skip
i\
% Prelude to show a draft string on every page.\
(DRAFT)\
/DRAFTDICT 10 dict def\
DRAFTDICT begin\
/DRAFTSTRING exch def\
/DRAFT { gsave\
        initmatrix\
       /Helvetica-Bold findfont setfont\
       DRAFTSTRING dup stringwidth pop 8.875 exch div dup 72 mul dup scale\
       52.3 rotate 2.5 exch div -.35 translate\
       0.95 setgray\
       0 0 moveto show\
       grestore } def\
/oldshow /showpage load def\
/oldcopy /copypage load def\
/oldinitgraphics /initgraphics load def\
end\
/showpage { DRAFTDICT begin oldshow DRAFT end } def\
/copypage { DRAFTDICT begin oldcopy DRAFT end } def\
/initgraphics { DRAFTDICT begin oldinitgraphics DRAFT end } def\
DRAFTDICT begin DRAFT end\
% End of draft prelude
: skip
}
EOF

cat $* | sed -f /tmp/psd$$.sed
