#!/usr/bin/perl
#
# checklinks
#
# Verify correct HTML links in all documents

$checkForeignLinks = 0;
$urlget = "/usr/local/bin/url_get";


# $rootdir is a UNIX path leading to the encyclopedia root directory

(($rootdir) = (`pwd` =~ m:^(.*/Connected):))
    || die "This script must be run from within the Connected hierarchy\n";

chdir($rootdir);
$idxfile = "$rootdir/index";



open(IDX, "<$idxfile");
while (<IDX>) {
    if (m:\tLink\t:) {

	($baseurl, $type, $name, $target) = split(/\t/, $_);
	chop($target);

	$basedir = $baseurl;
	$basedir =~ s:[^/]*$::;

	while ($target =~ m:^../:) {
	    $basedir =~ s:[^/]*/$::;
	    $target =~ s:^../::;
	}

	if ($target =~ m!^.*:!) {
	    if ($target !~ m!^mailto:baccala@FreeSoft.org!) {
		if (! $checkForeignLinks ||
		    system("$urlget $target > /dev/null")/256 != 0) {
		    print "Full URL: $baseurl -> $target\n";
		}
	    }
	} else {
	    if ($target !~ m:^/:) {
		$target = $basedir . $target;
	    }
	    $filename = $target;
	    $filename =~ s:^/Connected/::;
	    print "$baseurl -> $target\n" if (! -r $filename);
	}
    }
}
