"use strict";

module.exports = function (grunt) {

	// A temporary directory used by amdserialize to output the processed modules.
	var tmpdir = "./tmp/";

	// The final output directory.
	var outdir = "./out/";

	// The grunt.config property populated by amdserialize, containing the
	// list of files to include in the layer.
	var outprop = "amdoutput";


	grunt.initConfig({
		// The loader config should go here.
		amdloader: {
			// Here goes the config for the amd plugins build process.
			config: {
				// text should be replace by the module id of text plugin
				text: {
					inlineText: true
				},
				// i18n should be replace by the module id of i18n plugin
				i18n: {
					localesList: ["fr"]
				}
			}
		},

		// The common build config
		amdbuild: {
			// dir is the output directory.
			dir: tmpdir,

			// List of plugins that the build should not try to resolve at build time.
			runtimePlugins: [],

			// List of layers to build.
			layers: [{
				name: "layerName",
				include: [
					// Modules and layers listed here, and their dependencies, will be added to the layer.
				],
				includeShallow: [
					// Only the modules listed here (ie. NOT their dependencies) will be added to the layer.
				],
				exclude: [
					// Modules and layers listed here, and their dependencies, will NOT be in the layer.
				],
				excludeShallow: [
					// Only the modules listed here (ie. NOT their dependencies)  will NOT be in the layer.
				]
			}]
		},

		// Config to allow concat to generate the layer.
		concat: {
			options: {
				banner: "<%= " + outprop + ".header%>"
			},
			dist: {
				src: "<%= " + outprop + ".modules.abs %>",
				dest: outdir + "<%= " + outprop + ".layerPath %>"
			}
		},

		// Copy the plugin files to the real output directory.
		copy: {
			dist: {
				expand: true,
				cwd: tmpdir,
				src: "<%= " + outprop + ".plugins.rel %>",
				dest: outdir
			}
		},

		// Erase temp directory and previous build
		clean: {
			erase: [outdir],
			finish: [tmpdir]
		}
	});


	// The main build task.
	grunt.registerTask("amdbuild", function (amdloader) {
		var name = this.name,
			layers = grunt.config(name).layers;

		layers.forEach(function (layer) {
			grunt.task.run("amddepsscan:" + layer.name + ":" + name + ":" + amdloader);
			grunt.task.run("amdserialize:" + layer.name + ":" + name + ":" + outprop);
			grunt.task.run("concat");
			grunt.task.run("copy");
		});
	});


	// Load the plugin that provides the "amd" task.
	grunt.loadNpmTasks("grunt-amd-build");

	// Load vendor plugins.
	grunt.loadNpmTasks("grunt-contrib-concat");
	grunt.loadNpmTasks('grunt-contrib-copy');
	grunt.loadNpmTasks('grunt-contrib-clean');

	// Default task.
	grunt.registerTask("default", ["clean:erase", "amdbuild:amdloader", "clean:finish"]);
};

@@ -199,19 +199,21 @@
 ir = %22./
-out
+build
 /%22;%0A%0A%09//
@@ -435,287 +435,186 @@
 %0A%09%09%09
-// Here goes the config for the amd plugins build process.%0A%09%09%09config: %7B%0A%09%09%09%09// text should be replace by the module id of text plugin%0A%09%09%09%09text: %7B%0A%09%09%09%09%09inlineText: true%0A%09%09%09%09%7D,%0A%09%09%09%09// i18n should be replace by the module id of i18n plugin%0A%09%09%09%09i18n: %7B%0A%09%09%09%09%09localesList: %5B%22fr%22%5D%0A%09%09%09%09%7D
+baseUrl: %22./%22,%0A%0A%09%09%09// Enable build of requirejs-text/text%0A%09%09%09inlineText: true,%0A%0A%09%09%09// Here goes the config for the amd plugins build process (has, i18n, ecma402...).%0A%09%09%09config: %7B
 %0A%09%09%09
@@ -1389,22 +1389,22 @@
 o allow 
-concat
+uglify
  to gene
@@ -1421,22 +1421,22 @@
 ayer.%0A%09%09
-concat
+uglify
 : %7B%0A%09%09%09o
@@ -1487,16 +1487,37 @@
 eader%25%3E%22
+,%0A%09%09%09%09sourceMap: true
 %0A%09%09%09%7D,%0A%09
@@ -1704,28 +1704,31 @@
 %09copy: %7B%0A%09%09%09
-dist
+plugins
 : %7B%0A%09%09%09%09expa
@@ -1817,16 +1817,31 @@
 : outdir
+,%0A%09%09%09%09dot: true
 %0A%09%09%09%7D%0A%09%09
@@ -1859,27 +1859,8 @@
 ase 
-temp directory and 
 prev
@@ -1869,16 +1869,17 @@
 us build
+.
 %0A%09%09clean
@@ -1904,29 +1904,8 @@
 dir%5D
-,%0A%09%09%09finish: %5Btmpdir%5D
 %0A%09%09%7D
@@ -2238,16 +2238,34 @@
  + %22:%22 +
+ amdloader + %22:%22 +
  outprop
@@ -2286,22 +2286,22 @@
 sk.run(%22
-concat
+uglify
 %22);%0A%09%09%09g
@@ -2319,16 +2319,24 @@
 un(%22copy
+:plugins
 %22);%0A%09%09%7D)
@@ -2499,14 +2499,14 @@
 rib-
-concat
+uglify
 %22);%0A
@@ -2685,20 +2685,30 @@
 %22, %22
-clean:finish
+amdreportjson:amdbuild
 %22%5D);

update sample to latest api changes