#!/usr/bin/perl
#
# Copyright (C) 1999 Horst Tellioglu, All Rights Reserved
#
# This library is free software; you can redistribute it and/or modify it
# under the terms of the GNU Library General Public License as published by
# the Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.

# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
# License for more details.  
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
# $Id: bl01.pl 425 2002-02-27 17:12:56Z horst $
#
package htBib::htBib_bl ;

use strict ;
use diagnostics ;

use CGI qw(-compile :all);

use Text::BibTeX;
use tex_entities ;
use format_entry ;
use htbib_util ;
use htbib_auth ;

my ($entry, $entry_type) ;

my %field ;


my ($bibfile) ;
my $topic = param("topic") || '' ; 

use read_config ;
my %config = () ;
read_config::read_config( \%config ) ;

my $dbs_dir = $ENV{DOCUMENT_ROOT} . "/" . $config{root_directory} . "/dbs/" ;

print header(-expires=>'now') ;
#print "Content-type: text/html\n\n";

my %SYNOPSIS = ();
my $synopsis = "" ;
my $counter = 0 ;
my $dbs = param("dbs") || '' ; 
use MLDBM qw(DB_File Storable ) ;
#use MLDBM 'DB_File' ;

my %COMMENT = ();
my $db = $dbs_dir . $dbs . "_comment.dbm" ;
my $db_entry = () ;
my @annotation_authors = [] ;


my $title = $config{title} || 'htBib' ;

print '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"' ;
print '                      "http://www.w3.org/TR/REC-html40/loose.dtd">' ;
print '<html>' ;
print '  <head>' ;
print "    <title>$title</title>" ;
print '    <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">' ;
print '    <meta http-equiv="expires" content="0">' ;
print '    <link href="./formate.css" type="text/css" rel="stylesheet">' ;
print '  </head>' ;
print '  <body>' ;



if (open (HEADER, "./.header")) {
  print while <HEADER> ;
  close HEADER ;
  print_heading( $topic ) ; 
  print "  <div class=\"navigation\">\n" ;
}
else {
  print_heading( $topic ) ;
  print "  <div class=\"navigation\">\n" ;
  print "     <hr noshade size=\"2\">\n" ;
}
my $home_title = $config{home_title} || 'Home' ;
print "        [<a href=\"" . $config{home_directory} . "\">$home_title</a>]\n" ;
print "[<a href=\"./dbs/$dbs.bib\">BibTeX</a>]\n" ;



#my $r = Apache->request() ;
#my $log = $r->log ;
#my ($rc, $sent_pw) = $r->get_basic_auth_pw ;
#$log->debug( "\n\nget_basic_auth_pw: $rc" ) ;  
##  return 0 if $rc != OK ;
#my $user = $r->connection->user ;
#$log->debug( "user: " . ($user || '') ) ;
#$log->debug( "some_auth_required: " . ($r->some_auth_required || '')) ;
#$log->debug( "REMOTE_USER: " . (remote_user || '') ) ;  

#my $rc_auth = 0 ;



my ($rc_auth, $user, $sent_pw) = check_authentification( $config{"AuthUserFile"} ) ;

unless ($rc_auth) {
  print "       [<a href=\"./login.pl?dbs=$dbs&amp;topic=" . CGI::escape($topic) . "&amp;old_location=bl01.pl\">Log In</a>]\n" ;
  #print "       [&nbsp;<a href=\"./login.pl?dbs=$dbs&amp;topic=" . CGI::escape($topic) . "\">Log In</a>&nbsp;]\n" ;
}
else {
  print "       (Logged In)\n" ;
}
print "   <hr noshade size=\"1\">" ;
print "  </div>" ;


print "Could not open synopsis dbs!\n" unless tie (%SYNOPSIS, 'MLDBM', $dbs_dir . $dbs . "_synopsis.dbm") ;

## error handling!!!!
print "Could not open annotations!\n" unless tie (%COMMENT, 'MLDBM', $db) ;


my %KOMMERZ = ();
tie (%KOMMERZ, 'MLDBM', $dbs_dir . $dbs . "_kommerz.dbm") ;
# error handling!!!!

$bibfile = new Text::BibTeX::File  $dbs_dir . $dbs . ".bib";

my $num_annotations = 0 ;

while ($entry = new Text::BibTeX::Entry $bibfile) {
  next unless $entry->parse_ok;
  $counter++ ;

  print "      <div class=\"bibentry\">\n" ;
  print "$counter. " ;

    do_format(
	      $entry, 
	      "bl04.pl?key=".$entry->key."&amp;dbs=".$dbs."&amp;topic=".CGI::escape($topic),
	     ) ;

#  if ((exists $SYNOPSIS{$entry->key}) or
#      (exists $COMMENT{$entry->key}) or
#      (exists $KOMMERZ{$entry->key}) or
#      $rc_auth
#     ) {
#    do_format(
#	      $entry, 
#	      "bl04.pl?key=".$entry->key."&amp;dbs=".$dbs."&amp;topic=".CGI::escape($topic),
#	     ) ;
#  }
#  else {
#    do_format(
#	      $entry
#	     ) ;
#  }
  print "      </div>\n" ;


  if (exists $SYNOPSIS{$entry->key}) {
    print "     <div class=\"synopsis\">\n" ; 
    print "     <img src=\"images/right_arrow.png\" alt=\"Synopsis:\" border=\"0\">" ;
    print "$SYNOPSIS{$entry->key}</div>\n" ; # retrieve synopsis
  }


  $db_entry = $COMMENT{$entry->key} ;
  @annotation_authors = keys %$db_entry ;
  if (@annotation_authors) {
    print "     <div class=\"read-more\">\n" ; 
    print "     <a href=\"bl04.pl?key=".$entry->key."&amp;dbs=".$dbs."&amp;topic=".CGI::escape($topic) . "\">Read more...</a>\n" ;
    print "</div>\n" ;
  }
#  if (exists $KOMMERZ{$entry->key}) {
#    print "     <div class=\"read-more\">\n" ; 
#    print "     <a href=\"bl04.pl?key=".$entry->key."&amp;dbs=".$dbs."&amp;topic=".CGI::escape($topic) . "\"><span class=\"buy-related\">Buy related</span></a>\n" ;
#    print "</div>\n" ;
#  }
    
}
$bibfile->close ;
untie %COMMENT ;
untie %SYNOPSIS ;
untie %KOMMERZ ;

print "    <div class=\"navigation\">" ;
print "       <hr noshade size=\"1\">" ;
print "       [<a href=\"" . $config{home_directory} . "\">$home_title</a>]\n" ;
print "       [<a href=\"./dbs/$dbs.bib\">BibTeX</a>]\n" ;
print "       [<a href=\"./login.pl\">Log In</a>]\n" ;
if (open (FOOTER, "./.footer")) {
  print "    </div>" ;

  print while <FOOTER> ;
  close FOOTER ;
}
else {
  print "       <hr noshade size=\"2\">" ;
  print "    </div>" ;

  print "<div class=\"copyright\">" ;
  print $config{copyright} ;
  print "</div>" ;
}


print "  </body>" ;
print "</html>" ;
