#!/bin/bash
#
#  Wrapper around perlcritic and perl -c for Emacs flymake.

showhelp() {
    echo "flymake_perlcritic [-I <include-dir>] [-h] <source-file>

Run perlcritic and \"perl -c\" on <source-file> in a manner appropriate for use by Emacs flymake-perlcritic.el.

    -I <include-dir> Add the supplied dir to \$PERL5LIB include paths.
    -h               Display this help.
    <source-file>    The source file to run the checks on.";
    exit 0;
}

while getopts "I:h" "OPTNAME";
do
    case $OPTNAME in
        I) INCLUDE=$OPTARG ;;
        h) showhelp        ;;
        *) exit -1         ;;
    esac
done
shift $(($OPTIND - 1))

FILE_NAME="$1"
shift

if [ -n "$INCLUDE" ]; then
    if [ -n "$PERL5LIB" ]; then
        export PERL5LIB="$INCLUDE:$PERL5LIB"
    else
        export PERL5LIB="$INCLUDE"
    fi
fi

# Run perlcritic first.
# TODO: be nice to include severity and have it turn into warning/error.
perlcritic --nocolour "$FILE_NAME" --verbose "%f:%l:%c: Warning: %m. %e.\n" $*

# Run perl -c too for good measure.
perl -wc -Mstrict "$FILE_NAME"

exit 0