:
##########################################################################
# Shellscript:	seiten - determine number of pages needed for text file
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	1994-03-09
# Category   :	Text Utilities
# SCCS-Id.   :	@(#) seiten	1.5 04/02/18
##########################################################################
# Description
#
# Changes
# 25.08.94 stv	try to handle form feed characters in the text (0.2 beta)
# 22.03.00 stv	correct handling of file names containing whitespace (1.2)
##########################################################################

PN=`basename "$0"`
VER='1.5'

Usage () {
    echo "$PN - number of pages needed for text file, Version $VER (stv '94)
usage: $PN [-l page_length] [-n pages] [-Hh] [file ...]
   -l:  number of lines per page (default is $pl)
   -n:  number of pages per sheet (default is 1)
   -H:  print header line
   -h:  print this help

If no file is specified, $PN reads from the standard input." >&2
    exit 1
}

# CountPages (filename)
CountPages () {
    [ $# -gt 0 ] && file="$1"
    set -- ` \
    awk '
	BEGIN {
	    pages=0; lineno=0; linecnt=0
	}
	{
	    linecnt++; lineno++
	    if ( lineno>'$pl' ) {
		pages++; lineno=1
	    }

	    if ( $0 ~ // ) {		# special handling: form feed
		for ( i=1; i<=length; i++ )
		    if ( substr ($0, i, 1) == "" )
			pages++;
		lineno=1
	    }
	}
	END {
	    pages++			# count first page
	    print pages "	" linecnt
	}
    ' ${file+"$file"}
    `
    # $1=pages, $2=lines
    if [ "$KornShell" = y ]
    then
	((nlines=$nlines+$2))
	((npages=$npages+$1))
    else
	nlines=`expr $nlines + $2`
	npages=`expr $npages + $1`
    fi
    echo "$1	$2	$file"
}


# If this script is run with a Korn Shell, use fast integer operations
# instead of "expr"
if ([ 1+1 -eq 2 ])
then KornShell=y
else KornShell=n
fi

PageCnt=1				# Pages per sheet
pl=66					# Page length, lines per page
Header=n				# Parameter -H
while getopts hHl:n: opt
do
    case "$opt" in
    	l)	pl=$OPTARG;;
	n)	PageCnt=$OPTARG;;
	H)	Header=y;;
	-)	break;;
	*)	Usage;;
    esac
done
shift `expr $OPTIND - 1`

pl=`expr $pl \* $PageCnt`		# Lines per page

nlines=0
npages=0

[ "$Header" = y ] && echo "PAGES	LINES	FILE"
if [ $# -gt 0 ]
then
    for i
    do
	[ -r "$i" ] || continue
	CountPages "$i"
    done
    [ $# -gt 1 ] && echo "$npages	$nlines	total"
else
    CountPages			# read from stdin
fi
exit 0
