"use strict";
var path = require("path");
var fs = require("fs");

// Used for debugging. If this method is not called, it means this module is
// entirely bug free.

function die(message) {
	console.log.apply(console, [message]);
	process.exit(1);
}

function run(options) {
	var transformer = "streamline/lib/" + (options.generators ? "generators" : options.fibers ? "fibers" : "callbacks") + "/transform";
	var streamline = require(transformer).transform;

	// If CoffeeScript is used, we load it and duck.
	if (!require.extensions['.coffee']) {
		require.extensions['.coffee'] = function(module, filename) {
			try {
				require("coffee-script")
			} catch (e) {
				throw new Error("Cannot load CoffeeScript to compile: '" + filename + "'");
			}
			require.extensions['.coffee'](module, filename);
		}
	}

	function grind(filename) {
		try {
			var coffee = require("coffee-script");
		} catch (e) {
			throw new Error("Cannot load CoffeeScript to compile: '" + filename + "'");
		}
		return coffee.compile(fs.readFileSync(filename, "utf8"), {
			filename: filename
		});
	}

	function clone(obj) {
		return Object.keys(obj).reduce(function(val, key) {
			val[key] = obj[key];
			return val;
		}, {});
	}

	var streamliners = {
		js: function(module, filename, code) {
			module._compile(code, filename);
		},
		_js: function(module, filename, code) {
			var cachedTransform = require("streamline/lib/compiler/compile").cachedTransformSync;
			var opts = clone(options);
			opts.sourceName = filename;
			var streamlined = options.cache ? cachedTransform(code, filename, streamline, opts) : streamline(code, opts);
			module._compile(streamlined, filename)
		},
		coffee: function(module, filename, code) {
			var code = grind(filename);
			module._compile(code, filename);
		},
		_coffee: function(module, filename, code) {
			var code = grind(filename);
			streamliners._js(module, filename, code, options);
		}
	};

	// If Streamlined CoffeeScript is used, load CoffeeScript and register a new
	// handler for the `._coffee` extension.
	require.extensions['._coffee'] = function(module, filename) {
		var code = fs.readFileSync(filename, "utf8");
		streamliners._coffee(module, filename, code);
	}

	// Streamlined JavaScript extension handler.
	require.extensions['._js'] = function(module, filename) {
		var code = fs.readFileSync(filename, "utf8");
		streamliners._js(module, filename, code);
	}

	// If we were asked to register extension handlers only, we're done:
	if (options.registerOnly) return;

	// Otherwise, we're being asked to execute (run) a file too:
	var extension = path.basename(process.argv[0]) === '_coffee' ? '._coffee' : '._js';
	var filename = process.argv[1];

	process.execPath = require.main.filename;

	var mainModule = require.main;

	// Clear the main module cache.
	if (mainModule.moduleCache) {
		mainModule.moduleCache = {};
	}

	// Set the module paths.
	mainModule.paths = require("module")._nodeModulePaths(path.dirname(filename));

	function locateFile() {
		if (path.existsSync(filename + extension)) filename += extension;
		else if (path.existsSync(filename + extension.replace('_', ''))) filename += (extension = extension.replace('_', ''));
		else throw new Error("file not found: " + filename);
	}
	// Handle the case where filename is just the module name, add extensions
	if (!path.existsSync(filename)) {
		locateFile();
	} else if (fs.statSync(filename).isDirectory()) {
		var pkg, pkgname = path.join(filename, "package.json");
		if (path.existsSync(pkgname)) {
			pkg = JSON.parse(fs.readFileSync(pkgname, "utf8"));
			if (pkg.main) {
				filename = path.join(filename, pkg.main);
				if (!path.existsSync(filename)) {
					locateFile();
				}
			}
		}
		if (!pkg || !pkg.main) {
			filename = path.join(filename, "index");
			locateFile();
		}
	}

	mainModule.filename = filename ? fs.realpathSync(filename) : '.';
	process.argv.splice(1, 1, mainModule.filename)

	// Load the target file and evaluate it.
	fs.readFile(filename, "utf8", function(error, code) {
		var streamlined, javacript, match;
		// Strip the shebang line, while preserving line count.
		if (match = /^#!.*([^\u0000]*)$/.exec(code)) code = match[1];
		// Execute the code.
		streamliners[extension.substring(1)](mainModule, mainModule.filename, code);
	});
}

module.exports.run = run;
@@ -1231,90 +1231,8 @@
 = %7B%0A
-%09%09js: function(module, filename, code) %7B%0A%09%09%09module._compile(code, filename);%0A%09%09%7D,%0A
 %09%09_j
@@ -1583,125 +1583,8 @@
 %09%7D,%0A
-%09%09coffee: function(module, filename, code) %7B%0A%09%09%09var code = grind(filename);%0A%09%09%09module._compile(code, filename);%0A%09%09%7D,%0A
 %09%09_c

Remove unused "streamliners" in underscored.js.