#!/usr/bin/perl

#use Text::BibTeX;


require "bp.pl";

# 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{'noreport'});
    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";
    
    if(defined $prelist{$key}){
      $key.=$idx++;
      print "WARNING: $prelist{$key}<br>" if(defined $prelist{$key});
    }
    $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{

      $link="";
      if(defined $entry{'abstract_link'}){
	$img="link";
	$img="<IMG BORDER=0 SRC=\"/icons/sxi.gif\">" if($entry{'abstract_type'} eq "sxi");
	$img="<IMG BORDER=0 SRC=\"/icons/pdficonsm.gif\">" if($entry{'abstract_type'} eq "pdf");
	$img="<IMG BORDER=0 SRC=\"/icons/ppticon.gif\">" if($entry{'abstract_type'} eq "ppt");
	$img="<IMG BORDER=0 SRC=\"/icons/folder.gif\">" if($entry{'abstract_type'} eq "dir");
	$link="<A HREF=\"$entry{'abstract_link'}\">$img</A>";
      }

    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> $link</td>\n";
    }else{
      $prelist{$key}.="</td><td align=left >$entry{'title'} $link</td>\n";
    }
    }

    #$prelist{$key}.="<td class=tab>&nbsp;$poster</td>\n";
    if($entry{'poster_num'} ne ""){
    $prelist{$key}.="<td class=tab align=left>poster&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";
  }
  }
    #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);
}


print "<table align=center width=80\%><tr><td class=tab align=left><i><b>(Titles are given only for confirmed posters. If you don't find yours, please confirm it to z90info\@hea.iki.rssi.ru)</b></i></td></tr></table><br>";


print "<table align=center 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;
  }
}
