#!/usr/bin/perl
use strict;
use LEOCHARRE::CLI2 'b';
use LEOCHARRE::Dir ':all';
use Carp;
our $VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)/g;
$opt_d and $opt_b = 1;


@ARGV or die("Missing arguments.\n");
for (@ARGV){
   my $abs = Cwd::abs_path($_) or warn("Not dir on disk: '$_'\n") and next;
   onedir($abs);
}

exit;




sub onedir {
   my $abs_d = shift;
   debug($abs_d);

   my @files = lsfa($abs_d);



   my $count = scalar @files || 0;
   my $du = _dirsize($abs_d);

   my $avg;

   if($du and $count){
      $avg = int ($du / $count );
   }
   else {
      $avg = 0;
   }

   debug("total files: $count");
   debug("total k: $du");
   debug("average : $avg");

    
   if ($opt_b){
      printf "%10sk %6s %s\n", $avg, $count, $abs_d;
   }
   else {
      print $avg."k\n";
   }

   


}


sub _dirsize {
   my $abs_d = shift;
   `du -Ss "$abs_d"`=~/^(\d+)/ ;# -S is do not do subdirs

   my $dirsize= $1;

   debug("$abs_d : '$dirsize'");
   $dirsize;
}



sub usage {
   qq{lsas [OPTION]... DIR..
Find average size of files in a DIR.

   -d       debug on
   -h       help
   -v       version and exit
   -b       verbose, show dir and count

Try 'man lsas' for more information.
}}



__END__

=pod

=head1 NAME

lsas - list average size of files in a directory

=head1 DESCRIPTION

List average size of files in a directory. Is not recursive.

=head1 USAGE

lsas [OPTION] DIR..

   -d       debug on
   -h       help
   -v       version and exit
   -b       verbose, show dir and count

=head2 EXAMPLE USAGE

   lsas ./
   lsas -b ./



=head1 SEE ALSO

lsutils - parent package

=head1 AUTHOR

Leo Charre leocharre at cpan dot org

=head1 LICENSE

This package is free software; you can redistribute it and/or modify it under the same terms as Perl itself, i.e., under the terms of the "Artistic License" or the "GNU General Public License".

=head1 DISCLAIMER

This package 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.

=cut
