#!/usr/bin/perl -w

$path = $ARGV[0] // $ENV{HOME};
die "Not a directory" unless -d $path;
my @letters = split "", $path;
if ($letters[-1] ne "/"){
    $path .= "/";
}


my %hash;
ver($path);
for my $j (sort keys %hash){
    print "$j $hash{$j}{COUNT}\n";
    if (defined $hash{$j}{DESCRIPTION}) {
	print "$hash{$j}{DESCRIPTION}";
    }
}

sub ver{
    my $DIR = shift ;
#    print "DIR-> $DIR\n";              # Uncomment to debug
    opendir DIR, $DIR or warn "Cannot open $DIR";
    my @files = readdir DIR;
    closedir DIR;
    for my $i (@files){
	next if ($i=~/^\.$/ || $i=~/^\.\.$/);
	next if ($i =~ /[[:cntrl:]]/);
	next if ($i =~ /Belinda/);           # A small number of files
	next if ($i =~ / Die /);             # have the file name so rotten	
	next if ($i =~ /\[EMG\]/);           # that the command 'file' is unable
	next if ($i =~ /¿/);                 # to inspect, causing the program 
	next if ($i =~ /\.mpc/);             # to halt. This is terrific.
	next if ($i =~ /\~/);                # Thus, uncomment the lines to debug.
#	my $blackflag =0;
#	@letters = split // , $i;
#	for (@letters) {
#	    if (ord($_) < 32){
#		$blackflag =1;
#		last;
#	    }
#	}
#	next if $blackflag ==1;

#	print "$i\n";                    # Uncomment to debug
	if (-f $i) {
	    if ($i =~ m/\.([^.]+)$/){
		my $r = lc($1);
		
		$hash{$r}{COUNT}++;
		if ($hash{$r}{COUNT} == 1 ) {
		    unless ($r =~ /\(/ ){
			$hash{$r}{DESCRIPTION} = `/usr/bin/file \"$i\"`;
		    }
		}
	    }else{
		$hash{"w/o ext"}{COUNT}++; 
	    }
	}else{
	    unless (-l $i || $i =~ /wine/ || $i =~ /incoming/ || $i =~ /ystem/ || $i =~ /proc/) {
		my $newdir = $DIR.$i."/";
		if ( (chdir $newdir) ){
		    ver($newdir);
		}
	    }
	}
    }
}

							    