#! /usr/bin/perl
#############################################################################
# 
#############################################################################
#  
#   GLOBAL VARIABLES {
#
#############################################################################
 
 
$dirname = ".";
$subdir = "false";
$rec="nop";
$source=$ARGV[0];
$copyas=$ARGV[1];
$rec=$ARGV[2];

   
if (($source eq "") || ($copyas eq "") || ($copyas eq "-r" ) ) {
    print"\n USAGE: \n\tabsolute.pl   current_string  source_copyas_string extension [-r]. You may select  ALL for extention\n\n";
    exit();
}
if (  $rec eq "-r")
{$subdir="true";}

print "Replacing $source with $copyas \n";
    
handledir($dirname,0);



sub handledir() {
local($dir,$l)=@_;
print "$dir\n";
opendir(DIR, $dir) || die "no dir .";
foreach $fn ( readdir(DIR)) {
	#if ($l >= 5) {continue;}

	if ($fn eq ".") { next;}
	elsif ($fn eq "..") { next;}
	

	$fn=$dir."/".$fn;

	if (-d $fn) {
	        if ( $x =readlink($fn) ) {
		    print "SKIPPING THE LINK at $fn --> $x \n";
		    next;
		}
		$l+=1;
		if ($subdir eq "true") {
		    handledir($fn,$l);
		}
		$l-=1;
	} else {
	   
	    if ($fn =~ /.$source/ && !($fn =~ /.$source./)){
		handlefile($fn,$l);
		#print "$fn   $source \n";
	    }
	}
}
}

sub handlefile(){
local($file,$l)=@_;
#$margin="";
#for ($i=0;$i<$l;$i++){
#   	$margin.=" ";
#}
#print "$margin $file \n";
$copy=$file.".".$copyas;
print "$file ---(c)---> $copy \n";
system("cp $file $copy");


}
