// Check for the name of the naturaldocs binary
var fs = require('fs');
if (fs.existsSync("/usr/bin/naturaldocs")) {
    naturaldocs = "/usr/bin/naturaldocs";
} else if (fs.existsSync("/usr/lib/bin/natural_docs")) {
    naturaldocs = "/usr/lib/bin/natural_docs";
} else {
    naturaldocs = "NaturalDocs";
}

module.exports = function(grunt){
    var pkg = grunt.file.readJSON('package.json');

    grunt.initConfig({
        pkg: pkg,

        clean: {
            "prepare-doc": ["<%= natural_docs.docs.inputs[0] %>", "<%= natural_docs.docs.project %>"],
            "doc": ["<%= natural_docs.docs.output %>"],
            "prepare-release": ["strophejs-<%= pkg.version %>"],
            "release": ["strophejs-<%= pkg.version %>.zip", "strophejs-<%= pkg.version %>.tar.gz"],
            "js": ["strophe.js", "strophe.min.js"]
        },

        qunit: {
            all: {
                options: {
                    urls: [
                        'http://localhost:8000/tests/strophe.html',
                    ]
                }
            }
        },
        connect: {
            server: {
                options: {
                    port: 8000,
                    base: '.'
                }
            }
        },

        copy: {
            "prepare-release": {
                files:[
                    {
                        expand: true,
                        src:['', 'strophe.min.js', 'LICENSE.txt', 'README.txt',
                            'contrib/**', 'examples/**', 'plugins/**', 'tests/**', 'doc/**'],
                        dest:"strophejs-<%= pkg.version %>"
                    }
                ]
            },
            "prepare-doc": {
                files:[
                    {
                        src:['strophe.js'],
                        dest:"<%= natural_docs.docs.inputs[0] %>"
                    }
                ]
            }
        },

        jshint: {
            files: ['Gruntfile.js', 'src/*.js'],
        },

        shell: {
            tar: {
                command: 'tar czf strophejs-<%= pkg.version %>.tar.gz strophejs-<%= pkg.version %>',
                options: { failOnError: true }
            },
            zip: {
                command: 'zip -qr strophejs-<%= pkg.version %>.zip strophejs-<%= pkg.version %>',
                options: { failOnError: true }
            }
        },

        uglify: {
            options: {
                banner: '/*! <%= pkg.name %> v<%= pkg.version %> - built on <%= grunt.template.today("dd-mm-yyyy") %> */\n'
            },
            dist: {
                files: { 'strophe.min.js': ['strophe.js'] }
            }
        },

        watch: {
            files: ['<%= jshint.files %>'],
            tasks: ['build', 'uglify']
        },

        natural_docs: {
            docs: {
                bin: naturaldocs,
                inputs: [ "doc-tmp/" ],
                project: "ndproj",
                output: "doc"
            },
        },

        mkdir: {
            "prepare-doc": {
                options: {
                    create: ["<%= natural_docs.docs.project %>", "<%= natural_docs.docs.output %>"]
                }
            },
        },
    });

    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks("grunt-contrib-uglify");
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');
    grunt.loadNpmTasks("grunt-contrib-clean");
    grunt.loadNpmTasks('grunt-shell');
    grunt.loadNpmTasks('grunt-natural-docs');
    grunt.loadNpmTasks('grunt-mkdir');
    grunt.loadNpmTasks('grunt-contrib-qunit');

    grunt.registerTask("default", ["jshint", "min"]);
    grunt.registerTask("min", ["build", "uglify"]);
    grunt.registerTask("prepare-release", ["copy:prepare-release"]);
    grunt.registerTask("doc", ["build", "copy:prepare-doc", "mkdir:prepare-doc", "natural_docs"]);
    grunt.registerTask("release", ["default", "doc", "copy:prepare-release", "shell:tar", "shell:zip"]);
    grunt.registerTask("all", ["release", "clean"]);
    grunt.registerTask("test", ["connect", "qunit"]);

    grunt.registerTask('concat', 'Create a new build', function () {
        var done = this.async();
        require('child_process').exec(
                './node_modules/requirejs/bin/r.js -o build.js optimize=none out=strophe.js',
            function (err, stdout, stderr) {
                if (err) {
                    grunt.log.write('build failed with error code '+err.code);
                    grunt.log.write(stderr);
                }
                grunt.log.write(stdout);
                done();
            }
        );
        grunt.task.run('uglify');
    });
};

@@ -1223,32 +1223,481 @@
   %7D%0A        %7D,%0A%0A
+        concat: %7B%0A            dist: %7B%0A                src: %5B'src/wrap_header.js', 'src/base64.js', 'src/sha1.js', 'src/md5.js', 'src/polyfills.js', 'src/core.js', 'src/bosh.js', 'src/websocket.js', 'src/wrap_footer.js'%5D,%0A                dest: '%3C%25= pkg.name %25%3E'%0A            %7D,%0A            options: %7B%0A                process: function(src)%7B%0A                    return src.replace('@VERSION@', pkg.version);%0A                %7D%0A            %7D%0A        %7D,%0A%0A
         copy: %7B%0A
@@ -3189,21 +3189,22 @@
 asks: %5B'
-build
+concat
 ', 'ugli
@@ -3937,24 +3937,72 @@
 ib-clean%22);%0A
+    grunt.loadNpmTasks(%22grunt-contrib-concat%22);%0A
     grunt.lo
@@ -4243,29 +4243,30 @@
 sk(%22min%22, %5B%22
-build
+concat
 %22, %22uglify%22%5D
@@ -4369,21 +4369,22 @@
 doc%22, %5B%22
-build
+concat
 %22, %22copy
@@ -4674,22 +4674,22 @@
 erTask('
-concat
+almond
 ', 'Crea
@@ -4696,18 +4696,32 @@
 te a
- new build
+n almond build with r.js
 ', f
@@ -4894,16 +4894,23 @@
 strophe.
+almond.
 js',%0A   

Add the concat task again.