#!/usr/bin/perl -w
#
# 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: bl04.pl 425 2002-02-27 17:12:56Z horst $
#
package htBib_bl ;


use strict ;
no strict 'refs';
use diagnostics ;

use CGI qw(-compile :all);

use Text::BibTeX;
use tex_entities ; #qw(encode_entities);
use format_entry ;
use htbib_util ;
use htbib_auth ;

my ($entry, $entry_type) ;

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

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

my ($bibfile) ;
my $counter = 0 ;


my $topic = param("topic") || '' ; 
my $comment = param("comment") || '' ; 
my $dbs = param("dbs") || '' ; 
my $key = param("key") || '' ; 

use HTML::Entities ;



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

#use MLDBM 'DB_File' ;
use MLDBM qw(DB_File Storable ) ;

my $value = "";
my %COMMENT = ();
my $db = $dbs_dir . $dbs . "_comment.dbm" ;
my $db_entry = {};


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

print header(-expires=>'now') ;

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>' ;

# DEBUG
#print "db: $db<br>" ;
unless (tie (%COMMENT, 'MLDBM', $db)){
  print "<p>Can't open comment database '$db'!</p>\n";
  print "</body></html>\n" ;
  exit ;
}
else {
  # print "key: " . $key . "<br>\n" ;
  $db_entry = $COMMENT{$key} ; # retrieve from database ;
  # print "db_entry: $db_entry<br>\n" ;
}


#print "Content-type: text/html\n\n";

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 "        [&nbsp;<a href=\"" . $config{home_directory} . "\">$home_title</a>&nbsp;]\n" ;
print "        [<a href=\"./bl01.pl?key=" . $key ."&amp;dbs=" . $dbs ."&amp;topic=".CGI::escape($topic) . "\">List</a>]\n" ; 
print "   <hr noshade size=\"1\">" ;
print "  </div>" ;











print "  <h2>Publication:</h2>" ;


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

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

  print "      <div class=\"bibentry\">\n" ;
  print "&nbsp;$counter. " ;
  do_format( $entry ) ;
  print "\n      </div>\n" ; 
#  last ;
}
$bibfile->close ;








my %IMAGES = ();
my $book_image = "" ;
unless (tie (%IMAGES, 'MLDBM', $dbs_dir . $dbs . "_images.dbm")) {
  print "Can't open IMAGES database '" . $dbs_dir . $dbs . "_images.dbm'!" ;
}
# error handling!!!!
$book_image = $IMAGES{$key} ; # retrieve image

if ($book_image) {
  $IMAGES{$key} = $book_image ; # retrieve image

  if ($IMAGES{$key}) {
    print "\n<div class=\"book-image\">\n<img src=\"./images/$IMAGES{$key}\" alt=\"(Picture of book cover)\">\n</div>\n" ; # retrieve image
  }
}







my %SYNOPSIS = ();
my $synopsis = "" ;

#        tie (%SYNOPSIS, 'MLDBM', $dbs_dir . $dbs . "_synopsis.dbm") ;
unless (tie (%SYNOPSIS, 'MLDBM', $dbs_dir . $dbs . "_synopsis.dbm")) {
  print "Can't open SYNOPSIS database '" . $dbs_dir . $dbs . "_synopsis.dbm'!" ;
}

#### error handling!!!!

if (exists $SYNOPSIS{$key}) {
  print "<div class=\"synopsis\"><span class=\"synopsis-heading\">Synopsis</span>: $SYNOPSIS{$key}\n</div>" ; # retrieve synopsis

  if ($rc_auth) {
    print "  <div class=\"synopsis\">[<a href=\"./adm/sy02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "\">Edit</a>]" ;
    print "  [<a href=\"./adm/sy03.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "&amp;action=delete\">Delete</a>]\n" ;
    if ($config{show_kommerz}) {
      print " [<a href=\"./adm/co02.pl?key=" . $key ."&amp;dbs=" . $dbs . "&amp;topic=".CGI::escape($topic) . "\">Edit Shop Links</a>]" ;
    }
    print " [<a href=\"./adm/im02.pl?key=" . $key ."&amp;dbs=" . $dbs . "&amp;topic=".CGI::escape($topic) . "\">Edit Image</a>]" ;
    print "</div>\n" ;
  }

}
else {
  print "  <div class=\"synopsis\">\n" ;
#  print "    Currently this publication has no synopsis." ;

  if ($rc_auth) {
    print " Feel free to " ;
    print "<a href=\"./adm/sy02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "\">add a synopsis</a>." ;
    if ($config{show_kommerz} and $rc_auth) {
      print "  <br>[<a href=\"./adm/co02.pl?key=" . $key ."&amp;dbs=" . $dbs . "&amp;topic=".CGI::escape($topic) . "\">Edit Shop Links</a>]" ;
    }
    print " [<a href=\"./adm/im02.pl?key=" . $key ."&amp;dbs=" . $dbs . "&amp;topic=".CGI::escape($topic) . "\">Edit Image</a>]" ;
  }
  print "\n</div>\n" ;
}



print "  <p class=\"add-annotations\">\n" ;
print "We'd love for you to " ;
print "<a href=\"./an02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "\">add an annotation</a> for this book." ;
print "  </p>\n" ;





