#!/usr/bin/perl -w
#
# mkmeta -- make ThML.head, store in $bookID.meta file
#
# 2000-05-30 v. 0.3 -- try to get data from path, database.
# 2000-02-21 v. 0.2, Harry Plantinga
#
use strict;
#use DBI;

#
# this array contains tag names and prompts to use, if tag not found
#
my @tags = (
    "authorID" => "Author ID", 
    "name" => "Author name (short form)",
    "fileas" => "Author name [Last, First] (born-died)",
    "DC.Title" => "Book title",
    "ISBN" => "ISBN",
    "series" => "Series ID []",
    "version" => "Version [1.0]",
    "published" => "Publisher, date of print edition",
    "DC.Subject scheme=\"LCCN\"" => "LC call no.",
    "publisherID" => "Etext publisher [ccel]",
    "DC.Rights" => "Rights [public domain]",
    "DC.Source" => "Source of etext",
    "surl" => "Source URL",
    "DC.Identifier" => "URL [std ccel]",
    "description" => "Description",
    "digitizer" => "Digitizer",
    "status" => "Editorial status",
    "comments" => "Comments");

my (%info, $i, $input, @data);

my $authorID = shift;
$info{"authorID"} = $authorID;
my $bookID   = shift;
$info{"bookID"} = $bookID;
my $url      = shift;
$info{"DC.Identifier"} = $url;
my $path = `pwd`; chomp $path;

$authorID = getValue("authorID") unless $authorID;
$bookID = getValue("bookID") unless $bookID;
#print "authorID: $info{'authorID'} -- bookID: $info{'bookID'}\n";

die "\nMetadata file $bookID.meta already exists -- \n" .
    "  edit it or delete it to change the data.\n"
    if -f "$bookID.meta"; 

print "Making metadata for $info{'authorID'}/$info{'bookID'}\n";

#
# try to get data from database
#
# my $dsn = "DBI:mysql:database=ccel;host=pascal.calvin.edu;port=3306";
# my $dbh = DBI->connect($dsn, "user","password") #deleted...
#       or die "Couldn't connect to database!" . DBI->errstr;

# my $query="select fullname, shortname from person where personID='$authorID'";
# my $sth=$dbh->prepare($query) or die "Couldn't prep -- $dbh->errstr\n";
# $sth->execute() or die "Couldn't execute statement -- $dbh->errstr\n";
# if (@data = $sth->fetchrow_array()) {
#   $info{'fileas'} = $data[0];
#   $info{'name'} = $data[1];
#   print "Name:   $info{'name'}\n";
#   print "Fileas: $info{'fileas'}\n";
# }

#
# get path to current working directory to use as default
#
$path =~ s|^.*htdocs||;
$url = "http://www.ccel.org" . $path . "/htm/i.htm" unless $url;

#
# prompt for data we still don't have
#
for ($i=0; $i<$#tags; $i+=2) {		
  get($tags[$i], $tags[$i+1]);
}

#
# prompt for other authors
#
print "Other authors? [n] ";
my $resp=<>;
my $moreauthors = "";
while ($resp =~ m/\s*y/i) {
  print "  authorID: ";
  my $addAuthID = <>; chomp($addAuthID);
  print "ID: $addAuthID\n";

  #
  # prompt for other authors; add to database if necessary
  #
# $query="select fullname, shortname from person where personID='$addAuthID'";
# $sth=$dbh->prepare($query) or die "Couldn't prep -- $dbh->errstr\n";
# $sth->execute() or die "Couldn't execute statement -- $dbh->errstr\n";
# my ($fileas, $name);
# if (@data = $sth->fetchrow_array()) {
#   ($fileas, $name) = @data;
#   print "$name -- $fileas\n";
# } else {
#   print "That author isn't in the database.  Let's add.\n";
#   print "Shortname: ";
#   $name = <>; chomp($name);
#   print "Last, First (years): ";
#   $fileas = <>; chomp($fileas);
#   $query="insert into person(personID, fullname, shortname)
#       values('$addAuthID','$fileas','$name')";
#   print "$query\n";
#   $sth=$dbh->prepare($query) or die "Couldn't prep -- $dbh->errstr\n";
#   $sth->execute() or die "Couldn't execute statement -- $dbh->errstr\n";
#   print "$name has been inserted.\n";
# }
  print "Role: ";
  my $role=<>; chomp $role;

  $moreauthors .= 
      "<DC.Creator sub=\"$role\" scheme=\"ccel\">$addAuthID</DC.Creator>\n";

  print "Other authors? [n] ";
  $resp=<>;
}

