#! /usr/bin/perl
#############################################################################
# (a) Mehmet Sen
#############################################################################
# 
# ! /usr/bin/perl
##
## Check arguments.
##
sub check_args
{
    $replace=$by=-12576;
    if ( ! (@ARGV)) {
	print <<INLINE_LITERAL_TEXT;

        USAGE:  absolute.pl   -r current_string  -w replace_with_string [-d dir] [-nr] [-x extension] [-S]
	      nr : Non-recursive
	      x  : All extension types.
	      b  : process backups too; tar,gz,~    
	      d  : Working directory
              S  : SEARCH MODE, no update
INLINE_LITERAL_TEXT
	    exit(0);
    }
  while (@ARGV)
  {
      $arg = shift(@ARGV);
      $replace=shift(@ARGV),        next if $arg eq '-r';    ## 
      $by=shift(@ARGV),             next if $arg eq '-w';    ## 
      $extension=shift(@ARGV),       next if $arg eq '-x';   ## 
      $subdir=0,                next if $arg eq '-nr';   ## 
      $dirname=shift(@ARGV),        next if $arg eq '-d';   ## 
      $DONTTOUCHBACKUPS=0,      next if $arg eq '-b';    ## 
      $SEARCH=1,                 next if $arg eq '-S';    ## 
      last                          if $arg eq '-e';  ## end of options (in case a regex looks like an option).
  }
    if ( $replace == -12576 || $by==-12576){ exit(0);} # must parameters.
#print " $replace  $by  $extension  $rec  $DONTTOUCHBACKUPS ";
    if (!$SEARCH) {print " WARNING: WILL UPDATE FILES!\n Break or press enter to continue."; $dummmy=<STDIN>;}
}
#
#############################################################################
#  
#   GLOBAL VARIABLES 
#
#############################################################################
$SEARCH=0;
$dirname = ".";
$subdir = 1;
$extension='ALL';
$DONTTOUCHBACKUPS=1;
&check_args;
print " $replace  $by  $extension  $subdir   $DONTTOUCHBACKUPS ";   


print "Replacing $replace with $by in $dirname \n";
if ($DONTTOUCHBACKUPS) {
    print "SKIP  *~ \n" ;
    print "SKIP  *.default\n";
    print "SKIP  *.tar\n";
    print "SKIP  *.gz\n";
    print "SKIP  *.jar\n";
}
    
handledir($dirname,0);


#############################################################################
sub handledir() {
local($dir,$l)=@_;
#print "$dir\n";
opendir(DIR, $dir) || print "ERROR: CAN NOT OPEN DIRECTORY:  $dir !!!\n";
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) {
		    handledir($fn,$l);
		}
		$l-=1;
	} else {
	        if ( $x =readlink($fn) ) {
		    print "SKIPPING THE LINK at $fn --> $x \n";
		    next;
		}
		if ($extension eq "ALL" ) {;}
		elsif (!($fn =~ /\.$extension\b/ )){ next;}
		else{ print "$fn WARNING"; exit;}
		if ($DONTTOUCHBACKUPS) {
		    # print "$fn ? ";
		    if ( ($fn =~ /~/ )){ next;}
		    elsif ( ($fn =~ /\.default\b/ )){ next;}
		    elsif ( ($fn =~ /\.jar\b/ )){next;}
		    elsif ( ($fn =~ /\.tar\b/ )){next;}
		    elsif ( ($fn =~ /\.gz\b/ )){next;}
		}
		if (  searchfile($fn) )
		{handlefile($fn,$l);	}
	}
}
}

sub searchfile(){
    local($file)=@_;
    open(IN, "<$file"); 
    while (<IN>) {
	$line=$_;
	if ($line=~ /$replace/) { close(IN); print "$file "; return 1;}
    }
    close(IN);
    #print "$file CLEAR\n";
    return 0;
}

sub handlefile(){
local($file,$l)=@_;
    if ($SEARCH) {
	print "Return without handling \n";
	return;
    }
 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);
 print " OK \n";

}