if ($config{show_kommerz}) {
  ###
  # display shop links
  my %KOMMERZ = ();
  my $kommerz = "" ;
  
  unless (tie (%KOMMERZ, 'MLDBM', $dbs_dir . $dbs . "_kommerz.dbm")) {
    print "<p>Can't open KOMMERZ database " ;
    print  $dbs_dir . $dbs . "_kommerz.dbm!</p>" ;
  }
  # error handling!!!!
  
  $kommerz = $KOMMERZ{$key} ; # retrieve kommerz
  
  if ($kommerz) {
    print "  <div class=\"buy-related\">\n" ;
    print "    <div class=\"buy-related-note\">";
    print "      <span class=\"buy-related\"> Buy this book from</span>:" ;
    print "    </div>\n" ;
    for ($counter = 1 ; $counter < 6 ; $counter++) {
      my $identifier = "shop" . $counter ;
      
      # entries for all shops might exist. even if they are empty.
      if ($kommerz->{$identifier}{"url"}) {

	print "    <a href=\"" . $kommerz->{$identifier}{"url"} . "\">" ;
	if ($kommerz->{$identifier}{"image"}) {
	  print "<img src=\"images/" . $kommerz->{$identifier}{"image"} . "\" border=\"0\" alt=\"" .  ($kommerz->{$identifier}{"name"} || '') . "\">" ;
#	  if ($kommerz->{$identifier}{"name"}) {
#	    print $kommerz->{$identifier}{"name"} ;
#	  }
	}
	else {
	  print $kommerz->{$identifier}{"name"} ;
	}
      print "</a> \n" ;
      }
    }
    print "    </div>\n" ;
  }
  untie %KOMMERZ ;
}



###
#

my $author = "" ;
my $annotation = "" ;
my @annotation_authors = [] ;

#while ( ($key, $value) = each %COMMENT)
#  {
#  print "<h1>\n" ;
#  $db_entry = $COMMENT{$key} ; # retrieve from database ;
#  print "<p>key" . $key . "</p>\n" ;

#  print "<p>annotations\n" ;
#  @annotation_authors = keys %$db_entry ;
#  print "length: " . scalar( @annotation_authors ) ;
#  if (@annotation_authors) {
#    foreach $author (@annotation_authors) 
#      {
#      print "<p>annotation>\n";
#      print "<p>author" ;
#      print $db_entry->{$author}{"author"} . "\n" ;
#      print "</p>\n" ;
      
#      $value = $db_entry->{$author}{"annotation"} ;

#      print "<p>comment" . $value . "</p>\n" ;

#      print "</p>\n";
#      }
#    } 
#  else
#    {
#    }
#  print "</p>\n" ;
#  print "</h1>\n\n\n" ;
#  }


@annotation_authors = keys %$db_entry ;
#print "annotation_authors: " . scalar( @annotation_authors ) . "<br>\n";
if (@annotation_authors) {


  print"  <h2>Annotations:</h2>\n" ;
  print"  <div class=\"annotations\">\n" ;

  $counter = 0 ;
  foreach $author (@annotation_authors) {
    if ($counter > 0) {
      print "  <hr>\n" ;
    }
    $counter++ ;
    print "  <p><span class=\"annotation-author\">" . $db_entry->{$author}{"author"} . "</span>:</p>\n" ;

#    $value = HTML::Entities::encode_entities( 
#					     $db_entry->{$author}{"annotation"}
#					    ) ;
#    $value =~ s/\&#13;\n\&#13;/<\/p>\n<p class="annotation">/g ;

    $value = $db_entry->{$author}{"annotation"} ;
#    $value =~ s/\&#13;\n\&#13;/<\/p>\n<p class="annotation">/g ;

#    print "  <p class=\"annotation\">" . $value  . "</p>" ;
    print $value . "\n" ;

    if ($rc_auth) {
      print "  <p>\n" ;
      print "  [<a href=\"./adm/bl02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "\">Add</a>]\n" ;
      print "  [<a href=\"./adm/bl02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "&author=" . CGI::escape($author) . "\">Edit</a>]" ;
      print "  [<a href=\"./adm/bl03.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "&amp;author=" . CGI::escape($author) . "&amp;action=delete\">Delete</a>]</p>\n" ;
      
    }
  }

}
else {
  if ($rc_auth) {
    print"  <h2>Annotations:</h2>\n" ;
    print"  <div class=\"annotations\">\n" ;

    print"  <p>Sorry. Currently this publication has no annotations. Feel free to " ;
    print "<a href=\"./adm/bl02.pl?key=" . $key ."&amp;dbs=" . $dbs."&amp;topic=".CGI::escape($topic) . "\">add an annotation</a>.</p>\n" ;
  }
}

print"  </div>\n" ; # annotation

untie %COMMENT ;
#untie %SYNOPSIS ;
#untie %IMAGES ;

print "    <div class=\"navigation\">\n" ;
print "       <hr noshade size=\"1\">" ;
print "       [&nbsp;<a href=\"" . $config{home_directory} . "\">Home</a>&nbsp;]\n" ;
print "        [<a href=\"./bl01.pl?key=" . $key ."&amp;dbs=" . $dbs ."&amp;topic=".CGI::escape($topic) . "\">List</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>\n" ;

}


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


