var gulp = require('gulp');
var inject = require('gulp-inject');
var async = require('async');
var gutil = require('gulp-util');
var through2 = require('through2');

module.exports = function (bundles) {
    return through2.obj(function (file, enc, cb) {
        var stream = this;
        
        if (!bundles || !bundles.scripts || !bundles.styles) {
            return cb(null, file);
        }
        
        if (file.isNull()) {
            return cb(null, file);
        }

        if (file.isStream()) {
            return cb(new gutil.PluginError('gulp-inject', 'Streaming not supported'));
        }

        var scriptBundles = Object.keys(bundles.scripts);
        var styleBundles = Object.keys(bundles.styles);
        var referencedScriptBundles = [];
        var referencedStyleBundles = [];

        scriptBundles.forEach(function (bundle) {
            var expression = "(" + bundle + ":js\w*)";
            var regex = new RegExp(expression, "ig");
            if (regex.test(file.contents.toString('utf-8'))) {
                referencedScriptBundles.push(bundle);
            }
        });

        styleBundles.forEach(function (bundle) {
            var expression = "(" + bundle + ":css\w*)";
            var regex = new RegExp(expression, "ig");
            if (regex.test(file.contents.toString('utf-8'))) {
                referencedStyleBundles.push(bundle);
            }
        });
        
        if (!referencedScriptBundles.length && !referencedStyleBundles.length) {
            return cb(null, file);
        }

        if (referencedScriptBundles.length) {
            async.eachSeries(referencedScriptBundles, function (bundle, fn) {
                stream.pipe(inject(gulp.src(bundles.scripts[bundle], {
                    read: false
                }), {
                    name: bundle
                }));
                fn();
            }, function(err) {
                if (err) cb(err);
            });
        }
        
        if (referencedStyleBundles.length) {
            async.eachSeries(referencedStyleBundles, function (bundle, fn) {
                stream.pipe(inject(gulp.src(bundles.styles[bundle], {
                    read: false
                }), {
                    name: bundle
                }));
                fn();
            }, function(err) {
                if (err) cb(err);
            });
        }

        return cb(null, file);
    });
};
@@ -193,19 +193,58 @@
 (bundles
-) %7B
+, options) %7B%0A    options = options %7C%7C %7B%7D;%0A
 %0A    ret
@@ -1727,59 +1727,170 @@
     
-stream.pipe(inject(gulp.src(bundles.scripts%5Bbundle%5D
+var glob = options.debug ? bundles.scripts%5Bbundle%5D : bundles.outputDir.scripts + bundles.scripts%5Bbundle%5D + '.js';%0A                stream.pipe(inject(gulp.src(glob
 , %7B%0A
@@ -2261,58 +2261,168 @@
     
-stream.pipe(inject(gulp.src(bundles.styles%5Bbundle%5D
+var glob = options.debug ? bundles.styles%5Bbundle%5D : bundles.outputDir.styles + bundles.styles%5Bbundle%5D + '.css';%0A                stream.pipe(inject(gulp.src(glob
 , %7B%0A

add support for options