#!/PROGRA~1/NtResKit/Perl/bin/perl 

use strict ;

my $in = "./" ;
my $out = "c:/tmp/" ;
my ($i, $dir, $localdir, $copiedir, $file, @files) ;

$\= "\r\n" ;

if ($#ARGV < 0) { print "Give at least one argument" ; exit(0);}
$in = $ARGV[0] ;
if ($ARGV[1] ne "") { $out = $ARGV[1] ; }

@files = &LOOP_DIR ($in );

for ($i = 0; $i < $#files; $i++) {
#foreach $file (@files) {
  $dir = @files[$i++] ;
  $file = @files[$i] ;
  $localdir = $dir ;
  $localdir =~ s/$in// ;
  $copiedir = "$out/$localdir" ;
  print "l: ", $localdir, " / ", $file ;
  mkdir $copiedir, "drwxrwxrwx" ;
  &FILE ("$dir/$file", "$copiedir/$file" );
  $\= "\r\n" ;
  }



sub FILE
{
my $out = $_[1] ;

print "FILE: ", $_[0]," destination ", $out ;

open (FILE, "$_[0]") || die "ouverture de $_[0] ratee " ;

open (OUTFILE, ">$out") || die "Can not open $out : $!\n" ;

$/ = "\n" ;
$\= "\n" ;
while (<FILE>)
  {
  chomp($_) ;
  print OUTFILE $_ ;
  }
  
close (FILE) ;
close (OUTFILE) ;
}


sub LOOP_DIR
{
my $dir = $_[0] ;
my ($file, @files) ;
my $nb = 0 ;

opendir (DIR, $dir) || return ;
foreach $file (readdir(DIR))
  {
  my @pair=() ;
  my ($i, $j, $name, $file0, $file1, $file2) ;
  next if (($file eq ".") || ($file eq "..")) ;
  if ($file =~ /.+\.([ch]|(pl)|(txt)|(html))$/) { 
	push @files,$dir, $file; $nb++; }
  else { push @files, &LOOP_DIR ("$dir/$file", $out) ; }
  }
closedir (DIR) ;

print "Directory: $dir Nb files: ", $nb ;
return @files
}



