#!/usr/bin/perl -W


use strict;
use English;

my $file;

print "argv @ARGV\n";
($#ARGV<2)
  and die "Usage: reg orig_pattern future_pattern file_list\n";

my $orig=shift;
my $future=shift;
foreach $file (@ARGV){
  
  # read file
  open (IN,"<$file") || die ("can't open file $file - $!");
  my @out_tmp=(<IN>);
  close (IN);
  my $out=join('',@out_tmp);

  #try replacing the orig with the future pattern
  if ($out=~s/$orig/$future/g){
    #upon success, write future file down
    open(OUT,">$file.tmp") || die ("can't open tmp file $file.tmp - $!");
    print OUT $out;
    close(OUT);
    
    system("diff $file $file.tmp");
    
    print "Do you want to keep these changes? (y or Y for yes)\n";
    my $answer=<STDIN>;
    print "you replied $answer\n";
    if ($answer=~/^y\n?/i){
      print "copying $file.tmp on $file\n";
      system "mv $file.tmp $file";
    }
  }else{
    print "No Changes for $file\n";
  }
}
