#!/usr/bin/perl

use strict;
use File::Slurp qw(slurp);
use File::Find::Rule;
use List::Util qw(sum);

my @webroots = @ARGV or die "Usage: $0 webroot [webroot2 webroot3 ...]";
my %totals;

# Modify this to match your file naming conventions
my $match = File::Find::Rule->new
                            ->file
                            ->nonempty
                            ->name( '*.html', '*.epl', '*.ep' );

# Don't traverse into version control directories
my $ignore = File::Find::Rule->new
                             ->directory
                             ->name( '.svn', 'CVS' )
                             ->prune
                             ->discard;

foreach my $file ( File::Find::Rule->or( $ignore, $match )
                                   ->in( @webroots ) ) {
  # warn, not die, on errors
  my $content = slurp( $file, err_mode => 'carp' );
  next unless defined $content;

  my $count = 0;
  while ( $content =~ m/(\[([-*+!\$]).*?\2\])/gs ) {
    # use the following if you want to debug the set of matches in a file
    #push(@matches, scalar( split($/,$1) ), $1) &&
    #  ($count += $matches[-2]) ;
    $count += scalar( split($/,$1) );
  }

  $totals{$file} = $count;

}

exit unless %totals;

my @sorted = sort { $totals{$a} <=> $totals{$b} } keys %totals;

printf( "%10d %s\n", $totals{$_}, $_) foreach @sorted;

printf("
Totals:
  Files : %d
  Lines : %d

", scalar(@sorted), sum(values %totals) );

