#!/usr/bin/perl

#use Text::BibTeX;


require "bp.pl";

%posterw=();
$posterw{'A'}=100;
$posterw{'B'}=200;
$posterw{'C'}=300;
$posterw{'D'}=400;

# get current time and data
@TIME = localtime(time);
$DATE = @TIME[3].".".(@TIME[4]+1).".".(@TIME[5]+1900);
$time = @TIME[2].":".@TIME[1].":".@TIME[0];


$bib_file1="/export/web/www/Z-90/data/z90-form.bib";
$bib_file2="/export/web/www/Z-90/data/reg-db.bib";

$head="/export/web/www/Z-90/head.html";
$tail="/export/web/www/Z-90/tail.html";
$form="form";

   @TIME = localtime(time);
    $DATE = @TIME[3].".".(@TIME[4]+1).".".(@TIME[5]+1900);
    $time = @TIME[2].":".@TIME[1].":".@TIME[0];
    $query;

    if($ENV{'REQUEST_METHOD'} eq 'GET'){
      $query = $ENV{'QUERY_STRING'};
    }elsif($ENV{'REQUEST_METHOD'} eq 'POST'){
      sysread(STDIN,$query,$ENV{'CONTENT_LENGTH'});
    }
    $query=urldecode($query);



if($query ne ''){
  @fvalues = split(/&/,$query);
  
  foreach $i (0 .. $#fvalues){
    $str=$fvalues[$i];
    ($name,$val) = split(/=/,$fvalues[$i],2);
    $name=&urldecode($name);
    $val=&urldecode($val);
    $field{$name} = $val;
  }
}

$bib_file=$bib_file1;
if(defined $field{'db'}){
$bib_file=$bib_file1 if ($field{'db'} eq "1");
$bib_file=$bib_file2 if ($field{'db'} eq "2");
}

print "Content-Type: text/html\n\n";


if(checkf($head)){
  open FD,"<$head" ;
  while (<FD>){
    print;
  }close FD;
}

$totrecs = 0;
&check;

if(checkf($tail)){
  open FD,"<$tail" ;
  while (<FD>){
    print;
  }close FD;
}



sub urldecode{
  local($val)=@_;
  #$val =~ s/\+/ /g;
  $val =~ s/%([0-9a-hA-H]{2})/pack('C',hex($1))/ge;
  return $val;
}


