#!/usr/bin/perl -w
#
# Copyright © 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: bt01.pl 307 2000-02-29 14:35:03Z horst $
#
package htBib_bt ;

use lib "." ;

use strict ;
use diagnostics ;

use CGI qw(-compile :all);
use MLDBM 'DB_File' ;

use IO::File ;
use Fcntl ;

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

use read_config ;
my %config = ();
read_config::read_config( \%config, "./.htBib" ) ;


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

my ($entry, $entry_type) ;


my ($bibfile) ;
use HTML::Entities ; 

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

my $counter = 0 ;

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


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


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 "   <hr noshade size=\"1\">" ;
print "  </div>" ;


my $error = 0 ;

#print "<p>submit: $submit</p>\n" ;

my $fh ;


if ($submit eq "create") {
  unless ($new_db) {
    print "<p>Action <span class=\"warning\">$submit</span>: unable to comply. You have to specify a database name. Just type it into the field labeled &quot;Database&quot;.</p>\n" ;
  }
  else {
    
    if ($fh = IO::File->new( "<" . $dbs_dir . $new_db . ".bib" )) {
      print "<p><span class=\"warning\">Sorry:</span> unable to create database $new_db. A database with this name already exists!</p>\n" ;
    }
    else {
      
      unless ($fh = IO::File->new( 
				  $dbs_dir . $new_db . ".bib", 
				  O_WRONLY | O_CREAT
				 )
	     ) {
	print "<p><span class=\"warning\">Ooops</span>: unable to create database $new_db. This should not happen. Something is wrong. Please contact the system administrator <a href=\"mailto:" . $config{admin_email} . "\">" . $config{admin_name} . "</a>.</p>\n" ;
      }
      else {
	$fh->close() ;
      }
    }
  }
}
elsif ($submit eq "delete") {
  unless (unlink $dbs_dir . $new_db . ".bib") {
    
    if ($fh = IO::File->new( "<" . $dbs_dir . $new_db . ".bib" )) {
      print "<p><span class=\"warning\">Ooops</span>: unable to delete database $new_db. This should not happen. Something is wrong. Please contact the system administrator <a href=\"mailto:" . $config{admin_email} . "\">" . $config{admin_name} . "</a>.</p>\n" ;
      
      $fh->close() ;
    }
    else {
      print "<p><span class=\"warning\">Oops</span>: unable to delete database $new_db.\n" ;
      print "The database $new_db probably does not exist.</p>\n" ;
      
    }
  }
}


print "<h2>Available databases:</h2>\n" ;

print "<table>\n" ;
my @databases = htbib_util::available_databases( $dbs_dir ) ;
my $database = '' ;
foreach $database (@databases) {
  print "<tr><td><span class=\"database-name\">$database</span></td><td><a href=\"./bibtex/bt02.pl?dbs=$database\">Edit</a></td>\n" ;
  print "<td><a href=\"./bl01.pl?dbs=$database\">Show</a></td></tr>\n" ;
}
print "</table>\n" ;


print <<"EOF" ;
    <form action="bt01.pl" method="get">
      <p>
	Database:  <input type="text" name="new_db" size="20" maxlength="20">
	<input type="submit" name="submit" value="create">
	<input type="submit" name="submit" value="delete">
      </p>
    </form>
EOF




print "    <div class=\"navigation\">" ;
print "       <hr noshade size=\"1\">" ;
print "        [<a href=\"./" . $config{home_directory} . "\">$home_title</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>" ;

1;
