#!/Perl/5.00502/bin/MSWin32-x86-object/perl
########################################################################
# PWC: PW_Tools CleanWeb     
########################################################################
# PW_Tools_Ver: 1.0, April 5 16:26 2001 by Peter Wieland
########################################################################
#
# Copyright 1999, 2000, 2001 Peter Wieland.
#
# This file is part of PW_Tools.
#
# PW_Tools is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PW_Tools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with PW_Tools; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
########################################################################
# CONFIGURATION, HAS TO BE EDITED ...
########################################################################
use File::Copy;

$HEADERLINE="PWC log file";

$VERBOSE=1;     # 0 is false, 1 is true

#########################################################################
# DO NOT EDIT ABOVE THIS LINE
#########################################################################
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    localtime(time);
$year += 1900;
$mon++;

$mytime = localtime;

$PROJECT="$ARGV[0]";


if (length($PROJECT) eq 0){
    opendir(DIRHANDLE_E,"../PWU/Input/" );
    my (@FILE) = grep(/\.txt/, readdir(DIRHANDLE_E));
    closedir(DIRHANDLE_E);
    print "Available project files in ./PW_Tools/PWU/Input/:\n\n";
    foreach $file (@FILE) {
        if (length($file) > 1) {
            $shortfile = $file;
            $shortfile =~s/.txt//;
	    print "  $shortfile\n";
	}
    }
    
    print "\nPlease enter project file to use:\n";
    chop ($PROJECT = readline(*STDIN));
}

$READSTATE = 0;

$InputFile="../PWU/Input/$PROJECT.txt";
open (FromFILE,"< $InputFile") or die 
    "Can't open InputFile: $InputFile";
while ( <FromFILE> ){
    chop;
    if ($READSTATE eq 0)
    {
	$WEB_ROOT = "$_";
	print "..Web_Root: $WEB_ROOT\n" if $VERBOSE;
	$READSTATE++;
	
    }
}
close(FromFILE);

## 
## READ COLOR INFORMATION -----------------------------------------------
##

$IndexFile="./clean_$PROJECT.html";
open (ToFILE,"> $IndexFile") or die 
    "Can't open InputFile: $InputFile";
print ToFILE"<a name=\"0\"></a><h1>$HEADERLINE</h1>\n";

$start_dir = "$WEB_ROOT";
$counter=100;

scan_dir($start_dir);

foreach $dire (%SCAN) {

    print " .$SCAN{$dire} . \n";

}

print ToFILE"<hr>Generated: $mytime\n";
close (ToFILE);

sub scan_dir{
    
    my ($LOC_DIR) = @_;
    
    opendir(DIRHANDLE_E,"$LOC_DIR" );
    my (@FILE) = grep(/[^.]$/, readdir(DIRHANDLE_E));
    closedir(DIRHANDLE_E);
    $acounter=$counter;


    print ToFILE"<nobr>[<a href =\"#0\">Top</a>]</nobr>\n";
#    print ToFILE"[<a href=\"#$last_index\">$file</a>]\n";
	
	
    foreach $file (@FILE) {
        if (length($file) > 1) {
	    $new_string = "$LOC_DIR/$file";
	    if (-d "$new_string"){
		print ToFILE"<nobr>[<a href =\"#$acounter\">$file</a>]</nobr>\n";
		$acounter++;
	    }
	}
    }
    print ToFILE"<p>\n";

    foreach $file (@FILE) {
	    $new_string = "$LOC_DIR/$file";
            $tmp_string = "$LOC_DIR/$file.tmp";
	    if ($file =~ /.htm/) {
		if ((-e "$new_string") && (!-d "$new_string")){
		    print "process .. $file\n\n";
		    print ToFILE"<b>$file</b><br>\n";
		    
                    open (TmpFILE,"> $new_string.tmp")or die 
			"Can't open InputFile: $InputFile";

		    open (FromFILE,"< $new_string") or die 
			"Can't open InputFile: $InputFile";
		    
		    while ( <FromFILE> ){
#			chop;
                        $FromLINE = $_;
			if (/<a href=\"([^\"]*)\"([^>]*)>(.*?)(?=<(\/a)\s*>)/i) {
			    $ref = "$1";
                            $refstr = "$3";
                            if (($ref =~ /^\.(.*)\.htm/) || (! (($ref =~ /http/i) || ($ref =~ /mailto/i) || ($ref =~ /#/i)))){

				if ((-e "$LOC_DIR/$ref") || (-d "$LOC_DIR/$ref")) {
				    print ToFILE"<a href=\"$ref\">($LOC_DIR/$ref)<\/a>$refstr<br>\n";
				}
				else
				{
#                                    print "$LOC_DIR/$ref not existing ... \n";
				    print ToFILE"CLEAN <a href=\"$ref\">($LOC_DIR/$ref)<\/a>$refstr<br>\n";
                                    $FromLINE =~ s/<a href=\"([^\"]*)\"([^>]*)>(.*)<\/a>/$3/i;
				}
			    }
		}
                        print TmpFILE"$FromLINE";
		    }
		    close (FromFILE);
                    close (TmpFILE);
		    move($tmp_string,$new_string);                    
		}	
	    }
	}

    
    foreach $file (@FILE) {
        if (length($file) > 0) {
	    $new_string = "$LOC_DIR/$file";
	    if (-d "$new_string"){
		print ToFILE"<a name=\"$counter\"></a><h2>$file</h2>\n";
		$counter++;
		print ToFILE"<ul>\n";
		@SCAN{$dircounter++} = "$new_string";
                my ($savecounter) = $counter;
                $counter = $acounter;
		scan_dir($new_string);
                $counter = $savecounter;
		print ToFILE"</ul>\n";
                $last_index=$counter-1;
                print ToFILE"<br>[<a href=\"#$last_index\">$file</a>]\n";
		print ToFILE"<nobr>[<a href =\"#0\">Top</a>]</nobr>\n";
	    }
	}
    }
   
    return;
}
