#! /usr/bin/perl
#############################################################################
# 
#############################################################################
#  
#   GLOBAL VARIABLES {
#
#############################################################################
 
 
$dirname = ".";
$subdir = "true";

$replace=$ARGV[0];
$by=$ARGV[1];
$extention=$ARGV[2];

   
if (($replace eq "") || ($by eq "") || ($extention eq "" ) ) {
    print"\n USAGE: \n\tabsolute.pl   current_string  replace_by_string extension . You may select  ALL for extention\n\n";
    exit();
}
print "Replacing $replace with $by \n";
    
handledir($dirname,0);
system ("chmod -R 775 *");
system ("chgrp cpsedu  *");




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 ($extention eq "ALL" ) {;}
	    elsif (!($fn =~ /.$extention\b/ )){next;}
	    handlefile($fn,$l);	
	}
}
}

sub handlefile(){
local($file,$l)=@_;
#$margin="";
#for ($i=0;$i<$l;$i++){
#   	$margin.=" ";
#}
#print "$margin $file \n";
print "$file \n";

 open(IN, "<$file"); 
$of=$file."___";
 open(OUT, ">$of"); 

	 while (<IN>) {
		$line=$_;
		$line=~ s/$replace/$by/g;
	 	print OUT $line;
	}
 close(IN);
 close(OUT);

 rename($of,$file);

}
