#!/arch/unix/bin/perl
#rename.pl
#1.1
#
#(c)Josh Abraham
#Decemeber 1, 2004
#script that renames all the given files with a given extension
#to another extension

# touched up by Andrew Medico


use strict;
use warnings;

use File::Copy;

my $location;
my $oldext;
my $newext;

if (@ARGV == 2 or @ARGV == 3) {
    $oldext = $ARGV[0];
    $newext = $ARGV[1];
    $location = defined $ARGV[2] ? $ARGV : "./";
} elsif (@ARGV == 0) {
    print "Please enter the location: ";
    $location = <STDIN>;
    chomp($location);

    print "Please enter the old extension: ";
    $oldext = <STDIN>;
    chomp($oldext);

    print "Enter new extension: ";
    $newext = <STDIN>;
    chomp($newext);
} else {
	die "Usage: $0 [oldext newext [directory]]\n";
}

opendir(DIR, $location);
for (readdir(DIR)) {
    if (/$oldext$/) {
	my $new = $_;
	$new =~ s/$oldext$/$newext/;
        warn "Renaming file $_ to $new \n";
   if (! -e $new){
       move ($_, $new);
   }
   else{
       warn "File $new already exists ";
       next;
   }
}
closedir(DIR);

