#!/usr/bin/perl
#
# ::copy::
# ::maintainer::
#
use strict;

my $f_doit	= 1;
my $artist	= "";
my $file	= "";
my $sub_re	= "";
my $sub_str	= "";
my @files;
my @remove;
my $DEBUG	= 0;

usage()	if (!@ARGV);

while (@ARGV) {
	$_	= shift(@ARGV);
	my $arg2;
	my $arg3;
	$arg2	= $ARGV[0] if ($ARGV[0] !~ /^-/);
	$arg3	= $ARGV[1] if ($ARGV[1] !~ /^-/);

	CASE: {
	    	if ($_ eq "-D") {
			$DEBUG	= 1;
			last CASE;
		}
	    	if ($_ eq "-a") {
			usage( "-a requires an argument" )	if (!$arg2);
			$artist	= $arg2;
			shift(@ARGV);
			last CASE;
	    	}
		if ($_ eq "-r") {
			usage( "-r requires an argument" )	if (!$arg2);
			push( @remove, $arg2 );
			shift(@ARGV);
			last CASE;
		}
		if ($_ eq "-s") {
			usage( "-s requires 2 arguments" )	if (!$arg2 && !$arg3);
			$sub_re		= $arg2;
			$sub_str	= $arg3;
			shift(@ARGV);
			shift(@ARGV);
			last CASE;
		}
	    	if ($_ eq "-n") {
	    		$f_doit	= 0;
			last CASE;
		}
		usage()	if ($_ =~ /^-/);
		push( @files, $_ );
	}
}

usage()	if (!@files);

$artist	= lc($artist);


for $file (@files) {
	my $new	= lc($file);
	my ($c, $n);

	for $_ (@remove) {
		$new	=~ s/$_//;
	}

	$new	=~ s/^chk //;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/_/ /g;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/ \~ / - /;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/   / - /g;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/ -\s+- / - /g;	print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/  +/ /g;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/  *-  */ - /g;	print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/ +\././;		print "D1> -> '$new'\n"	if ($DEBUG);
	$new	=~ s/\212/\350/g;	print "D1> -> '$new'\n"	if ($DEBUG);

	if ($sub_re) {
		$new	=~ s/$sub_re/$sub_str/g;
		print "Ds> -> '$new'\n"	if ($DEBUG);
	}

	if ($artist && $new !~ /^$artist - /) {
		$new = "$artist - $new";
		print "Da> -> '$new'\n"	if ($DEBUG);
	}

	#	- lowercases all
	#	- capitalize to first "-" (artist name)
	#	- capitalize each first word before "-"
	#
	my $artist_done	= 0;
	my $k		= 2;
	my $old		= $new;

	# first 2 chars ...
	#
	$new	= uc( substr( $old, 0, 1 ) ) . substr( $old, 1, 1 );

	while ($k < length($old)) {

		$c	= substr( $old, $k, 1 );
		$n	= substr( $old, $k+1, 1 );
		$n	= ""	if (!defined $n);
		####print "k=$k, c='$c', n='$n', evaluating ...\n";

		if ($c eq " " && !$artist_done) {
			$new		.= " ";
			$k		++;
			$c		= substr( $old, $k, 1 );
			if ($c ne "-") {
				###print "k=$k, c='$c', not '-', uccing & next\n";
				$new	.= uc( substr( $old, $k, 1 ) );
				$k	++;
				next;
			}
		}

		if ($c eq "-" && $n eq " ") {
			$artist_done	= 1;
			###print "k=$k, c='$c', k+2 ...\n";
			$new	.= "- ";
			$k	+= 2;
			$c	= uc(substr( $old, $k, 1 ));
			$new	.= $c;
			###print "k=$k, c='$c', k++, next\n";
			$k	++;
			next;
		}
		
		###print "k=$k, c='$c', adding ...\n";
		$new	.= $c;
		$k	++;
	}
	print "Du> -> '$new'\n"	if ($DEBUG);


	###print "$new\n";
	
	#	known words ...
	#
	$new	=~ s/Mtv/MTV/g;
	$new	=~ s/ i / I /;
	$new	=~ s/ ii / II /;
	$new	=~ s/ iii / III /;
	$new	=~ s/ iv / IV /;
	$new	=~ s/ v / V /;
	$new	=~ s/ vi / VI /;
	$new	=~ s/ vii / VII /;
	$new	=~ s/ viii / VIII /;
	$new	=~ s/ ix / IX /;
	$new	=~ s/ x / X /;
	$new	=~ s/^Mcr / MCR/;
	$new	=~ s/egitto/Egitto/;

	# just to fix ...
	if ($f_doit) {
		system( "chmod", "-x", $file );
	}

	if ($new ne $file) {
		print "$new\n";
		if ($f_doit) {
			system( "mv", $file, $new );
		}
	}
}

exit(0);





sub usage
{
	print STDERR "\n", @_, "\n"	if (@_);
	die "
usage: mp3rename [options] dirs/files

options:

  -n		don't execute
  -a artist	prepend artist name to songnames
  -r string	remove string from names (multiple -r allowed)
  -s re str	subst regular expr 're' with string 'str'

\n";
}
