#!/usr/local/bin/perl

# Usage: rename perlexpr [files]

($op=shift) || Usage();

$test = 'undefined';
if ($op eq "-x"){
  print "-x detected...\n";
  $op=shift  || Usage();
  $test=0;
}
elsif ($op eq "-t"){
  print "-t detected...\n";
  $op=shift  || Usage();
  $test = 1;
}

print "Testmode!\n" unless !$test;
if (!@ARGV){
  @ARGV = <STDIN>;
  chop(@ARGV);
}

for (@ARGV) {
  $was=$_;
  eval $op;
  die $@ if $@;

if (!$test){
#  rename($was,$_) unless $was eq $_;  alte version, cookbook
  rename $was,$_ unless -f ;     #     neue Version Ben      
}
  print "$was \t ==> \t $_\n";
}


sub Usage{
print <<EOF;
Usage: rename.pl [-t|-x] perlexpr [files]

       renames each file according to the perl expression.
       The expression can acess the file name in $_ and it will
       be renamed to whatever $_ is after the evaluation of the expression.

       e.g.rename "s/before/after/" file_before_* 
       changes before to after in each filename. 
	. 
  switches: 
    -t (default) test the pattern (only print what would be done)
    -x           execute pattern on files (renaming them)
EOF

exit(1);
}

