#!/bin/sh 
#
# Program to print the G&S mailing list, from the APO mailing list
#
# Usage:
#
# GSprint [options] {postscript_datafile_generated_by_database_program}
#
#     Possible options: "-P <printer>"
#			"-p <postcript output file>"
#			"-m"   <--- manualfeed options (for printing on
#					labels on the laser writer)
#
# Examples:
#	GSprint -P linus GandS.data.PS
#		Print the mailing list on the Athena printer "linus"
#
#	GSprint -m -p - GandS.data.PS | lwpr 
#		Print the mailing list on the SIPB laserwriter (in the SIPB
#		office).
#
# Written by Theodore Ts'o, [tytso:19890627.1935EDT]
#

tmpfile=/tmp/GSpro$$
tmpfila=/tmp/GSend$$

trap "rm -f $tmpfile $tmpfila" 0 1 4 9 15

datafile=GandS.data.PS
outfile=''
printer=''

while true ; do
	case $1 in
	-P)	printer=-P$2; shift; shift ;;
	-p)	outfile=$2; shift; shift ;;
	-m)	manualfeed=true; shift ;;
	*)	break ;;
	esac
done

if [ $# -gt 1 ] ; then
	echo "usage: $0 [-P printer] [-p outfile] [datafile]"
	exit 1
fi

if [ $# = 1 ] ; then
	datafile=$1
fi

cat <<'EAT!THIS!' > $tmpfile
%!postscript
%%
%% Prologue to print mailing labels from the APO mailing list....
/CurrentLabel 1 def
/CurrentColumn 1 def

/labelfont /Helvetica def
/labelsize 10 def
/linespacing 12 def

/inch {72 mul} def

/topmargin 0 def
/leftmargin 0.375 inch def
/labelwidth 2.75 inch def
/numlabels 10 def
/numcolumns 3 def

% converts column, label_number into x and y coordinates
% column label_number --- x, y coordinates for upper left label
/labelxy {
   % calculate y coordinate
   1 sub 72 mul 11 inch exch sub topmargin sub linespacing sub
   exch 1 sub labelwidth mul leftmargin add exch % calculate x coordinate
} def

% set up to do a label at a specified position
% column label ---
/SetupLabel {
	labelxy	
	/LabelY exch def
	/LabelX exch def
	LabelX LabelY moveto
} def

/newline {
	show
	/LabelY LabelY linespacing sub def
	LabelX LabelY moveto
} def

/StartLabel {
%	statusdict /manualfeed true put
	labelfont findfont labelsize scalefont setfont
	 CurrentColumn CurrentLabel SetupLabel
} def

/NewPage {
	showpage
	/CurrentLabel 1 def
	/CurrentColumn 1 def
	StartLabel
} def

/NextLabel {
	/CurrentLabel CurrentLabel 1 add def
	CurrentLabel numlabels gt {
		/CurrentLabel 1 def
		/CurrentColumn CurrentColumn 1 add def
	} if
	CurrentColumn numcolumns gt {
		NewPage
	} if
	CurrentColumn CurrentLabel SetupLabel
} def

%% ----- end of original label prologue

StartLabel

/DOBLOCK {
	pop pop pop pop
	/mail exch def
	/address2 exch def
	/address1 exch def
	/first exch def
	/last exch def
	pop
	last length 0 ne {
		() newline
		first show ( ) show last 
		newline
		address1 newline
		address2 newline
		mail newline
		NextLabel
	} if
} def

%% ----- end of label prologue

EAT!THIS!

if [ ${manualfeed} ] ; then
	echo "statusdict /manualfeed true put" >> $tmpfile
fi

if [ ${outfile} ] ; then
	if [ ${outfile} = '-' ] ; then
		echo "showpage" | cat $tmpfile $datafile - 
	else
		echo "showpage" | cat $tmpfile $datafile - > $outfile
	fi
else
	echo "showpage" >$tmpfila
	lpr $printer $tmpfile $datafile $tmpfila
fi


