#!/usr/bin/env perl

use strict;
use warnings;

use File::Find;
use Fcntl ':mode';

my $tstat = { file => 0, dir => 0, other => 0 };
my $sstat = {};
my ($mode, $size, $slot);

sub handler {
  ($mode, $size) = (stat $_)[2,7];

  if    (S_ISDIR($mode)) { $tstat->{dir}++   }
  elsif (S_ISREG($mode)) { $tstat->{file}++  }
  else                   { $tstat->{other}++ }

  if ($size >= (1024 ** 2)) {
    $sstat->{'>1024k'}++;
    return;
  }
  if ($size >= 1024 * 100) {
    $sstat->{'100k - 1024k'}++;
    return;
  }
  if ($size >= (1024 * 10)) {
    $sstat->{'10k - 100k'}++;
    return;
  }
  if ($size >= 1024) {
    $sstat->{'1k - 10k'}++;
    return;
  }
  if ($size < 1024) {
    $sstat->{'0k - 1k'}++;
    return;
  }
}

File::Find::find({wanted => \&handler}, 'ports/');

print "Stats for file types\n";
print "--------------------\n";
foreach my $key (sort keys %{ $tstat }) {
  printf " %7d | %s\n", $tstat->{$key}, $key;
}

print "Stats for file sizes\n";
print "--------------------\n";
foreach my $key (sort keys %{ $sstat }) {
  printf " %7d | %s\n", $sstat->{$key}, $key;
}

exit 0;
