#!/usr/bin/perl

if (!$ARGV[0]) {
	print "You need to give me an argument of some kind. Following this, \n";
	print "I will take the argument and remove it from the filename of   \n";
	print "every file in the vicinity. Very useful for mass renamings \n";
	print "If you give me 2 arguments I will use the second one to \n";
	print "replace the first one with it! \n";
	exit(0);
}

print "argv o is $ARGV[0]";
print "argv 1 is $ARGV[1]";
$DIRECTORYNAME=".";
if($ARGV[2]){
	print "Directory to alter is $ARGV[2]";
	$DIRECTORYNAME=$ARGV[2];
}
opendir(DIRHANDLE,$DIRECTORYNAME);
@foo=readdir(DIRHANDLE);
closedir(DIRHANDLE);
$tocompare=$ARGV[0];
$toreplace=$ARGV[1];
print " Tocompare element : $tocompare";

#print join (" \n ", @foo);
#print "\n Test \n";

$n=0;
foreach $element (@foo) {
	$tempelement="$n";
	if($element=~m/$tocompare/){
		$tempelement=$element;
		$tempelement=~s/$tocompare/$toreplace/g;
		print "\n rename  $element to  $tempelement \n";
		$command="mv $DIRECTORYNAME/\"$element\" $DIRECTORYNAME/\"$tempelement\"";
		print "command running will be: $command \n";
		`$command`;
	}
	$n++;
}
