/*
 * grunt-lithe
 * https://github.com/xiaojue/grunt-lithe
 *
 * Copyright (c) 2013 xiaojue
 * Licensed under the MIT license.
 */

'use strict';

var lithe = require('lithe');
var tool = lithe.tool;
var litheOptions = tool.options;
var Path = require('path');
var rootpath = process.cwd();

function isJs(path) {
	return Path.extname(path) === '.js';
}

function isDirPath(path){
   return path.lastIndexOf('/') === path.length - 1; 
}

module.exports = function(grunt) {

	grunt.registerMultiTask('lithe', 'lithe for grunt plugin', function() {

		var options = this.options({
			//alias: {}
			//basepath:''
		});

		if (options.basepath) {
			litheOptions.basepath = Path.resolve(rootpath, options.basepath);
		}
		if (options.alias) {
			litheOptions.alias = options.alias;
		}

		this.files.forEach(function(f) {
			var src = [];
			f.src.filter(function(path) {
				if (grunt.file.exists(path)) {
					return true;
				} else {
					return false;
				}
			}).forEach(function(path) {
				if (grunt.file.isFile(path) && isJs(path)) {
					src.push({
						path: path,
						name: Path.filename(path)
					});
				} else if (grunt.file.isDir(path)) {
					grunt.file.recurse(path, function(abspath, rootdir, subdir, filename) {
						if (isJs(abspath)) {
							src.push({
								path: abspath,
								name: filename
							});
						}
					});
				}
			});
			src.forEach(function(file) {
				var conf = Path.resolve(rootpath, file.path);
				var requires = tool.findJsAllrequires(conf);
				requires.push(conf);
				var str = requires.map(function(file) {
					return grunt.file.read(file);
				}).join('');

				if (isDirPath(f.dest)) {
					grunt.file.write(f.dest + file.name, str);
					grunt.log.writeln('File "' + f.dest + file.name + '" created.');
				} else {
					grunt.file.write(f.dest, str);
					grunt.log.writeln('File "' + f.dest + '" created.');
				}
			});
		});

	});

};


@@ -377,13 +377,12 @@
 ath)
+ 
 %7B%0A
-   
+%09
 retu
@@ -426,17 +426,16 @@
 gth - 1;
- 
 %0A%7D%0A%0Amodu
@@ -603,16 +603,41 @@
 path:''%0A
+%09%09%09//filter:function()%7B%7D%0A
 %09%09%7D);%0A%0A%09
@@ -1534,24 +1534,115 @@
 push(conf);%0A
+%09%09%09%09if (options.filter && typeof filter == 'function') requires = requires.filter(filter);%0A
 %09%09%09%09var str 

add filter