#!/usr/bin/perl
#
# Run carthage to make a new TEI  DTD
#
# Sebastian Rahtz 2002/04/14
# 
# There are no mandatory options; the default is 
# effectively to build teilite. The most likely thing to change
# is the path where the TEI DTDs can be found, so we show the default
# up front.
#
$TEIDTD="/usr/local/share/sgml/tei/p4";
$ISODIR="../../XML_Entities";
use File::Copy;
use English;
use Getopt::Long;
use FileHandle;
use File::Basename;
use Cwd;

if ($^O eq 'MSWin32') 
 { $IsWin32 = 1 ;}
else 
 { $IsWin32 = 0 ;}

($SysSec,$SysMin,$SysHour,$SysMday,$SysMon,$SysYear,$SysWday,$SysYday,$isdst) =
						localtime(time);
$SysMon=$SysMon +1 ;
if ($SysMon < 10) { $SysMon= "0" . $SysMon; }
if ($SysMday < 10) { $SysMday= "0" . $SysMday; }
if ($SysHour < 10) { $SysHour= "0" . $SysHour; }
$SysYear=$SysYear+1900;

# OPTIONS. 

# The base set
# one of prose, verse, drama, speech, terminology,general
# should do error checking on this.
$opt_base="prose";

# The extra tag sets (space-separated list)
# linking figures analysis 
# fs certainty transcr textcrit names.dates nets corpora
$opt_tagsets="linking figures analysis";

# which ISO entity sets to include
$opt_isoents="isolat1 isonum isopub";

# output
$opt_result="mytei.dtd";

# pre-existing driver file?
$opt_driver="";

# generate XML
$opt_xml=1;
$opt_sgml=0;

# generate tag omissibility
$opt_xmlpe=0;

# where the TEI DTD is
$opt_teidir="$TEIDTD";

# where the ISO entity sets are
$opt_isodir="$ISODIR";

# pass options to carthago
$opt_carthageopts="--commdrop";

$opt_verbose=1;
$opt_debug=0;
$opt_help=0;

$result = GetOptions (
# state changes
"debug!",               # verbose result and debug files left behind
"verbose!",             # verbose result
"xml!",                 # make XML DTD
"sgml!",                # make SGML DTD
"xmlpe!",               # make XML DTD with tag omissibility
"help!",                # give options
"teidir=s",             # location of TEI DTDs
"ext=s",                # stem name of extension DTD and ENT files
"result=s",             # name of final DTD
"base=s",		# base tagset
"tagsets=s",		# list of tag sets to include
"isoents=s",            # list of ISO entity sets to include
"driver=s",		# pre-existing driver file
"carthageopts=s",	# options to pass to carthage
);

if ($result eq 0 ) {
  die ("FATAL: OPTION FAILURE");
}

if ($opt_help) {
print "Usage:\nmaketeidtd [--debug] [--verbose] [--base=basename] [--tagsets=list]\n\t[--driver=DRIVER] [--teidir=PATH-TO_TEI]\n\t[--ext=EXTSTEM] [--result=RESULTNAME]\n\t[--isoents=list] [--xmlpe] [--noxml] [--sgml]\n";
 exit(0);
	}

if (!-f "$opt_teidir/tei2.dtd") { 
  die ("FATAL: $opt_teidir/tei2.dtd does not exist"); }
if ($opt_sgml) { $opt_xml=0; $opt_xmlpe=0;}
if ($opt_verbose) {
print "maketeidtd:\n";
  if ($opt_xmlpe) { 
                  $XMLFLAGS=" --xmlpe "; 
                  print "\tMaking an XML dtd with parameterized tag omissibility\n"; 
		}
  elsif ($opt_xml) { 
                  $XMLFLAGS=" -x "; 
                  print "\tMaking an XML dtd\n"; 
		}
  else          { $XMLFLAGS=" "; 
		  print "\tMaking an SGML dtd\n"; }
print "\tTEI master DTD directory: $opt_teidir\n";
print "\tDirectory containing ISO entity files: $opt_isodir\n";
print "\tBase tag set: $opt_base\n";
print "\tISO entity sets: $opt_isoents\n";
print "\tExtra tag sets: $opt_tagsets\n";
if ($opt_ext) {
 if (!-f "$opt_ext.ent") { die ("FATAL: $opt_ext.ent does not exist"); }
 if (!-f "$opt_ext.dtd") { die ("FATAL: $opt_ext.dtd does not exist"); }
 print "\tUser extension files: $opt_ext.dtd, $opt_ext.ent\n"
}

if ($opt_result) {
 print "\tResult file: $opt_result\n";
}

}
#---------------------------------------------------------
# prepare driver skeleton file, if not provided

