#!/usr/bin/perl -w
#
# mkmeta -- make ThML.head, store in $bookID.meta file
#
# 2000-02-23 v. 0.3 -- try to get data from file and database too
# 2000-02-21 v. 0.2, Harry Plantinga
#
# to do:
#    get info from database if possible
#
use strict;
my %info;
#
# this array contains tag names and prompts to use, if tag not found
#
my @tags = (
    "authorID" => "Author ID", 
    "name" => "Author name",
    "fileas" => "Author name [Last, First]",
    "years" => "Author years",
    "DC.Title" => "Book title",
    "version" => "Version [1.0]",
    "published" => "Publisher, date of print edition",
    "DC.Identifier" => "URL [/ccel/authorID/bookID.htm]",
    "DC.Subject scheme=\"LCCN\"" => "LC call no.",
    "publisher" => "Etext publisher [ccel]",
    "DC.Rights" => "Rights [public domain]",
    "DC.Source" => "Source of etext",
    "description" => "Description",
    "digitizer" => "Digitizer",
    "status" => "Editorial status",
    "comments" => "Comments");

my $input;
my $filename = $ARGV[0];
if (-f $filename) {
  while (<>) {
    $input .= $_;
  }
  $_ = $input;
}
$_ ||= "";
print "$filename: " . length($_) . " bytes\n";

m|&lt;bookID&gt;(.*?)&lt;/bookID&gt;|;
my $bookID = $1 || $filename;
$bookID =~ s/\.html?//;

if (!$bookID) {
  print "Book ID:          - ";
  $bookID = <>; chop($bookID);
}

print "\nbookID: $bookID\n";


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

my ($tag, $i);
for ($i=0; $i<$#tags; $i+=2) {		# try to find header info in the doc
  $tag = $tags[$i];
  find($tag);
}

#
# now try to get data from database
#

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

#
# Now process defaults.
#
$info{"DC.Rights"} ||= "Public Domain";
my $nm = $info{"name"};
$nm =~ s/^(.*) (\w+)\s*$/$2, $1/;
$nm .= " ($info{'years'})";
$info{"fileas"} = $nm;
$info{"version"} ||= "1.0";
$info{"publisherID"} = "";
$info{"publisherID"} ||= "ccel" unless $info{"DC.Publisher"};
$info{"DC.Publisher"} = 
    "Grand Rapids, MI: Christian Classics Ethereal Library" 
        if $info{"publisherID"} eq "ccel";
# get ccel subject codes here


#
# 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>
  <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>
    <DC.Publisher>$info{"DC.Publisher"}</DC.Publisher>
    <DC.Subject scheme="LCCN">$info{"DC.Subject scheme=\"LCCN\""}</DC.Subject>
    <DC.Subject scheme="ccel"></DC.Subject>
    <DC.Contributor sub="Digitizer">$info{"digitizer"}</DC.Contributor>
    <DC.Date sub="Created"></DC.Date>
    <DC.Type>Text.Monograph</DC.Type>
    <DC.Format scheme="IMT">text/html</DC.Format>
    <DC.Identifier scheme="URI">$info{"DC.Identifier"}</DC.Identifier>
    <DC.Source>$info{"DC.Source"}</DC.Source>
    <DC.Language>en-us</DC.Language>
    <DC.Rights>$info{"DC.Rights"}</DC.Rights>
  </DC>

  <comments>$info{"comments"}</comments>
</electronicEdInfo>
</ThML.head>
!!!


sub find {
  my $tag=shift;
  my $endtag = $tag;
  $endtag =~ s/ .*//;

  my $val = $1 if m|\&lt;$tag\&gt;([^>]*?)\&lt;/$endtag\&gt;|si;

  print STDERR "$tag: $val\n" if $val;
  $info{$tag} = $val;
}
  
sub getValue {
  my $x;
  $x = <>;
  chop $x;
  return $x;
}

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