#print "moreauthors: $moreauthors\n";


#
# Now process defaults.
#
$info{"DC.Rights"} ||= "Public Domain";
$info{"version"} ||= "1.0";
$info{"publisherID"} ||= "ccel" unless $info{"DC.Publisher"};
$info{"DC.Publisher"} = "";
$info{"DC.Publisher"} = 
    "Grand Rapids, MI: Christian Classics Ethereal Library" 
        if $info{"publisherID"} eq "ccel";

$path =~ s|^.*htdocs||;
if (!$url) {
  my $url = "http://www.ccel.org" . $path . "/htm/i.htm";
}

$info{"DC.Identifier"} ||= $url;
print "URL: $info{'DC.Identifier'}\n";

print "CCEL Subjects: All; Bibles; Proofed; Classic; Early Church;
Fiction; History; Hymns; Mysticism; Reference; Sermons; Symbols
CCEL subjects - ";
my $ccsubjects = <>; chomp($ccsubjects);

my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
    localtime;
$mon = "0$mon" if $mon < 10;    
$mday = "0$mday" if $mday < 10;    
my $date = "" . $year + 1900 . "-$mon-$mday";

#
# output metadata file
#
open OUT, ">$bookID.meta";
print OUT <<"!!!";
<ThML.head>

<generalInfo>
  <description>$info{"description"}</description>
  <pubHistory></pubHistory>
  <comments>$info{"comments"}</comments>
</generalInfo>

<printSourceInfo>
  <published>$info{"published"}</published>
</printSourceInfo>

<electronicEdInfo>
  <publisherID>$info{"publisherID"}</publisherID>
  <authorID>$info{"authorID"}</authorID>
  <bookID>$bookID</bookID>
  <version>$info{"version"}</version>
  <series>$info{"series"}</series>
  <editorialComments></editorialComments>
  <revisionHistory></revisionHistory>
  <status>$info{"status"}</status>

  <DC>
    <DC.Title>$info{"DC.Title"}</DC.Title>
    <DC.Creator sub="Author">$info{"name"}</DC.Creator>
    <DC.Creator sub="Author" scheme="file-as">$info{"fileas"}</DC.Creator>
    $moreauthors 
    <DC.Publisher>$info{"DC.Publisher"}</DC.Publisher>
    <DC.Subject scheme="LCCN">$info{"DC.Subject scheme=\"LCCN\""}</DC.Subject>
    <DC.Subject scheme="ccel">$ccsubjects</DC.Subject>
    <DC.Contributor sub="Digitizer">$info{"digitizer"}</DC.Contributor>
    <DC.Date sub="Created">$date</DC.Date>
    <DC.Type>Text.Monograph</DC.Type>
    <DC.Format scheme="IMT">text/html</DC.Format>
    <DC.Identifier scheme="URL">$info{"DC.Identifier"}</DC.Identifier>
    <DC.Identifier scheme="ISBN">$info{"ISBN"}</DC.Identifier>
    <DC.Source>$info{"DC.Source"}</DC.Source>
    <DC.Source scheme="URL">$info{"surl"}</DC.Source>
    <DC.Language>en</DC.Language>
    <DC.Rights>$info{"DC.Rights"}</DC.Rights>
  </DC>

</electronicEdInfo>
</ThML.head>
!!!

# $sth->finish;
# dbh->disconnect;
exit(0);



#
# get a value using prompt; store in $info{$prompt}
#
sub getValue {
  my $prompt = shift;
  print "$prompt: ";
  my $val = <>;
  chomp $val;
  $info{"$prompt"} = $val;
  return $val;
}


sub get {
  my $tag = shift;
  my $prompt = shift;
  my $x;
  if (!$info{$tag}) {
    print "$prompt" . " " x (24-length($prompt)) . " - ";
    $x = <>;
    chop $x;
    $info{$tag} = $x;
  }
}


