#! /usr/bin/perl
## Web compare tool tree.pl
## Files: tree.pl socket.pl httpd.map
## In httpd.map containing the following line:
## URL Directory_Path_Mapping
## Usage: tree.pl Directory_compared DB_URL_logfile

$num = @ARGV ;
$tmpfile = "__t_mp_db.tmp";
$match_file = "httpd.map" ;

if ($num != 2) {
    print "Usage: tree.pl Directory DB_URL_logfile \n" ;
    exit ;
}

if (!open(MATCH, $match_file)) {
    print "Sorry, can't open file \"$match_file\". \n" ;
    print "It contains: URL_address Mappping_Directory\n" ;
    exit ;
}

$_ = <MATCH> ;
($HTTP_URL, $MAP_DIR) = split(/\s+/, $_) ;

#print $HTTP_URL, " ", $MAP_DIR, "\n" ;
#close(MATCH) ;

($host, $port, $path, $file,$extra) = &split_url($ARGV[1]) ;
#print $host, " ", $port, " ", $path, " ", $file, "\n" ;

require 'socket.pl' ;

open(FIND, "find $ARGV[0] -print |") || die "Couldn't run find: $!\n";

while ($filename = <FIND>) {
#    print $filename ;

    open(db_file, $tmpfile) || die "Couldn't open DB_file: $!\n" ;
    $Flag = 0 ;
    $_filename = $filename ;
    $_filename =~ s/$MAP_DIR// ;
    $_filename = $HTTP_URL . $_filename ;
#print "test .... ", $_filename ;
    while ($line = <db_file>) {
	if ($_filename eq $line) {
	    $Flag = 1;
	    last ;
	}
    }
    chop($filename) ;
#    print $filename,"\n" if -d $filename ;
    print $filename, "\n" unless ($Flag == 1 || -d $filename) ;
}
    unlink($tmpfile) ;

sub split_url {
    local($url)=@_;
    local($host,$port,$path,$file,$extra,$v);
    ($url,$extra)=$url=~m!^([^\?\#]*)(.*)$!;
    $v=$url=~m!^http:(//([^:/]*)(:(\d*))?)?((/([^/]+/)*)?([^/]*))?$!;
    return () unless $v;
    ($host,$port,$path,$file)=(($2?$2:"localhost"),($4?$4:80),$6,$8);
    ($host,$port,$path,$file,$extra);
}
