#!/bin/sh
# 4.2BSD line printer spooler interface for PostScript/TranScript printer
# this is the printcap/lpd-invoked top level filter program for ALL file types
# Copyright (c) 1985,1987 Adobe Systems Incorporated. All Rights Reserved. 
# GOVERNMENT END USERS: See Notice file in TranScript library directory
# -- probably /usr/lib/ps/Notice
# RCSID: $Header: psint.proto,v 2.2 87/11/17 16:40:51 byron Rel $

PATH=/bin:/usr/bin:/usr/ucb
export PATH

# set up initial undefined variable values
width= length= indent= login= host= afile=
dfile=bogus
prog=$0
cwd=`pwd`
pname=`basename $cwd`

# CHANGE THIS TO POINT TO THE RIGHT THING
exceeded=/usr/adm/exceeded.ps

# define the default printer-specific and TranScript
# configuration options, these may be overridden by
# real printer-specific options in the .options file

PSLIBDIR=//usr/lib/transcript
REVERSE=//usr/lib/transcript/psrv
SPOOLDIR=/usr/spool/NeXT/$pname
VERBOSELOG=1
BANNERFIRST=0
BANNERLAST=0
BANNERPRO=$PSLIBDIR/banner.pro
PSTEXT=$PSLIBDIR/pstext
PSCOMM=$PSLIBDIR/pscomm
PAGECOUNT=$PSLIBDIR/pagecount
UPDATEPAGE=$PSLIBDIR/updatepage
export PSLIBDIR VERBOSELOG BANNERFIRST BANNERLAST BANNERPRO REVERSE PSTEXT

# load the values from the .options file if present
test -r ./.options && . ./.options

# parse the command line arguments, most get ignored
# the -hhost vs. -h host is due to one report of someone doing it wrong.
# you could add or filter out non-standard options here (-p and -f)

while test $# != 0
do	case "$1" in
	-c)	;;
	-w*)	width=$1 ;;
	-l*)	length=$1 ;;
	-i*)	indent=$1 ;;
	-x*)	width=$1 ;;
	-y*)	length=$1 ;;
	-n)	user=$2 ; shift ;;
	-n*)	user=`expr $1 : '-.\(.*\)'` ;;
	-h)	host=$2 ; shift ;;
	-f)	dfile=$2 ; shift ;;
	-h*)	host=`expr $1 : '-.\(.*\)'` ;;
	-*)	;; 
	*)	afile=$1 ;;
	esac
	shift
done

PATH=$PSLIBDIR:$PATH
export PATH

# now exec the format-specific filter, based on $prog
# if - communications filter [default]
# of - banner filter [execed directly]
# nf - ditroff, tf - troff (CAT), gf - plot
# vf - raster, df - TeX DVI, cf - cifplot, rf - fortran

prog=`basename $prog`
comm="$PSCOMM -P $pname -p $prog -n $user -h $host $afile"

case $prog in
	psif) 	device=`niutil -read . /printers/$pname | grep "^lp: " | sed "s/lp: //"` ;
		page="$PAGECOUNT $device" ;
		quota=`niutil -read / /users/$user | grep "^pagequota:" | sed "s/pagequota: //"` ;
		usage=`niutil -read / /users/$user | grep "^pageusage:" | sed "s/pageusage: //"` ;
		if [ Q$quota = "Q" ] ; then
			quota=-1 ;
			usage=-1;
		fi ;
		if [ $usage -gt $quota ] ; then
			cat $exceeded | \
			sed "s/USER/$user/" | \
			sed "s/QUOTA/$quota/" | \
			sed "s/USAGE/$usage/" | \
			sed "s/HOST/$host/" > $SPOOLDIR/$dfile ;
			quota=-1 ;
		fi ;
		page1=`$page` ;
		$comm ;
		page2=`$page` ;
		pages=`echo "$page2 - $page1" | bc` ;
		newuse=`echo "$usage + $pages" | bc` ;
		if [ $quota -ne -1 ] ; then
			$UPDATEPAGE $user $pages
		fi ;
#		echo $user $host $pages $pname > /dev/console ;
		exit 0 ;;

	psof) exec psbanner $pname ; exit 0 ;;
	psnf) psdit | $comm ;;
	pstf) pscat | $comm ;;
	psgf) psplot | $comm ;;
	psvf|pscf|psdf|psrf) echo "$prog: filter not available." 1>&2  ;
			psbad $prog $pname $user $host | $comm ;;
esac
