#!/usr/bin/env perl

# bin/critic - Perl::Critic script.
# Copyright (C) 2010-2011 Xelhua Development Group, et al.
# This program is free software; rights to this code are stated in doc/LICENSE.
require 5.010000;
use strict;
use warnings;
use feature qw(say);
use Perl::Critic;

our $VERSION = 1.00;

# Set default severity to 1.
my $severity = 1;

if (!defined $ARGV[0]) {
    # Missing parameter file.
    say 'Not enough parameters. Usage: <file> [level]';
    say 'Levels: 1/brutal, 2/cruel, 3/harsh, 4/stern, 5/gentle';
}

if (defined $ARGV[1]) {
    # If a different severity was passed, get it.
    my $ts = $ARGV[1];
    $ts = lc $ts;

    if ($ts =~ m/^(1|2|3|4|5|brutal|cruel|harsh|stern|gentle)/xsm) {
        # If it's valid, set severity to it.
        $severity = $ts;
    }
}

# Create an instance of Perl::Critic, excluding certain Policies.
my $critic = Perl::Critic->new(-exclude => ['Perl::Critic::Policy::Miscellanea::RequireRcsKeywords',
                                            'Perl::Critic::Policy::RegularExpressions::ProhibitSingleCharAlternation',
                                            'Perl::Critic::Policy::Modules::ProhibitExcessMainComplexity',
                                            'Perl::Critic::Policy::InputOutput::ProhibitBacktickOperators',
                                            'Perl::Critic::Policy::InputOutput::RequireCheckedSyscalls',
                                            'Perl::Critic::Policy::CodeLayout::RequireTidyCode'], -severity => $severity);

# Get violations.
my @violations = $critic->critique($ARGV[0]);

my $count = 0;

if (!defined $violations[0]) {
    # No violations! :D
    say "No violations. $ARGV[0] has passed!";
    exit;
}

# There was some violations. Lets list them.
say 'Violations:';
foreach (@violations) {
    $count++;
    say $_;
}
say "$count violations in $ARGV[0].";

# vim: set ai et sw=4 ts=4:
