/*
 * grunt-phpunit
 * https://github.com/SaschaGalley/grunt-phpunit
 *
 * Copyright (c) 2013 Sascha Galley
 * http://xash.at
 * Licensed under the MIT license.
 */
'use strict';

// External libs.
var path = require('path');
var exec = require('child_process').exec;

exports.init = function(grunt) {

    var exports  = {},
        defaults = {
            // Default options
            bin: 'phpcs',
            extensions: false,
            ignore: false,
            severity: false,
            standard: false,
            verbose: false,
            reportFile: false,
            report: 'full',
            maxBuffer: 200*1024,
            ignoreExitCode: false
        },
        cmd    = null,
        done   = null,
        config = {};

    /**
     * Builds phpunit command
     *
     * @return string
     */
    var buildCommand = function(dir) {

        var cmd = path.normalize(config.bin);

        if (grunt.option('extensions') || config.extensions) {
            // A comma separated list of file extensions to check
            cmd += ' --extensions=' + config.extensions;
        }

        if (grunt.option('ignore') || config.ignore) {
            // A comma separated list of patterns to ignore files and directories.
            cmd += ' --ignore=' + config.ignore;
        }

        if (grunt.option('severity') || config.severity) {
            // The minimum severity required to display an error or warning
            cmd += ' --severity=' + config.severity;
        }

        if (grunt.option('warning-severity') || config.warningSeverity) {
            // The minimum severity required to display an error or warning
            cmd += ' --warning-severity=' + config.warningSeverity;
        }

        if (grunt.option('standard') || config.standard) {
            // Define the code sniffer standard.
            cmd += ' --standard=' + config.standard;
        }

        if (grunt.option('report-file') || config.reportFile) {
            // Define the file to write the report to.
            cmd += ' --report-file=' + config.reportFile;
        }

        if (grunt.option('report') || config.report) {
            // Define the style of the report.
            cmd += ' --report=' + config.report;
        }

        if (grunt.option('verbose') || config.verbose === true) {
            // Output more verbose information.
            cmd += ' -v';
        }
        return cmd;
    };

    /**
     * Setup task before running it
     *
     * @param Object runner
     */
    exports.setup = function(runner) {

        var dir = path.normalize(runner.data.dir);
        config  = runner.options(defaults);
        cmd     = buildCommand(dir) + ' ' + dir;

        grunt.log.writeln('Starting phpcs (target: ' + runner.target.cyan + ') in ' + dir.cyan);
        grunt.verbose.writeln('Exec: ' + cmd);

        done    = runner.async();
    };

    /**
     * Runs phpunit command with options
     *
     */
    exports.run = function() {
        var cmdOptions = {
            maxBuffer: config.maxBuffer
        };

        exec(cmd, cmdOptions, function(err, stdout, stderr) {

            if (stdout) {
                grunt.log.write(stdout);
            }

            if (err) {
                if (! config.ignoreExitCode) {
                    grunt.fatal(err);
                }
            }
            done();
        });
    };

    return exports;
};

@@ -459,37 +459,8 @@
 se,%0A
-            severity: false,%0A
     
@@ -649,16 +649,506 @@
      %7D,%0A
+        cliOptions = %7B%0A            errorSeverity: grunt.option('error-severity'),%0A            extensions: grunt.option('extensions'),%0A            ignore: grunt.option('ignore'),%0A            report: grunt.option('report'),%0A            reportFile: grunt.option('report-file'),%0A            severity: grunt.option('severity'),%0A            standard: grunt.option('standard'),%0A            warningSeverity: grunt.option('warning-severity'),%0A            verbose: grunt.option('verbose')%0A        %7D,%0A
         
@@ -1385,38 +1385,209 @@
 if (
-grunt.option('extensions') %7C%7C 
+config.errorSeverity !== undefined) %7B%0A            // The minimum severity required to display an error or warning%0A            cmd += ' --error-severity=' + config.errorSeverity;%0A        %7D%0A%0A        if (
 conf
@@ -1753,34 +1753,8 @@
 if (
-grunt.option('ignore') %7C%7C 
 conf
@@ -1925,36 +1925,8 @@
 if (
-grunt.option('severity') %7C%7C 
 conf
@@ -1932,24 +1932,38 @@
 fig.severity
+ !== undefined
 ) %7B%0A        
@@ -2110,44 +2110,8 @@
 if (
-grunt.option('warning-severity') %7C%7C 
 conf
@@ -2128,16 +2128,30 @@
 Severity
+ !== undefined
 ) %7B%0A    
@@ -2317,36 +2317,8 @@
 if (
-grunt.option('standard') %7C%7C 
 conf
@@ -2461,39 +2461,8 @@
 if (
-grunt.option('report-file') %7C%7C 
 conf
@@ -2618,34 +2618,8 @@
 if (
-grunt.option('report') %7C%7C 
 conf
@@ -2754,35 +2754,8 @@
 if (
-grunt.option('verbose') %7C%7C 
 conf
@@ -3065,16 +3065,34 @@
 ata.dir)
+,%0A            attr
 ;%0A      
@@ -3129,16 +3129,177 @@
 aults);%0A
+%0A        for (attr in cliOptions) %7B%0A            if (cliOptions%5Battr%5D !== undefined) %7B %0A                config%5Battr%5D = cliOptions%5Battr%5D;%0A            %7D%0A        %7D%0A%0A
         

Fix support for CLI options