#! /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

#
# 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\
/bd /Helvetica-Bold findfont def\
/od bd maxlength 1 add dict def\
bd {exch dup /FID ne {exch od 3 1 roll put} {pop pop} ifelse} forall\
od /FontName /Outline0 put od /PaintType 2 put od /StrokeWidth 0 put\
/Outline0 od definefont pop\
/DRAFT { gsave\
        initmatrix\
       /Outline0 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 0 moveto show\
       grestore } def\
/oldshow /showpage load def\
/oldcopy /copypage load def\
end\
/showpage { DRAFTDICT begin DRAFT oldshow end } def\
/copypage { DRAFTDICT begin DRAFT oldcopy end } def\
% End of draft prelude
: skip
}
EOF

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