sub check{
&bib'format("auto");

%prelist=();

print "<TR><TD>";

$oral=1;

start:
$totrecs=0;
$cdinner=0;
$calive=0;
%prelist=();
foreach $file ($bib_file1,$bib_file2){

  next unless &bib'open($file);
  while ($record = &bib'read($file)){
    %entry = &bib'explode($record);
    next if(defined $entry{'unable'});
    next if(defined $entry{'hide'});
    next if(defined $entry{'oral'});
    next unless(defined $entry{'confirm_poster'});
    next if(defined $entry{'no_report'});
    next if(defined $entry{'use_preregistration'});
    $poster="";
    $hotel="";


#    if($oral){
#      next unless(defined $entry{'oral'});
#    $poster="hotel" if(defined $entry{'confirmed'});
#    }else{
#      next if(defined $entry{'oral'});
#      $poster="confirm" if(defined $entry{'confirm_poster'});
#    }

    #unless(defined $entry{'oral'}){
    #  next unless($entry{'alive'} eq "yes");
    #}

$poster="confirm" if(defined $entry{'confirm_poster'});
    $entry{'author'} = $entry{'author1'}." ".$entry{'author2'} unless(defined $entry{'author'});
    $work = $entry{'author'};
    $work =~ s/^([A-Za-z])[a-z]*/$1./;
    $work =~ s /\.\./\./;
    @l = split ' ',$work;
    $work =~ s / /\&nbsp;/;
    $entry{'author'}=$work;    
    $key = pop @l; push @l," $key";

    $key=$entry{'poster_num'};
    $k=$key;
    $k =~ s/(.)//;  $first_letter = $1;
    ($keynum) = split ('\D',$k);
    $key = $posterw{$first_letter} + $keynum;
    if(defined $prelist{$key}){
      $key.=$idx++;
      print "WARNING: $prelist{$key}<br>" if(defined $prelist{$key});
    }


    if($entry{'poster_num'} ne ""){
    $prelist{$key}.="<td class=tab align=left>&nbsp;$entry{'poster_num'}</td>\n";
  }else{
    if($entry{'oral'} ne ""){
      $prelist{$key}.="<td class=tab align=left>talk</td>\n";
  }else{
    $prelist{$key}.="<td class=tab>&nbsp;</td>\n";
  }
  }

    $prelist{$key}.="<td class=tab >$entry{'author'}</td>\n";

    #$entry{'title'}=~s/^ /_/;

    if((!defined $entry{'oral'}) && (!defined $entry{'confirm_poster'})){
      $prelist{$key}.="<td class=tab>&nbsp;</td>\n";
    }else{
    if($entry{'abstract'} ne ""){
      $abstract=$entry{abstract};
      $abstract=~s/\'+/&#180;/g;
      $abstract=~s/\"/&#180;/g;
      $abstract=~s/\\+/\\\\/g;
      $prelist{$key}.=
	  "</td><td align=left ><a class=tab href=\"javascript:winopen(\'$entry{'author'}\',\'$abstract\');\">$entry{'title'}</a></td>\n";
    }else{
      $prelist{$key}.="</td><td align=left >$entry{'title'}</td>\n";
    }
    }

    #$prelist{$key}.="<td class=tab>&nbsp;$poster</td>\n";

    #if(defined $entry{'alive'}){
    #  $calive++;
    #  $prelist{$key}.="<td class=tab> $entry{'alive'}</td>\n";
    #}else{
    #  $prelist{$key}.="<td class=tab>&nbsp;</td>\n";
    #}
    $prelist{$key}.="</tr>\n";
    $totrecs++;
  }
  &bib'close($file);
}


$c=1;
print "<TR><TD>";
print "<STYLE>BODY,DIV,TABLE,THEAD,TBODY,TFOOT,TR,TH,TD,P { font-family:\"Arial\"; font-size:x-small }</STYLE>";
open HTML,"<program.html";
while(<HTML>){
if(/^<body/) {$c=0; next};
next if($c);
s/bgcolor=\"\#ffffff\"//;
if(/\/12/){
s/size=\"4\">/size=\"3\" style=\"font-weight:bold;\">/;
}else{
s/size=\"4\">/size=\"2\" >/;
}



s/font-size:x-small/font-size:small/;
s/font-family:\"Arial\";//;

s/(bgcolor=\"\#99ccff\")|(background-color: rgb\(153, 204, 255\);)//;
s/bgcolor=\"\#ffcc99\"/style=\"font-weight:bold;\"/;

s/<\/body>//;
s/<\/html>//;


print;
}
close HTML;
print "</TD></TR><TR><TD>";

print "<HR><table align=center width=80\% cellspacing=0 cellpadding=0><tr><td class=tab align=left style=\"font-color=black;\"><b><font size=2>POSTER SESSION</font></b></td></tr></table><br>";



print "<table align=left border=1 width=80\% border=1  bordercolor=\"#0000cc\" bordercolordark=\"#FFFFFF\" bordercolorlight=\"#0000cc\" cellspacing=0 cellpadding=5>";
#print "<tr><th><span class=default>Participant</span></th><th class=default>P (\%)</span></th><th class=default>Title</span></th><th class=default>&nbsp;</span></th><th class=default>From...</span></th></tr>";

foreach $i (sort keys %prelist){
  print "$prelist{$i}";
}
print "</table>";



print "</TD></TR>";

}

sub checkf{
  unless (-f $_[0]){
    print "ERROR: Can\'t open file \"$_[0]\"" ;
    return false;
  }else{
    return true;
  }
}

