# *-*-perl-*-*
eval 'exec perl -Ssw $0 "$@"'
    if 0;
#!/usr/local/bin/perl -sw
#
# get_microtheory.perl: retrive the assertions for a CYC microtheory
#

# Load in the common module, making sure the script dir is in the Perl lib path
BEGIN { my $dir = `dirname $0`; chomp $dir; unshift(@INC, $dir); }
require 'common.perl';

&init_var(*mt_info_file, "baseMt_refs.html");
&init_var(*base_link, "http://corinth.nmsu.edu:8000/cgi-bin/");
## &init_var(*dump_info, ($debug_level > 3));

if (!defined($ARGV[0])) {
    $options = "options = [mt_info_file=file]";
    $example = "ex: $script_name HumanPreferencesMt\n";
    $note = "note: mt_info_file can be generated by getting all refs of 
Predicate : baseMt\n";

    die "\nusage: $script_name [options] mt ...\n\n$options\n\n$example\n\n$note\n";
}


&read_mt_links($mt_info_file);

@microtheories = ();
if ($ARGV[0] eq "all") {
    @microtheories = sort(keys(%html_link));
}
else {
    @microtheories = @ARGV;
}

foreach $microtheory (@microtheories) {
    &debug_out(4, "retrieving assertions for microtheory $microtheory\n");

    $link = $html_link{$microtheory};
    if (!defined($link)) {
	&debug_out(1, "*** no link defined for $microtheory\n");
    }
    else {
	&display_assertions($microtheory, $link);
    }
}

#------------------------------------------------------------------------------

# read_mt_links(mt_info_file)
#
# Read html links from html file containing all the CYC Microtheories.
# Assumed to be the output of the cross-references for baseMt.
#
# example input (first in ascii, then in html):
#
# 0   [+](baseMt  ScenarioTestMt-9 HumanActivitiesMt)
# 0   [+](baseMt  ScenarioTestMt-9 BaseKB)
# 
# 0   <a href="cyc?c10-a&3872020">[+]</a><i>(<a href="cyc?c10-c&229">baseMt</a>
#     <a href="cyc?c10-c&934373">ScenarioTestMt-9</a> 
#     <a href="cyc?c10-c&32371">HumanActivitiesMt</a>)</i>
# 0   <a href="cyc?c10-a&7453233">[+]</a><i>(<a href="cyc?c10-c&229">baseMt</a>
#     <a href="cyc?c10-c&934373">ScenarioTestMt-9</a>
#     <a href="cyc?c10-c&273">BaseKB</a>)</i>
# 

sub read_mt_links {
    local($mt_info_file) = @_;
    &debug_out(4, "read_mt_links(@_)\n");

    open(MT_INFO, "< $mt_info_file")
	|| die "** Unable to open file w/ CYC microtheory links ($mt_info_file)\n";

    while(<MT_INFO>) {
	while (/baseMt<\/a> <a href=\"([^\"]+)\">([^<>]+)<\/a>/) {
	    $_ = $';
	    local($link) = $1; local($microtheory) = $2;
	    &debug_out(7, "adding link for $microtheory: $link\n");

	    if ($link !~ /^http:/) {
		$link = "${base_link}${link}";
	    }
	    $html_link{$microtheory} = $link;
	}
    }
    close(MT_INFO);

    return;
}


sub display_assertions {
    local ($microtheory, $link) = @_;
    &debug_out(4, "display_assertions($microtheory, $link)\n");

    # First, output the microtheory page to the file
    &issue_command("lynx -dump '$link' > $microtheory.text");

    # Extract additional links if needed
    # ex: <hr><a href="cyc?c10-ist&32371">[All 6227 assertions in 
    #     mt HumanActivitiesMt]</a>
    $mt_info = &run_command("lynx -source '$link'");
    if ($mt_info =~ /<a href=\"([^\"]+)\">.*all \d+ assertions/i) {
	$link2 = $1;

	if ($link2 !~ /^http:/) {
	    $link2 = "${base_link}${link2}";
	}
	&issue_command("lynx -dump '$link2' >> $microtheory.text");
    }

    return;
}
