#!/usr/bin/perl

#
# Script to generate author index
#
# This script assumes the availability of a file with
# matching reference numbers and proceeding page number.
# The script output is in HTML.
#
# Syntax: perl script < PAGELIST-FILE
#
# Author: Steinar Hauan <steinhau@pvv.org>
#

$ENV{'PATH'}    = '/bin:/usr/bin';
$ENV{'IFS'}     = '';
$ENV{'SHELL'}   = '/bin/sh';

unshift (@INC,'/local/web/PSESCAPE-97/perl');

require "defines.pl";
require "utils.pl";

print STDERR "Reading pagelist...\n";
(%pages) = &GetPageList;
print STDERR "Reading database paperlist...\n";
(%refs)  = &GetPaperList;

undef %auths;
undef %arefs;
undef %xcheck;

print STDERR "Extracting authors...\n";
for $ref (sort keys %pages)
{
    # special cases: invited lectures (=not in database)
    if ( $ref == 1 ){
	$alist = "Author of Invited Lecture 1";
    } elsif ( $ref == 2 ) {
	$alist = "Author of Invited Lecture 2";
    } else {
	# cross check list
	$xcheck{$ref} = 1;

	$cfg = sprintf("%s/paper.ps\@control",&Num2Dir($ref));
	if ( -f $cfg )
	{
	    (%data) = &ReadPaperConfig($cfg);
	    if ( $data{'review'} ne 'accept' )
	    {
		print STDERR "\t(FATAL) ref $ref is not accepted\n";
		next;
	    }
	} else {
	    print STDERR "\t(FATAL) no ref $ref (page $pages{$ref})\n";
	    next;
	}
	# get & preparse list of authors
	($alist = $data{'authors'}) =~ s/ and /, /g;
    }


    # get array of individual authors
    @atab = split(/,/,$alist);

    # parse/rewrite each entry
    foreach $a (@atab)
    {
	$a =~ s/^(\s+)//;	# remove leading whitespace
	$a =~ s/\./ /g;		# change all dots to space
	$a =~ s/(\s+)/ /g;	# change multiple whitespace
				# to single spaces

	@tmp = split(/ /,$a);
	if ( $#tmp < 1 ) # if it didn't split into 2+ parts
	{
	    print "\t(Parse Error) name $a unparsable\n";
	    next;
	}
	# start ref with last name
	$aname = "$tmp[$#tmp], ";

	# make capitalized initials
	for ($i=0; $i<$#tmp; $i++)
	{
	    $tmp[$i] =~ s/^\.//;
	    $tmp[$i] =~ y/a-z/A-Z/;
	    $int = (split(//,$tmp[$i]))[0];
	    next if ( $int eq '' );
	    $aname .= sprintf("%s.",$int);
	}

	# store data
	if ( $auths{$aname} eq '' )       # if first reference
	{
	    $auths{$aname} = 1;
	    $arefs{$aname} = $pages{$ref};
	} else {		          # else append
	    $auths{$aname}++;
	    $arefs{$aname} .= ":$pages{$ref}";
	}
    }
}

print STDERR "Writing author index...\n";
for $a (sort keys %auths)
{
    # print author name
    print "  <DD><B>$a</B> : ";
    # sort references and make HREFs to list
    @xlist = sort numerically split(/:/,$arefs{$a});
    for ($i=0; $i<=$#xlist; $i++)
    {
	print "<A HREF=\"PaperList.html#PAGE_$xlist[$i]\">$xlist[$i]</A>";
	if ( $i == $#xlist )
	{
	    print "\n";
	} else {
	    print ", ";
	}
    }
}

print STDERR "Crosschecking...\n";
for $ref (sort keys %refs)
{

    $PageRefList{$pages{$ref}} = $ref;

    next if ( $xcheck{$ref} ne '' );

    # mask out rejections
    $cfg = sprintf("%s/paper.ps\@control",&Num2Dir($ref));
    (%data) = &ReadPaperConfig($cfg);
    next if ( $data{'review'} ne 'accept' );

    print STDERR "\tref $ref: not included in pagelist\n";
}

print STDERR "Writing pagelist...\n";
for $p (sort numerically keys %PageRefList)
{
    $ref = $PageRefList{$p};
    next if ( $xcheck{$ref} eq '' );
    $cfg = sprintf("%s/paper.ps\@control",&Num2Dir($ref));
    (%pdata) = &ReadPaperConfig($cfg);   

    $cont  = "$pdata{'firstname'} $pdata{'lastname'}";
    print "<B><A NAME=\"PAGE_$pages{$ref}\"></A>Page $pages{$ref}</B>\n";
    print "<DL>\n";
    print "  <DD><B>Title</B>: <B><EM>$pdata{'title'}</EM></B>\n";
    print "  <DD><B>Authors</B>: $pdata{'authors'}\n";
    print "  <DD><B>Affil.</B>: $pdata{'affiliation'}\n";
    print "  <DD><B>Contact</B>: $cont\n";
    print "  <DD><B>Address</B>: $pdata{'adress'}\n";
    print "  <DD><B>email</B>: $pdata{'email'}\n";
    print "  <DD><B>phone/fax</B>: $pdata{'phone'} / $pdata{'fax'}\n";
    print "</DL>\n";
}

exit 0;

sub numerically { $a <=> $b; } # subroutine used by sort

sub GetPageList
{
    undef %pagelist;
    local($ref,$start) = 0;
    while(<STDIN>)
    {
	s/(\s+)/ /; # change multiple whitespace to <SPACE>
	($ref,$start) = split;
	if ( $pagelist{$ref} eq '' )
	{
	    $pagelist{$ref} = $start;
	} else {
	    printf(STDERR "\tref $ref repeated in pagelist (pages %d and %d)\n",
		   $pagelist{$ref},$start);
	}
    }
    return (%pagelist);
}
