/**
 * @overview Automation tasks with grunt.
 * @see {@link http://gruntjs.com/ | grunt}
 * @param grunt
 */
var path = require('path'),
    fs = require('fs'),
    cp = require('child_process'),
    fork = cp.fork;

module.exports = function (grunt) {
    grunt.initConfig({
        mkdir: {
            makeProjectDirectories: {
                options: {
                    create: [
                        'bin',
                        'doc',
                        'ci',
                        'ci/bin',
                        'ci/data',
                        'core_apps',
                        'lib/commands',
                        'test/mock',
                        'test/work',
                        'test/unit_tests',
                        'test/unit_tests/commands',
                        'test/scenario_tests',
                        'test/scenario_tests/command_scenario',
                        'tmpl'
                    ]
                }
            }
        },
        chmod: {
            defaultMode: {
                options: {mode: '644'},
                src: ['lib/**/*.js']
            },
            executableMode: {
                options: {mode: '755'},
                src: ['bin/*', 'ci/bin/*', 'core_apps/*.sh']
            },
            readonlyMode: {
                options: {mode: '444'},
                src: [
                    'lib/index.js'
                ]
            }
        },
        mochaTest: {
            command_scenario: {
                src: 'test/scenario_tests/command_scenario/*'
            }
        },
        nodeunit: {
            unit_tests: ['test/unit_tests/**/*_test.js'],
            coverage_unit_tests: ['.work/instrument/test/unit_tests/**/*_test.js']
        },
        'new': {
            options: {
                engine: require('consolidate').handlebars
            },
            libIndex: {
                template: 'tmpl/lib-index.js.hbs',
                data: {
                    dest: 'lib/index.js',
                    names: {
                        commands: './commands',
                        _print: './_print'
                    },
                    commandNames: 'new,show,task,heir'.split(','),
                    readmePath: 'README.md',
                    wikiDirPath: 'doc/wiki'
                }
            }
        },
        jsdoc: {
            dist: {
                src: [
                    'README.md',
                    'lib/**/*.js'
                ],
                options: {
                    destination: 'doc/apiguide',
                    configure: 'doc/apiguide-conf.json',
                    template: 'tmpl/doc/apiguide'
                }
            }
        }
    });

    grunt.loadNpmTasks('grunt-mkdir');
    grunt.loadNpmTasks('grunt-chmod');
    grunt.loadNpmTasks('grunt-contrib-nodeunit');
    grunt.loadNpmTasks('grunt-mocha-test');
    grunt.loadNpmTasks('grunt-new');
    grunt.loadNpmTasks('grunt-jsdoc');

    /** Update apeman core apps data. */
    grunt.registerTask('apc', function () {
        var done = this.async();
        var forked = fork(require.resolve('./ci/bin/apc'));
        forked.on('exit', done);
    });

    grunt.registerTask('doc', [
        'jsdoc'
    ]);

    grunt.registerTask('unitTest', [
        'nodeunit:unit_tests'
    ]);

    grunt.registerTask('test', [
        'mochaTest',
        'unitTest'
    ]);

    grunt.registerTask('build', [
        'mkdir',
        'chmod:defaultMode',
        'new',
        'chmod:readonlyMode'
    ]);

    grunt.registerTask('coverage', [
        'nodeunit:coverage_unit_tests'
    ]);

    grunt.registerTask('default', [
        'build',
        'test'
    ]);

};
