var path		= require('path');
var crypto		= require('crypto');
var fs			= require('fs');

var underscore	= require('underscore');
var send		= require('send');
var debug		= require('debug')('seajs-combo-express:main');
var SendStream	= require('./send');

var defaults = {
	cachePath	: '',
	separator	: '\n',
	root		: path.dirname(module.parent.parent.filename),
	maxage		: 0,
	comboSyntax	: ["??", ","],

	parseFilename: function(url) {
		return crypto.createHash('sha1').update(url).digest('hex')+'.js';
	}
};

module.exports = function(config) {
	if (typeof config == 'string') {
		config = {root: config};
	}

	config = underscore.extend({}, config, defaults);
	config.root = path.normalize(config.root);

	return function(req, res, next) {
		var sender = new SendStream(req, res, config);

		var rs = sender.check();
		if (rs === false) {
			next();
		} else if (rs === true) {
			return;
		}


		if (1 || config.cachePath) {
			// width cache
			var file = config.cachePath+'/'+config.parseFilename(req.url);
			debug('dist file name: %s', file);

			if (0 && fs.existsSync(file)) {
				// static resource
				send(req, file, underscore.extend({}, config, {root: config.cachePath})).pipe(res);
			} else {

				var newLstats;
				var allData = [];

				sender.on('readStart', function(lstats, lfile, filePaths) {
						newLstats = lstats;
					})
					.on('data', function(data) {
						allData.push(data);
					})
					.on('end', function() {
						// write cache file
						fs.open(file, 'w+', function(err, fd) {
							if (err) return debug('open file err: %s', file, err);

							try {
								var buf = Buffer.concat(allData);
								fs.writeSync(fd, buf, 0, buf.length, null);
								fs.futimesSync(fd, newLstats.atime, newLstats.mtime);
							} catch(err) {
								debug('write file err: %s', file, err);
							}

							// close fd
							try {
								fd && fs.closeSync(fd);
							} catch(err) {
								debug('close file err: %s', file, err);
							}
						});
					})
					.on('error', function(err) {
						debug('send err', err);
					})
					.combo();
			}
		} else {
			sender.combo();
		}
	}
};

@@ -901,13 +901,8 @@
 if (
-1 %7C%7C 
 conf
@@ -1051,13 +1051,8 @@
 if (
-0 && 
 fs.e
@@ -1514,76 +1514,42 @@
 %09%09%09%09
-if (err) return debug('open file err: %25s', file, err);%0A%0A%09%09%09%09%09%09%09try %7B
+try %7B%0A%09%09%09%09%09%09%09%09if (err) throw err;%0A
 %0A%09%09%09
@@ -1701,16 +1701,82 @@
 mtime);%0A
+%09%09%09%09%09%09%09%09fs.closeSync(fd);%0A%0A%09%09%09%09%09%09%09%09debug('write file: %25s', file);%0A
 %09%09%09%09%09%09%09%7D
@@ -1841,25 +1841,30 @@
 r);%0A
+%0A%09
 %09%09%09%09%09%09%09
-%7D%0A
+try %7B
 %0A%09%09%09%09%09%09%09
 // c
@@ -1863,65 +1863,90 @@
 %09%09%09%09
-// close fd%0A%09%09%09%09%09%09%09try %7B%0A%09%09%09%09%09%09%09%09fd && fs.close
+%09%09fd && fs.closeSync(fd);%0A%09%09%09%09%09%09%09%09%09if (fs.existsSync(file)) fs.unlink
 Sync(f
-d
+ile
 );%0A
+%09
 %09%09%09%09
@@ -1967,24 +1967,25 @@
 ) %7B%0A%09%09%09%09%09%09%09%09
+%09
 debug('close
@@ -2008,24 +2008,34 @@
 file, err);%0A
+%09%09%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%09%09%7D%0A%09%09%09

remove file when wirting error