#!/bin/sh
# =============================================================================
#  $Id: draft,v 1.2 1993/07/01 18:37:32 grover Rel $
#
#  Put code into the Setup section to place DRAFT on ps pages
#
#  Features:
#  * Works on a single file (making a backup of the file)
#  * Detects if the draft commands were already added
#  * Works on file with and with out DSC comments
#  * Can also work as a filfer
#
#  - Kevin O. Grover, Cre: 19 Mar 1992, grover@isri.unlv.edu
# =============================================================================

# die "Usage: draft [file]\n" unless (@ARGV <= 1);

awk '
NR == 1		{
	if ($0 !~ /^%!PS-Adobe-/)  {
		print
		psflag = 1
		printps()
		next
	}
}

/^%%EndComments/	{
	print
	if (psflag == 0) printps()
	next
}

		{
	print
}

#
# Print PS code to define draft out.
# This is more portable than __END__ and <DATA>
#

function printps() {
print "% DRAFT Added: $Id: draft,v 1.2 1993/07/01 18:37:32 grover Rel $"
print "/ShowDict 2 dict def"
print "ShowDict begin"
print "/oldshowpage /showpage load def	    % Save old showpage command"
print "/draft {                             % Command to place DRAFT on page"
print "gsave"
print "/Helvetica-Bold findfont 220 scalefont setfont"
print ".95 setgray 130 70 moveto 50 rotate (DRAFT) show"
print "grestore "
print "} bind def"
print "end"
print "% Redefine showpage (using command defined in ShowDict)"
print "/showpage { ShowDict begin oldshowpage draft end } bind def"
print "ShowDict begin draft end     % So the first page has the text"
}' $1
