#!/usr/local/bin/perl

#
#  print.filter -- reads xpg print data from stdin and produce
#                  PostScript output, using groff.
#                  Copyright (C) 1993  Ranen Goren (ranen@cs.huji.ac.il)
#
#
# The following text describes the data output by xpg and parsed by this 
# script. Values appearing in the same line are separated by a tab character.
# The text between the brackets is my remarks.
#
#
# [--------------- starting to send --------------]
# class_name
# database_name
# attr_name_1 ... attr_name_m
# tuple_1_attr_1 ... tuple_1_attr_m
#  .
#  .
#  .
# tuple_n_attr_1 ... tuple_n_attr_m
# 	     [the part starting here till the end of the data is produced
# 	      only if at least one of the totals functions was requested]
# !TOTALS!     [signals the script that the table ended and totals start]
# totals_function_name_1
# totals_func_1_attr_1 ... totals_func_1_attr_m
#  .
#  .
#  .
# totals_func_o_attr_1 ... totals_func_o_attr_m
# [----------------- end of data -----------------]
#
#
# In order to see the data, simply start xpg with the "-printCmd cat"
# command-line switch; all output will be output to the terminal.



chop($className = <STDIN>);
chop($databaseName = <STDIN>);
chop($attNames = <STDIN>);
@attNames = split("\t", $attNames);
$num = @attNames;
chop($attTypes = <STDIN>);
@attTypes = split("\t", $attTypes);
for ($i=0; $i<$num; $i++)  {
	$just[$i] = "l";     #default is left justification
	foreach ((16,21,23,700,701))  {   #list of types to be right-justified
		$just[$i] = "r"  if $attTypes[$i] == $_;
	}
}

open(GR, "|groff -ts -me") || die "cannot find groff!\n";
#open(GR, "|cat") || die "cannot find cat?!\n";

print GR ".fo //-%-//\n";
print GR ".TS H\n";
print GR "center box;\n";
print GR "cbp+4", " s" x ($num-1), " .\n";
print GR "$className\n";
print GR ".T&\n";
print GR "cbp-2", " s" x ($num-1), " .\n";
print GR "$databaseName\n";
print GR "=\n";
print GR ".T&\n";
print GR "cbI | " x $num, "\n";
for ($i=0; $i<$num; $i++)  {
	print GR "$just[$i] | ";
}
print GR ".\n";
print GR "$attNames\n";
print GR "=\n";
print GR ".TH\n";

#now print the whole data
while ($_ = <STDIN>)
{
	last if $_=~/!TOTALS!/;
	print GR "_\n"  if $. > 5;
	print GR;
}

#print the totals part
if ($_ =~ /!TOTALS!/)
{
	print GR "_\n";

	while ($_ = <STDIN>)
	{
		print GR "_\n"  if $. > 5;

		print GR ".T&\n";
		print GR "lbI | " x $num, "\n";
		print GR ".\n";

		print GR;

		print GR ".T&\n";
		for ($i=0; $i<$num; $i++)  {
			print GR "$just[$i] | ";
		}
		print GR ".\n";

		$_ = <STDIN>;
		print GR;

	}
}

print GR ".TE\n";
close GR;