@tagsets=split(" ", $opt_tagsets);
@isoents=split(" ", $opt_isoents);
if ($opt_driver eq "") {
 $opt_driver="driver.$$";
 open DRIVER,">driver.$$" || die ("cannot create driver.$$ file");
 print DRIVER "<!DOCTYPE TEI.2 SYSTEM \"tei2.dtd\" [\n";
 print DRIVER "<!ENTITY % TEI.$opt_base \"INCLUDE\">\n";
 foreach (@isoents) {
  my $pubId = "ISO 8879:1986//ENTITIES $_//EN";
  my $entity = $_;
  s/iso/iso-/;
  print DRIVER "<!ENTITY \% $entity SYSTEM  \"$_.ent\" >\n \%$entity;\n";
}
 foreach $i (@tagsets) {
  print DRIVER "<!ENTITY % TEI.$i \"INCLUDE\">\n";
 }
 if ($opt_xml || $opt_xmlpe) {
   print DRIVER "<!ENTITY % TEI.XML \"INCLUDE\">\n";
  }
 if ($opt_ext) {
  #---------------------------------------------------------
  # tinker with extensions file if XML requested
  if ($opt_xml || $opt_xmlpe) {

  open(OUT, ">teient.$$") || die "Could not open teient.$$";
  # now put the user entities back in
  open(IN, "$opt_ext.ent");
  while (<IN>) {   print OUT; }
  close IN;
  close OUT;
  open(OUT, ">teidtd.$$") || die "Could not open teidtd.$$";
  # put the user dtd in
  open(IN, "$opt_ext.dtd");
  while (<IN>) {   print OUT; }
  close IN;
  close OUT;
  $entityfile="teient.$$";
  $dtdfile="teidtd.$$"; 
 } 
   else
  {
 $entityfile="$opt_ext.ent"; 
 $dtdfile="$opt_ext.dtd"; 
   }
  print DRIVER "<!ENTITY % TEI.extensions.ent SYSTEM \"$entityfile\">\n";
  print DRIVER "<!ENTITY % TEI.extensions.dtd SYSTEM \"$dtdfile\">\n";
}
print DRIVER "]>\n";
close DRIVER;
}


if ($opt_verbose) { print "running carthage with driver $opt_driver \n"; }

open(OLDOUT, ">&STDOUT");   
$ENV{SGML_PATH}="%S:$opt_teidir/%S";
$ENV{DPP_PATH}=":.:$opt_teidir:$opt_isodir";
open(STDOUT, ">teitemp.$$") || die "Can't redirect stdout to $teitemp.$$";
open JOB,"|carthago --undeclared $opt_carthageopts --output carthago.$$";
open DRIVER, "$opt_driver";
while (<DRIVER>) { print JOB; }
close DRIVER;
close JOB; 
close STDOUT;
open(STDOUT, ">$opt_result") || die "Can't redirect stdout to $opt_result";
print STDOUT "<!-- TEI DTD  generated on $SysYear $SysMon $SysWday -->\n";
print STDOUT "\n<!-- carthage options $XMLFLAGS  -->\n";
print STDOUT "\n<!-- Input to carthage was:\n";
# open DRIVER, "$opt_driver";
#  while (<DRIVER>) { print STDOUT "   $_"; }
# close DRIVER;
print STDOUT "-->\n";
open JOB, "| carthago $XMLFLAGS $opt_carthageopts  --delenda carthago.$$  ";
 open DRIVER, "$opt_driver";
  while (<DRIVER>) { print JOB ; }
 close DRIVER;
close JOB;
close STDOUT;
open(STDOUT, ">&OLDOUT");
if ($opt_verbose) { print "carthage complete\n"; }
if (!$opt_debug) {
 unlink "driver.$$"; 
 unlink "teient.$$"; 
 unlink "teidtd.$$"; 
 unlink "teitemp.$$";
 unlink "carthago.$$";
 }
