#!/bin/sh
#
# $Id: pspages,v 1.1 1993/08/25 20:20:41 grover Exp $
#
# count the number of pages output by a PostScript file
# Requires GhostScript 1.5 or greater
#
# Syntax:  psages [psfile [...]]
#
# - Kevin O. Grover, grover@isri.unlv.edu
# Cre: Wed 08-Aug-1993
#
# pspages is an sh script to count the number of pages in a postscript
# file.  It counts all instances of an output page (included copypage
# and/or showpage).  It also works on files that redefine copypage
# and/or showpage.  It requires ghostscript to run.  It works by
# redefining the copypage and showpage commands to keep track of the
# number of pages output, and then, at the end of the ps file, it
# outputs the number of pages.
# 
# If you wish to use the script for accounting -- i.e. tracking the
# number of pages printed by people on a printer that does not support
# internal page counts, the name of the dictionary PageCountDict, and
# the name of the variable "NumPages" should be changed to something
# more criptic, and normal users should not be able to view this
# contents of this file.  This is NOT required for the program to run,
# it is simply a security measure.  If a person knew postscript, and knew
# that this program was being used to track output pages, they could
# write their ps code to zero the NumPages count after each copypage and
# showpage.
# 
# Note:  if the file passed in is not a valid PS file, or there is an
# error in the PS file, then that error is printed (from ghostscript
# directly).
# 
# Please send bug reports and enchancements to me.
# 
# - Kevin Grover, grover@isri.unlv.edu
#

(
cat <<_END_PROLOG
/PageCountDict 1 dict def
PageCountDict begin
/NumPages 0 def
end
/showpage {
  erasepage initgraphics
  PageCountDict begin
    /NumPages NumPages 1 add def
  end
} bind def
/copypage {
  erasepage initgraphics
  PageCountDict begin
    /NumPages NumPages 1 add def
  end
} bind def
_END_PROLOG
cat $1
cat <<_END_POSTLOG
PageCountDict begin
NumPages 6 string cvs 
end
print (\n) print
_END_POSTLOG
) | gs -q -dNODISPLAY -r1 -
