use strict;
use String::CRC32;
use Getopt::Std;
use File::Basename;
my %opt;
getopt('d:s:e:r:', \%opt);
unless (defined $opt{'d'} and defined $opt{'s'} and defined $opt{'e'}) {
print "Usage: @{[ basename($0) ]} -d < /path/to a/directory >" .
" -s \"Title String\" -e \"ext\"\n" .
"Use -r < regex > for a custom number finding regex.\n";
exit 1;
}
my $gex = defined $opt{'r'} ? $opt{'r'} : '[^v](\d+)';
opendir(THERE, $opt{'d'}) or die "Unable to open working directory: $!\n";
my @files = readdir(THERE);
@files = @files[2..$#files];
closedir(THERE);
chdir $opt{'d'} or die "Unable to enter working directory: $!\n";
foreach my $file (sort @files) {
$file =~ /$gex/oi;
my $num = $1;
open(CRCFILE,$file) or warn("Unable to open file: $!"), next;
binmode(CRCFILE);
my $crc = sprintf '%x', crc32(*CRCFILE);
close(CRCFILE);
my $nfile = sprintf('%s - %02d [%s].%s',$opt{'s'},$num,uc($crc),$opt{'e'});
rename($file,$nfile);
print $nfile, "\n";
}