#!/usr/bin/perl -w


BEGIN {
    die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
    unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
}


use File::Basename;
use unicode;

my %classifications = (
		       'Pharmacokinetics' => ['Absorption', 'Distribution', 'Metabolism', 'Elimination',
					       'Effect of Food', 'Drug Interactions', 'Special Populations'],
		       'Pharmacodynamics' => ['Trace Metal', 'Excretion', 'Selectivity', 'Copper', 'Zinc',
					      'Manganese', 'Iron', 'Distribution', 'Tissue', 'Brain',
					      'Heart', 'Plasma', 'Fetal', 'Liver', 'Kidney'],
		       'Safety/Side Effects/Toxicity' => ['Acute Toxicity', 'Repeated Dose Toxicity',
							 'Studies in Rodents', 'Studies in Dogs',
							 'Overdosage', 'Reproductive Toxicity',
							 'Teratogenicity', 'Mutagenecity', 'Carcinogenicity'],
		       'Other' => ['Superoxide Dismutase', 'Ferroxidase I', 'Ferroxidase II', 
				   'Metallothienein', 'Ceruloplasmin'],
		       'Efficacy' => ['Heart', 'Complications', 'Microvascular', 'Renal', 'Diabetes'],
		       'Contraindications' => [],
		       'Precautions' => []
		       );

my @meta_files = ();

&recursive_rename("import");
# process meta files
foreach my $mfile (@meta_files) {
    &process_meta_file($mfile);
}

sub recursive_rename {
    my ($dir) = @_;

    opendir (DIR, "$dir") || die;
    my @files = readdir DIR;
    closedir DIR;
    
    foreach $file (@files) {
	next if $file eq "." || $file eq "..";
	next if $file =~ /processed.htm$/;

	my $path = "$dir/$file";
	if (-d $path) {
	    &recursive_rename($path);
	} else {
	    $newfile = &rename_file($file);
	    if ($newfile ne $file) {
		print STDERR "renaming $file --> $newfile\n";
		`mv "$path" "$dir/$newfile"`;
	    }

	    if ($file =~ /^title\.txt$/) {
		# we'll process all the title.txt files after we've finished 
		# renaming everything
		push(@meta_files, "$dir/$newfile");
	    }

	    if ($file =~ /\.html?$/i) {
		&process_html_file("$dir/$newfile");
	    }
	}
    }
}

# clean up the html (currently just use the non-css version)
sub process_html_file {
    my ($filename) = @_;

    print STDERR "processing $filename\n";

    open (FILE, $filename) || die;
    undef $/;
    my $file = <FILE>;
    $/ = "\n";
    close FILE;

    my $header = "<html>\n<head></head>\n<body bgcolor=\"#FFFFFF\">\n";
    my $footer = "</body>\n</html>\n";

    my ($noncss, $css) = $file =~ /document\.write\(\"(.*?[^\\])\"\).*?document\.write\(\"(.*?[^\\])\"\)/si;

    # remove backslashes added for javascript strings
    $noncss =~ s/\\\"/\"/sg;
    $noncss =~ s/\\\n/\n/sg;

    # Occurances of 2HCl (lowercase el) were mistakenly changed to 2HC1
    # (digit one) during OCR in some cases so we'll change them back
    $noncss =~ s/2HC1/2HCl/g;

    # alter <img> tags to support renamed files
    $noncss =~ s/(<img src=\")([^\"]+)/$1 . &rename_file($2)/sige;

    # remove rules="all" attribute from table tags
    $noncss =~ s/(<table.*?)rules=\"all\"\s+/$1/sig;

    # remove empty <div> tags 
    while ($noncss =~ s/(<div[^>]*>\s*<\/div>)//sig) {}

    # remove empty table rows
    $noncss =~ s/(<tr[^>]*>(\s*<td[^>]*>\s*<\/td>)*\s*<\/tr>)//sig;
    $noncss =~ s/<table[^>]*>\s*<\/table>//sig;

    open (FILE, ">$filename") || die;
    print FILE $header . $noncss . $footer;
    close FILE;

}

sub rename_file {
    my ($filename) = @_;

    $filename =~ s/\s+//g;
    $filename =~ s/^protemix\(\d+\)-?//;
    $filename =~ s/^OCR\d+-0//;

    $filename =~ s/^(\d+)-\d+\.pdf$/$1-all.pdf/;

    return $filename;
}

# process a title.txt file and replace it with a meta.xml file
sub process_meta_file {
    my ($filename) = @_;

    open (FILE, $filename) || die ("couldn't open $filename");
    undef $/;
    my $title = <FILE>;
    $/ = "\n";
    close FILE;

    unlink($filename);

    $title =~ s/\s+/ /gs;
    $title =~ s/^\s+//;
    $title =~ s/\s+$//;
    $title =~ s/\"/&quot;/g;
    $title =~ s/</&lt;/g;
    $title =~ s/>/&gt;/g;
    $metafile =~ s/&/&amp;/g;
    $title = &unicode::ascii2utf8(\$title); # assumes title is iso-8859-1
    my $metafile = "<Metafile>\n";
    $metafile .= "  <Metadata name=\"Title\">$title</Metadata>\n";

    # currently just write some random values for classification metadata
    my @c1 = ('Animal', 'Human', 'Other');
    my $rand = int(rand 3);
    my $classtext = "    <Metadata name=\"Class1\">" . $c1[$rand] . "</Metadata>\n";
    my @c2 = ('Pharmacokinetics', 'Pharmacodynamics', 'Safety/Side Effects/Toxicity',
	      'Other', 'Efficacy', 'Contraindications', 'Precautions');
    $rand = int(rand 7);
    my $c2val = $c2[$rand];
    $classtext .= "    <Metadata name=\"Class2\">$c2val</Metadata>\n";
    if (defined $classifications{$c2val}) {
	if (scalar(@{$classifications{$c2val}})) {
	    my $numvals = scalar(@{$classifications{$c2val}});
	    $rand = int(rand $numvals);
	    my $c3val = $classifications{$c2val}->[$rand];
	    $classtext .= "    <Metadata name=\"Class3\">$c3val</Metadata>\n";
	}
    } else {
	print STDERR "ERROR: '$c2val' not in clasifications list\n";
    }

    my $dir = File::Basename::dirname($filename);

    opendir(DIR, $dir) || die;
    my @files = readdir DIR;
    foreach my $file (@files) {
	if ($file =~ s/\.html?$//i) {
	    $metafile .= "  <Page filename=\"$file\">\n$classtext  </Page>\n";
	}
    }
    $metafile .= "</Metafile>\n";

    $filename = "$dir/meta.xml";
    open (FILE, ">$filename") || die;
    print FILE $metafile;
    close FILE;
}


