#!/usr/bin/perl -w
# v 0.02 - last change 28/07/00
# v 0.03 - last change 09/10/06
# nb this probably wants merging with t'other version elsewhere

use strict;
use POSIX 'getcwd';

my (@files, $cwd, $cwd2, $i, $x);

opendir(D,".") || die "Wah! Can't open .\n";
@files=grep(!/^\./,readdir(D));
closedir(D);

($cwd = getcwd() ) =~s#.*/([^/]+)$#$1#;
($cwd2 = $cwd) =~ (s#^(.*)_([Tt]he)#$2_$1#);

for my $x (@files) {
	if ($x=~/^[^A-Za-z]+\.mp3$/ || -d $x) {next}
	$i = $x;
	$i=~s/^[\d]+[\.\-\_]*//og;   # kill leading numbers and punctuation
	$i=~s/%20|\.|\ /_/og; # swap spaces periods and %20s for _
	$i=~s/[\'\`\(\)\.\,\[\]]//og;   # kill '`().
	$i=~s/\&/and/og;   # change & for and
	$i=~s/([_-]){2,}/$1/eg;
	$i=~s/^(.)/uc(substr($1,0,1))/e;
	$i=~s/_?mp([23])$/.mp$1/i;
	# take out the directory name (ie artist name) from the
	# mp3 filename and strip leading -_. and space chars
	$i=~s/^$cwd([- _])|^$cwd2([- _])|^[\_\-\.\ ]+//oig;
	# kludge for vfat fs
	if ( $i ne $x && -e $i ) { print "! $x -> $i } skipping  - $i seems to exist already\n"; } else {
		if ($x ne $i) {
			print "$x -> $i\n";
			rename $x, "RentempRen";
			rename "RentempRen", $i;
		}
	}
}
