#!/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.p1";
$bib_file2="/export/web/www/Z-90/data/reg-db.bib.p1";

$head="/export/web/www/Z-90/lhead.html";
$tail="/export/web/www/Z-90/ltail.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";


#  my $bibfile = new Text::BibTeX::File "/tmp/foo.bib";
#  my $newfile = new Text::BibTeX::File ">/tmp/newfoo.bib";
#   while ($entry = new Text::BibTeX::Entry $bibfile)
#   {
#      next unless $entry->parse_ok;
#      print "<hr>".$entry->key."<br>";
#      $entry->write ($newfile);
#   }
#  exit 0;



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

&check ($bib_file);

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>";
foreach $file ($_[0]) {
  next unless &bib'open($file);
  $totrecs = 0;

  while ($record = &bib'read($file) ) {

    %entry = &bib'explode($record);

    next if(defined $entry{'unable'});
    next if(defined $entry{'hide'});
        

    $entry{'author'} = $entry{'author1'}." ".$entry{'author2'} unless(defined $entry{'author'});

    #
    # split author name to extract last name and make shorter first name.
    #

    @l = split " ",$entry{'author'};
    $key = pop @l;
    push @l," $key";

    if( defined $prelist{$key}){
      $key.=$idx;
      $idx++;
    }
    $entry{'author'}=~s/ /_/;
    if($entry{'email'}=~m/(.ru$)|(.spb.edu$)|(.su$)/ |
       $entry{'address'}=~m/Moscow|Russia|Petersburg/ ||
       $entry{'institution'}=~m/Moscow|Russia|IKI|RAN|RAS/){

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

    }else{

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

    }

    


    if(defined($entry{'presentation_talk'})){
    $prelist{$key}.="<td>T</td>\n";
    }elsif(defined($entry{'presentation_poster'})){
    $prelist{$key}.="<td>P</td>\n";
    }elsif(defined($entry{'presentation_none'})){
    $prelist{$key}.="<td>N</td>\n";
    }


    $prelist{$key}.="<td align=center class=tab>&nbsp;".$entry{'attendence'}."</td>" ;#if(defined $entry{'attendence'});
    $prelist{$key}.="<td align=center class=tab>&nbsp;".$entry{'institution'}."</td>" ;#if(defined $entry{'institution'});
    $prelist{$key}.="<td align=center class=tab>&nbsp;".$entry{'address'}."</td>" ;#if(defined $entry{'address'});
    #$prelist{$key}.="<td align=center class=tab>&nbsp;".$entry{'title'}."</td>";

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

    if($entry{'use_preregistration'} eq "on"){
      $prelist{$key}.="</td><td align=left bgcolor=yellow>use prereg. info</td>\n";
    }else{
      $prelist{$key}.="</td><td align=left bgcolor=yellow></td>\n";
    }

    $prelist{$key}.="</td><td align=left bgcolor=yellow>&nbsp;$entry{'take_me_to_the_dinner'}</td>\n";
    $prelist{$key}.="</td><td align=left bgcolor=yellow>&nbsp;$entry{'dinner_apersons'}</td>\n";

      #$prelist{$key}.="</td><td align=left bgcolor=yellow>&nbsp;$entry{'arriving_date'} (arr.)</td>\n";
      #$prelist{$key}.="</td><td align=left bgcolor=yellow>&nbsp;$entry{'departure_date'} (dep.)</td>\n";




    if(defined $entry{'oral'}){
      $prelist{$key}.="</td><td align=left bgcolor=whitegreen>oral</td>\n";
    }else{
      $prelist{$key}.="</td><td align=left bgcolor=white>&nbsp;</td>\n";
    }


    $sub=(split " ",$entry{'submitted'})[0];
    $prelist{$key}.=				  
    "</td><td align=left bgcolor=lightgreen>\&nbsp;$sub (submitted)</td>\n";


    if(defined $entry{'unable'}){
      $prelist{$key}.="</td><td align=left bgcolor=red>unable</td>\n";
    }


    $prelist{$key}.="</tr>\n";
    $totrecs++;
  }
  &bib'close($file);
}

print "<table align=center 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 "<table align=center width=60\%><tr><td class=tab align=right>Total number is $totrecs.</td></tr></table><br>";

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

}

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