#!/usr/bin/perl

#    rename-special-chars.pl - Remove non-US-ASCII and FAT32 reserved
#    characters from the filenames of all files under the current
#    directory.  This makes such files suitable for copying to, for
#    example, an iAudio X5 USB digital audio player.
#
#    Copyright Peter Oliver, 2005-12-04.
#
#    This program is free software; you can redistribute it and/or
#    modify it under the terms of the GNU General Public License,
#    version 2, as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    http://www.gnu.org/copyleft/gpl.html

use warnings;
use strict;
use File::Find;
use Text::Unidecode;
use locale;

finddepth( \&callback, '.' );

sub callback {
    return if $_ eq '.';
    my $initial = $_;

    unidecode( $_ );

    tr/<>:\"\\|\?\*\//();\'!!.%!/;
    s/[\. ]+$//;

    if ( $initial ne $_ ) {
	my $dir = chdir $File::Find::dir;
 	print STDERR "Renaming '$initial' to '$_'";
  	rename( $initial, $_ ) or die ": $!";
	print STDERR "\n";
	chdir $dir;
    }
}
