#!/usr/bin/perl -w


use Encode;

sub debug_unicode_string
{
    join("",
         map { $_ > 127 ?                      # if wide character...
                   sprintf("\\x{%04X}", $_) :  # \x{...}
                   chr($_)          
               } unpack("U*", $_[0]));         # unpack Unicode characters
}


if (scalar(@ARGV)!=1) {
    my $prog_name = $0;
    $prog_name =~ s/^.*(\\|\/)//;

    print STDERR "Usage: $prog_name dir\n";
    exit(1);
}

my $dir = $ARGV[0];

opendir(DIN,"$dir") 
    || die "Unable to open $dir";

my @files = grep { $_ !~ m/^\./ } readdir(DIN);

close(DIN);

foreach my $f (@files) 
{
    print "Filename as is: $f\n";
    print "Filename debug: ", debug_unicode_string($f),"\n";

    my $dirsep = ($^O =~ /mswin/i) ? "\\" : "/";

    $f =~ s@$dirsep$@@;

    my $full_f = "$dir$dirsep$f";
    if ($^O =~ /mswin/i) {
	require Win32;
	$full_f = Win32::GetLongPathName($full_f);
	#$full_f = encode("UTF8",$full_f);
	print "Windows full filename:       ", $full_f,"\n";
	print "Windows full filename debug: ", debug_unicode_string($full_f),"\n";

	$full_f = Win32::GetShortPathName($full_f);
    }

    if ( -e $full_f ) {
	print "   Perl can see file $full_f\n";
    }
    else {
	print "   Perl CANNOT see $full_f\n";
    }
}


