#!/usr/bin/perl

use strict;
use warnings;

use Cwd 'abs_path';
use File::Basename;

use constant DIR  => dirname( dirname( __FILE__ ) );
use constant DICT => abs_path( DIR.'/doc/aspell.en.pws' );

sub spellcheck {
    my $file = shift;
    print "Spell checking $file\n";
    system('aspell', '-H', '-p', DICT, '-c', $file);
}

my $argc = scalar @ARGV;
if ( ($argc == 0) or (($argc == 1) and not -f $ARGV[0]) ) {
    my $treeish = $ARGV[0] || 'master';
    open ( GIT, "git diff --name-only '$treeish'|" ) or die $!;
    while (<GIT>) {
        chomp;
        next unless m{.*/.*\.xml$};
        spellcheck( $_ );
    }
}
else {
    foreach (@ARGV) {
        spellcheck( $_ );
    }
}
