#!/usr/bin/perl
#
# Find all filenames with bad character encoding
# and if you wish rename them
#
# v0.1 Tomas Pospisek <tpo@sourcepole.ch>
# 6.Oktober 2001

# Directory to restore (will be traversed with find)
$DIRECTORY = "/home/samba";

# Print processed filenames
$VERBOSE  = 1;

# Print processed filenames that are OK
#$VERBOSE_OK = 1;

# Print processed filenames that were successfully corrected
# in the form of:
# old_version
#   new_version
$VERBOSE_CORRECTED = 1;

# Print processed filenames that are not OK
# you should allways leave this on!
$VERBOSE_BAD = 1;

# execute the rename
#$DO_RENAME = 1;



##########################################################

if($DO_RENAME) {
  $RENAME ="D";
  go();
  $RENAME ="F";
  go();
}
else {
  $RENAME ="NONE";
  go();
}

sub go {
 open(FILELIST, "find $DIRECTORY|");
 
 while(<FILELIST>) {
   chomp;
   if( ! /^[\w|\s|ä|ü|ö|Ä|Ü|Ö|é|è|:|;|¡|!|,|.|'|´|`|~|\(|\)|\[|\]|\{|\}|\-|\+|%|=|@|&|\$|«|ø|½|\/]*$/ ) {
     if( /^[\w|\s|ä|ü|ö|Ä|Ü|Ö|é|è|:|;|¡|!|,|.|'|´|`|~|\(|\)|\[|\]|\{|\}|\-|\+|%|=|@|&|\$|«|ø|½|\/|\x81|\x84|\x94|\x8E|\x9A|\x99|\x82|\x8A]*$/ ) {
       my $old = $_;
       tr/\x84\x81\x94\x8E\x9A\x99\x82\x8A/äüöÄÜÖéè/;
       $new = $_;
       if ( $RENAME eq "D" && ( -d $old)) {
         # rename( $old, $new ) || print "Arg! $old \n";
       }
       elsif ( $RENAME eq "F" && ! -d $old) {
         # rename $old, $new;
       }
 
       if($VERBOSE && $VERBOSE_CORRECTED) {
         print "$old\n";
         print "  $_\n";
       }
     }
     else {
       if($VERBOSE && $VERBOSE_BAD) {
         print "$_\n";
       }
     }
   }
   else {
     if($VERBOSE && $VERBOSE_OK) {
       print "$_\n";
     }
   }
 }
} # end sub go
