#! /bin/sh
#
#	@(#)nnscript		1.0 (sun!waynet) 11/29/88
#
PATHNAME=oculus:/HOME/waynet/bin/nnscript
#
# Author: Wayne Thompson
#
# Description:
#     This is a wrapper for enscript(1).
#         1. It prints an expanded Gaudy header. The header has
#             a. full pathname
#             b. file size in bytes
#             c. user name in internet format
#             d. '(folded)' if so
#         2. It prints using the least number of sheets (within reason)
#             by fooling with the number of columns, rotation, and font
#             size.
#         3. It prevents character lossage due to line length by
#             fooling with the rotation, font size, and by folding as
#             a last resort.
#
#     enscript - Line & Page lengths for various options w/Gaudy
#
#				font = Courier
#     lnlen 2r      R       r               pglen   r       R
#     f10   62c     90c     125c            f10     47l     64l
#     f9    69c     101c    139c            f9      52l     70l
#     f8    77c     113c    156c            f8      58l     78l
#     f7    89c     129c    179c            f7      65l     88l
#     f6                    209c
#     f5                    250c
#
# Options:
#     -s	Single column only.
#     Anything that enscript understands.
#
# Files:
#
# Diagnostics:
#
# Dependencies:
#     Above table applies only to Courier type face and Gaudy mode.
#
# Bugs:
#     Each file is filtered through expand(1) and read by awk(1) to
#         determine longest line, this can be slow for large files.
#     A banner page is produced for each file.
#

font=Courier						# mono space typeface
user=${USER}@`hostname`

for i
do 
    case $i in
	-d) debug=true; shift;;
	-s) sgl_col=true; shift;;			# single column
	-*) hard_opts="${hard_opts} \"$i\""; shift;;# buffer opts
	*)  break;;
    esac
done

for file
do
    cd `dirname ${file}`
    file=`basename ${file}`
    pathname=`pwd`/${file}
    if [ ! -f $pathname ]
    then
	echo "$pathname: No such file"
	continue
    fi

    cmd=`expand ${file} | awk '
	{
	    l = length ($0);
	    if (l > ll)
		ll = l;	    	    	    		# longest line in file
	}

	END {
    	    opts = "-1Rf'"${font}"'10";		# default - 1col portrait 10pt
	    dp = int (NR / 64); if (NR % 64 > 0) dp++;	# pgs for default

    	    if (ll > 179) {
    	    	cmd = "fold -176 '"${file}"' | ";
	    }
	    else {
		cmd = "cat '"${file}"' | ";
	    }
	    cmd = cmd "enscript -GJ'"${file}"' ";

    	    if (ll > 156) {
    	    	opts = "-1rf'"${font}"'7";    		# 1col landscape 7pt
	    }
    	    else if (ll > 139) {
    	    	opts = "-1rf'"${font}"'8"; 	    	# 1col landscape 8pt
	    }
    	    else if (ll > 125) {
    	    	opts = "-1rf'"${font}"'9"; 	    	# 1col landscape 9pt
	    }
    	    else if (ll > 113) {
    	    	opts = "-1rf'"${font}"'10";	    	# 1col landscape 10pt
	    }
   	    else if (ll > 101) {
    	    	opts = "-1Rf'"${font}"'8"; 	    	# 1col portrait 8pt
	    }
    	    else if (ll > 90) {
    	    	opts = "-1Rf'"${font}"'9"; 	    	# 1col portrait 9pt
	    }
    	    else if (ll > 89) {
    	    	opts = "-1Rf'"${font}"'10"; 	    	# 1col portrait 10pt
	    }
    	    else if (ll > 77 && ll < 90 && "'"${sgl_col}"'" == "") {
		np = int (NR / 130); if (NR % 130 > 0) np++;
		if (dp > np)
    	    	    opts = "-2rf'"${font}"'7";    	# 2col landscape 7pt
    	    }
    	    else if (ll > 69 && "'"$sgl_col"'" == "") {
		np = int (NR / 116); if (NR % 116 > 0) np++;
		if (dp > np)
    	    	    opts = "-2rf'"${font}"'8";    	# 2col landscape 8pt
    	    }
    	    else if (ll > 62 && "'"$sgl_col"'" == "") {
		np = int (NR / 104); if (NR % 104 > 0) np++;
		if (dp > np)
    	    	    opts = "-2rf'"${font}"'9";    	# 2col landscape 9pt
    	    }
    	    else  if ("'"$sgl_col"'" == "") {
		np = int (NR / 94); if (NR % 94 > 0) np++;
		if (dp > np)
    	    	    opts = "2rf'"${font}"'10";   	# 2col landscape 10pt
    	    }

	    if ("'"${debug}"'" == "true") {
		printf ("ll = %d, NR = %d ,dp = %d, np = %d, cmd = ",\
		     ll, NR, dp, np);
	    }
	    print cmd opts;
	}
    '`

    size=`ls -l ${file} | awk '{ print $4 }'`
    note=`echo ${cmd} | awk '/^fold/ { print " - (folded)"; }'`
    header="${pathname}, ${size} bytes, ${user}${note}"

    if [ -n "$debug" ]
    then
	echo ${cmd} -b\"${header}\" ${hard_opts}
    else
	echo -n "${pathname} "
	eval ${cmd} -b\"${header}\" ${hard_opts}
    fi
done
