package should;

sub main'should {
    local($url, @ancestors) = @_;
    local($should) = 1;
    print("*****Should($url, @ancestors)\n") if $main'debug;
    if(substr($url, rindex($url, '.')) =~ /(gif)|(jpg)/i){
	$should = 0;
	print("Picture\n") if $main'debug;
    }
    elsif(substr($url, 0, 4) !~ /http/i){
	$should = 0;
	print("Not HTTP\n") if $main'debug;

    }
    elsif($url =~ /#[^#]+$/){
	  $should = 0;
	  print("Hash reference\n") if $main'debug;
      }
    elsif(&logger'been_there($url)){
	$should = 0;
	print("Been there\n") if $main'debug;
    }
    elsif(&queue'on_queue($url)){
	$should = 0;
	print("On the queue ($url)\n") if $main'debug;
    }
    elsif(&on_kill_list(@ancestors, $url)){
	$should = 0;
	print("On kill list ($url)\n") if $main'debug;
    }
    elsif(($#ancestors >= $MaxAncestors{$ancestors[0]}) && $ancestors[0]){
	$should = 0;
	print("Depth exceeded ($url) ($#ancestors > $MaxAncestors{$ancestors[0]} of $ancestors[0])\n") if $main'debug;
    }
    elsif(&deep_branch($url, @ancestors)){
	$should = 0;
	print("Branch too deep ($url)\n") if $main'debug;
    }
    elsif($url eq ""){
	$should = 0;
	print("Null URL\n") if $main'debug;
    }
    $should;
}

sub deep_branch {
    local($u, @anc) = @_;

    return 0 if !$MaxBranch{$anc[0]};
    $maxbranch = $MaxBranch{$anc[0]};
    print("Max branch from $anc[0] is $maxbranch\n") if $main'debug;
    $bdepth=0;
    ($origin, $path, $oport) = &url'ParseURL($anc[0]);
    for $a (@anc){
	($site, $path, $port) = &url'ParseURL($a);
	if($site ne $origin || $port ne $oport){
	    print(join("-", @anc));
	    print("::$site is not $origin or $port is not $oport\n") if $main'debug;
	    $bdepth++;
	}
    }
    ($bdepth >= $maxbranch);
}

sub on_kill_list {
    local(@anc) = @_;

    @killthese = split("\n", $KillList{$anc[0]});
    for $killme (@killthese){
	if(grep($_ eq $killme, @anc)){
	    return 1;
	}
    }
    0;
}

sub main'load_config {

    $starturl = $ARGV[0];
    $MaxAncestors{$starturl} = 1;
    $starturl;

}

1;
