#!/usr/bin/perl -w
#
# Usage: rename perlexpr [files]
# http://www.evolt.org/article/Renaming_Files_with_Perl/17/351/

# Copyright 2013, Naoki Takebayashi <ntakebayashi@alaska.edu>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Version: 20230721

my $usage = "Usage: $0 perlexpr [files]\n" .
    "This script does NOT recursively go down into the directory\n";

our ($opt_h, $opt_n);
use Getopt::Std;
getopts('hn') || die "$usage\n";
die "$usage\n" if (defined($opt_h));

($regexp = shift @ARGV) || die "$usage";

if (!@ARGV) {
   @ARGV = <STDIN>;
   chomp(@ARGV);
}


foreach $_ (@ARGV) {
   $old_name = $_;
   eval $regexp;
   die $@ if $@;

   if (defined $opt_n) {
       print "$old_name => $_\n";
   } else {
       rename($old_name, $_) unless $old_name eq $_;
   }
}

exit(0);
