#!/usr/bin/perl -w use strict; use Getopt::Std; my %opts; my $munge; my $ext = qr/mp3|ogg|nfo/; sub renamer { my $code = shift; return sub { for (@_) { eval $code; } } } getopts('fsla01xun:z:e:FDgqvc', \%opts); if ($opts{f}) { $munge = renamer 'musicfix();'; } if ($opts{s}) { $munge = renamer 'tr/_ /-_/;musicfix();'; } if ($opts{l}) { $munge = renamer 'tr/A-Z &()\'/a-z_+[]_/;s/%20/_/g;' } if ($opts{a}) { $munge = renamer 's/^([abcxypb]|aa)[12]?\.//;'; } if ($opts{0}) { $munge = renamer 's/^aa\./b./;s/^([a-z])\./${1}01./;s/^([a-z])(\d)\./${1}0${2}./;'; } if ($opts{1}) { $munge = renamer 's/^1/a/;s/^2/b/;s/^3/c/;'; } if ($opts{x}) { $munge = renamer 's/^x\./a01./;s/^y\./b01./;s/^xx\./c01./;s/^yy\./d01./;'; } if ($opts{u}) { $munge = renamer 'y/-/ /;s/\+/ + /g;s/\.(?!$ext)/ - /g;'; } if ($opts{n}) { $munge = renamer 's/\./.'.$opts{n}.'./;'; } if ($opts{z}) { $munge = renamer 's/(\d+)\.('.$opts{z}.')\.(.*?)\.feat.(.*?)\.($ext)/$1.$2+$4.$3.$5/;'; } if ($opts{e}) { $munge = renamer $opts{e}; } die 'no operation given' unless $munge; if ($opts{c}) { while () { &$munge($_); print; } exit; } foreach my $file (@ARGV) { unless (-e $file || $opts{g}) { warn "$file does not exist"; next; } my $newfile = do_munge($file); if ($newfile eq $file) { warn "$newfile was not affected" if $opts{v}; next; } if (-e $newfile && !$opts{F} && !$opts{g}) { warn "$newfile already exists"; next; } print "$file -> $newfile\n" unless $opts{q}; unless ($opts{g}) { rename $file, $newfile or die "can't rename: $!"; } } sub do_munge { my ($file) = @_; if ($opts{D}) { &$munge($file); return $file; } else { my @file_components = split m[/] => $file; &$munge($file_components[-1]); return join '/' => @file_components; } } sub musicfix { # put everything into lowercase, etc. tr/A-Z/a-z/; tr/(){}&:,?!'"/[][]+._/sd; s/($ext)\.[0-9]$/.$1/; # we want dashes for spaces and dots for separators. s/-/./g; s/\[/./g; s/\]/./g; #s!/!.!g; s/ /-/g; s/_/-/g; s/%20/-/g; # tracks with numbers may not have a proper separator # s/^([0-9]+)/$1./; # clean up the mess (pass 1) s/-\.-/./g; s/-\././g; s/\.-/./g; s/^\.+//; s/^-+//; s/\.+$//; s/-+$//; s/\.+/./g; s/-+/-/g; # deal with some tricky words correctly s/-featuring-/+/g; s/-feat[.-]/+/g; s/-ft[.-]/+/g; s/\.featuring-/.feat-/g; s/\.feat[.-]/.feat-/g; s/\.ft[.-]/.feat-/g; #s/-and-/+/g; s/-\+-/+/g; s/rmx/remix/g; s/-iii/-3/g; s/-ii/-2/g; # common filename cruft we can get rid of s/\.(?:wav|sour|rns|ms|sb|kw|pusy|bpm|lame|khz|xtc|rfl|trt|boss|atm|0db)\.($ext)$/.$1/; # s/\.(?:2001|2002)\.($ext)$/.$1/; # clean up the mess (pass 2) s/-\.-/./g; s/-\././g; s/\.-/./g; s/^\.\+//; s/^-+//; s/\.+$//; s/-+$//; s/\.+/./g; s/-+/-/g; # leading zero for tracks 0-9 s/(^\d\.)|(^\d$)/0$&/; }