#!/usr/bin/perl -i~
# =============================================================================
#  $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);

$first = <>;				  # Get first line

if ($first =~ m/^%!PS-Adobe-/o) {	  # Does it have DSC comments?
    print $first;			  # yes, print the first line
    while (<>) {			  # loop until
	if (m/^%%EndComments/o) {	  #    end of comment header found
	    print;			  #  print end of comment line
	    &outPSCode;			  #  output ps code for draft
	    last;			  #  exit the while loop
	} else {
	    print;			  # echo lines until match found
	}	
    }
} else {				  # no DSC comments
    &outPSCode;				  #  put code to add draft first
    print $first;			  #  print first line of original file
}

while (<>) { print; }			  # output rest of input file

#
# See if draft is already present in the file.  If not, output it,
# if so, do not output a new copy
#

sub outPSCode {
    local($temp);
    $temp = <>;				  # Get next line
    if ($temp !~ m/^% DRAFT Added:/o) {	  # Is draft already defined?
	&DefineDRAFT;			  #  no, then define it
	print $temp;			  #  output line read in
    } else {
	print $temp;			  #  yes, don't define it
    }
}

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

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