# Logging section

package logger;

sub init {
    $MemoryLog = "/usr/tmp/$ARGV[1]/memory";
    $Index = "/usr/tmp/$ARGV[1]/index";
}

sub main'log_visit {
    local($url, $title, $headers, $beginning, $types, $#local_links,
	  $#ancestors, $markup) = @_;

	### Write some substantial code...
	&write_memory_log($url);
#	&write_index($url, $title, $headers, $beginning, $types,
#		     $#local_links, $#ancestors, $markup);
	exit;
}

sub write_memory_log {
    local($writeme) = @_;

    open(MLOG, ">>$MemoryLog");
    print MLOG $writeme, "\n";
    close(MLOG);
}

sub write_index {
    local($url, $title, $headers, $beginning, $types, $#local_links,
	  $#ancestors, $markup) = @_;

    open(IND, ">>$Index");
    print(IND "URL: $urlTitle: $titleHeaders: $headers$beginning");
    print(IND "$types$#local_links$#ancestors$markup\n");
    close(IND);
}

sub been_there {
    local($checkme) = @_;

    &queue'mygrep($checkme, $MemoryLog);
}

sub main'badlink {
    local($msg, $url, @ancestors) = @_;
    $ancs = join("\n", @ancestors);
    $ancsp = join(" ", @ancestors);
#    open(ZPH, "|zwrite -d -n -q $ENV{'USER'}");
#    print(ZPH "Error type _ $msg _ for $url -----$ancs\n-----\n\n");
#    close(ZPH);
    
    open(ERRLOG, ">>/usr/tmp/$ARGV[1]/errlog");
	if($msg =~ /[a-zA-Z]/){
	    print ERRLOG "BADPROTOCOL: $url ($ancsp)\n";
	    $errors{$ancestors[0]} .= "BADPROTOCOL: $url ($ancsp)\n";
	}
	elsif(int($msg) == 0){
	    print ERRLOG "UNREACHABLE: $url ($ancsp)\n";
	    $errors{$ancestors[0]} .= "UNREACHABLE: $url ($ancsp)\n";
	}
	else{
	    print ERRLOG "ERRORNUM$msg: $url ($ancsp)\n";
	    $errors{$ancestors[0]} .= "ERRORNUM$msg: $url ($ancsp)\n";
	}
	if(($n = ($errors{$ancestors[0]} =~ s/\n/\n/g))>8){
	    &mail_errors($ancestors[0]);
	}

    close(ERRLOG);
}

sub mail_errors {
    local($from) = @_;
    return unless $ErrorsTo{$from};
    open(MAIL, "|/usr/lib/sendmail -fintegrity-wanderer $ErrorsTo{$from}");
    print("Subject: Some Errors\n\n");
    print $errors{$from};
    close(MAIL);
    $errors{$from} = "";
    
}
1;
