#!/usr/bin/env perl
#
# Copyright 2014, Anaseto (anaseto@bardinflor.perso.aquilenet.fr).
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
# with modifications from Kaoseto
#
# This script counts and compares the number of paragraphs in frundis files
#

sub slurp {
    my $file = shift;
    open(my $fh, '<', $file) or die;
    local $/;
    my $text = <$fh>;
    close $fh;
    return $text;
}

sub stats {
    my $file = shift;

    my $text = slurp($file);
    $text =~ s/\.\\".*//g;
    my @chapters_names;
    while ($text =~ /^\.\h*Ch\h+(?:-nonum\h+)?(.*)/mg) {
        push @chapters_names, $1;
    }
    my @chapters = split /^\.\h*Ch\h/m, $text;
    
    shift @chapters;    # remove before first chapter

    my $chapter_num = 0;
    my @chapter_stats;
    for (@chapters) {
        my %stats = (
            chapter_num      => $chapter_num,
            name             => $chapters_names[$chapter_num],
            paragraph_number => 0,
            tirets_dialogue  => 0,
            tirets_pause     => 0,
            dms              => 0,
        );

        while (/\.P\n/g) { $stats{paragraph_number}++; }
        while (/\.D\n/g) { $stats{tirets_dialogue}++; }
        while (/—/g) { $stats{tirets_pause}++; }
        while (/\.Bm\ -t\ dm/g) { $stats{dms}++; }

        push @chapter_stats, \%stats;

        $chapter_num++;
    }

    return @chapter_stats;
}

if (@ARGV == 1) {
    my @chapter_stats = stats(shift @ARGV);
    printf "%s %10s %10s %10s %5s\n", "chapter", "paragraphs", "dialogues",
      "tirets_pause", "dms";
    foreach my $stats (@chapter_stats) {
        printf "%3d %10d %10d %10d %10d\n",
          @$stats{
            'chapter_num',  'paragraph_number', 'tirets_dialogue',
            'tirets_pause', 'dms'
          };
    }
} elsif (@ARGV == 2) {
    my ($file1, $file2) = @ARGV;
    my @chapter_stats1 = stats($file1);
    my @chapter_stats2 = stats($file2);

    if (@chapter_stats1 != @chapter_stats2) {
        print "¡¡Warning!! $file1 has ", +@chapter_stats1, "chapters ",
          "and $file2 has ", +@chapter_stats2, "chapters\n";
    }
    for (my $i = 0 ; $i <= $#chapter_stats1 ; $i++) {
        foreach (grep { $_ !~ /^name$/ } keys %{ $chapter_stats1[0] }) {
            my $val1 = $chapter_stats1[$i]->{$_};
            my $val2 = $chapter_stats2[$i]->{$_};
            $val1 eq $val2
              or print qq(chapter $i "$chapter_stats1[$i]{name}" for $_ get $val1 vs $val2\n);
        }
    }
} else {
    print <<EOF;
Usage:
    $0 file
    $0 file1 file2
EOF
}

