#!/usr/local/bin/perl
#
#
# A perl script for the flatening of DTDs.
#
# A slight modification of a script due to A.Saremba (http://www.saremba.de)
#
#

scalar(@ARGV) >= 3 or
  die "Usage: $0 dtd-filename target-filename dummydoc";
($dtd,$flat,$dummydoc) = @ARGV;

die "$dtd??  "     unless -e $dtd;
die "$dummydoc ??" unless -e $dummydoc;
if (-f $flat) { unlink $flat };

$tmpfile="flat.temp";
$command = "spam -p -p -w xml -w sgmldecl -w undefined -m ms -m shortref " . $dummydoc . " > " . $tmpfile;
print "$command\n";

system( $command ) ;

open DOC, "<$tmpfile";
while (<DOC>) {
    s{<!DOCTYPE[^[]*\[}{};
    $doc .= $_;
}
close DOC;
unlink $tmpfile;

$doc =~ s/]>.*//s;

open OUT, ">$flat";
print OUT $doc;
print "ready\n";
