#!/usr/bin/perl
# usage: e2u /folder
# /folder without the last /

# script for replacing " " and %?? with "_" and "--" with "_-_"

# written 01/27/1999 by Knut Grahlmann <Knut.Grahlmann@bigfoot.com>
# published under the terms of the GPL

# takes the path
$path=$ARGV[0];


# checks if the commandline parameter is present
if ($path eq undef) {
	die("usage: e2u /folder\n       omit the last / in /folder");
}

# this way, the user has it easier, he only needs to type /folder and not /folder/
$path.="/";

# opens the specified folder to read the names of the files in it
opendir(MP3,$path) || die "Could not open $path: $!";
foreach $file (sort readdir(MP3)) {

	# makes the replacing (the s///-stuff) lots easier because it looks by
	# default into $_
	$_=$file;
	do {
		s/ /_/;
		s/%[0-9]+/_/;
		s/--/_-_/;
		s/_[0-9]+/_/;
	} while(/ / || /%[0-9]+/ || /--/ || /_[0-9]+/);
	if ($_ ne $file) {
		print($path.$file," ",$path.$_,"\n");
		rename($path.$file,$path.$_);
	}
	else {
		print($path.$file," didn't need to be renamed.\n");
	}
}
closedir(MP3);
print("Have a nice day...\n");
