#!/Perl/5.00502/bin/MSWin32-x86-object/perl
########################################################################
# PWU: PW_Tools UpdateWeb 
########################################################################
# 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;

$VERBOSE=1;     # 0 is false, 1 is true
$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));
}


# ONE LINE PER FILE TO COPY

$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++;
if (! -d $WEB_ROOT)
{
    $VERBOSE=0;
}
	
    }
    else {
	if (/#.*/)  {
	    $READSTATE++;
	    $counter=1;
	}
	else { 
	    if ($READSTATE eq 2)
	    {
		($Source,$Destination) = split(/;/,$_);
		print "..FILES: $Source;$Destination\n" if $VERBOSE;
		@COPYFILE{$counter} = $Source;
                $PATHandFILE =  (length($Destination) eq 0 ? $Source : $Destination);
                $PATHandFILE =~ s/^.*\///;
		@COPYDEST{$counter} = $PATHandFILE;
		$counter++;
	    }
	    else {
		print "..DIRS: $_\n" if $VERBOSE;
		if (length($_)>0){@COPYDIR{$counter} = $_};
		$counter++;
	    }
	}
    }
}
close(FromFILE);


#########################################################################
# DO NOT EDIT ABOVE THIS LINE
#########################################################################

$filescopied = 0;

if (! -d $WEB_ROOT)
{
    print "\n\nError: Destination directory $WEB_ROOT not available!!\n";
    print "Please correct $InputFile or map/create $WEB_ROOT.\n\n";
}
else
{
copy_dir(%COPYDIR);
copy_file(%COPYFILE);
print "$filescopied files copied\n";
}
print "Press <RETURN> to continue ... ";
$dat = readline(*STDIN);


sub copy_dir {

    my %LOC_COPYDIR = @_;
    
    foreach $DIR (keys %LOC_COPYDIR){

	$DIR_TO_COPY = $LOC_COPYDIR{$DIR};
	print "Examine $DIR_TO_COPY\n";
	$path =  $DIR_TO_COPY;
	$path_begin = "";
	while ($path =~ /^([^\/]*)\/(.*)/){
	    $path_begin = "$path_begin/$1";
	    $real_path = "$WEB_ROOT$path_begin";       
	    if (!-d $real_path) {
		mkdir $real_path,755;
		print "mkdir $real_path\n";
	    };
	    $path=$2;
	}
	$path_begin = "$path_begin/$path";
	$real_path = "$WEB_ROOT$path_begin";       
	if (!-d $real_path) {
	    mkdir $real_path,755;
	    print "mkdir $real_path\n";
	};
	
	opendir(DIRHANDLE_A, "../../$DIR_TO_COPY");
	@FILES = grep(m/[^\.]/, readdir(DIRHANDLE_A));
	close (DIRHANDLE_A);

        $dircounter = 0;
        delete @PRE_DIR{keys %PRE_DIR};
	
	foreach $file (@FILES) {
	    
	    $source =  "../../$DIR_TO_COPY/$file";
	    $dest = "$WEB_ROOT/$DIR_TO_COPY/$file";
#	    if (((-M $source) < (-M $dest)) || (!-e $dest)){
	    if (-d $source) {
		@PRE_DIR{$dircounter++} = "$DIR_TO_COPY/$file";
	    }
	    else {
		if (((-M $source) < (-M $dest)) || (!-e $dest)){
		    print "copy to $dest\n" if $VERBOSE;
		    $filescopied++;
		    copy ($source,$dest);
		}	
	    }
	}
	copy_dir(%PRE_DIR);
    }
    return;
}

sub copy_file {
    
    my(%LOC_COPYFILE) = @_;

    foreach $FILE (keys %LOC_COPYFILE){
	
	$FILE_TO_COPY = $LOC_COPYFILE{$FILE};
	print "Examine $FILE_TO_COPY\n";

	$path =  $FILE_TO_COPY;
	$path_begin = "";
	while ($path =~ /^([^\/]*)\/(.*)/){
	    $path_begin = "$path_begin/$1";
	    $real_path = "$WEB_ROOT/$path_begin";       
	    if (!-d $real_path) {
		mkdir $real_path,755;
		print "mkdir $real_path\n";};
	    $path=$2;
	}
	
	$source =  "../../$FILE_TO_COPY";
	$dest = "$WEB_ROOT$path_begin/@COPYDEST{$FILE}";    
	
	if (((-M $source) < (-M $dest)) || (!-e $dest)){
	    print ".. copy to $dest\n" if $VERBOSE;
	    $filescopied++;
	    copy ($source,$dest);
	}
    }
    return;   
}


