var fs     = require('fs');
var path   = require('path');
var mkpath = require('mkpath');
// We don't want to include our own version of CoffeeScript since we don't know
// what version the parent relies on
var coffee;
try {
  coffee = require('coffee-script');
} catch (e) {
  // No coffee-script installed in the project; warn them and stop
  process.stderr.write("coffee-cache: No coffee-script package found.\n");
  return;
}

// Directory to store compiled source
var cacheDir = process.env['COFFEE_CACHE_DIR'] || '.coffee';
// Storing coffee's require extension for backup use
var coffeeExtension = require.extensions['.coffee'];

var wrapCompiled = function(content, filename, header, module) {
  // If we want to modify the header to include our source map, we must
  // overwrite the static Module.wrapper and then restore it. Ugly, but it's a
  // quick fix
  // We're taking module as an argument because we need to _compile with the module
  // of the thing being compiled, rather than with coffee-cache's module, or relative
  // paths will be all sorts of messed up
  var wrapMethod = module.constructor.wrap;
  module.constructor.wrap = function(script){
    // Restore the old method immediately in the event we have another require
    // before we exit the method
    module.constructor.wrap = wrapMethod;
    return (header || '') + wrapMethod(script);
  };
  var mod = module._compile(content, filename);
  // Restore in the event it was not called
  module.constructor.wrap = wrapMethod;
  return mod;
};

var sourceMapHeader = function(path) {
  return "//@ sourceMappingURL=" + path + "\n";
};

// Set up an extension map for .coffee files -- we are completely overriding
// CoffeeScript's since it only returns the compiled module.
require.extensions['.coffee'] = function(module, filename) {
  // First, convert the filename to something more digestible and use our cache
  // folder
  var cachePath = path.join(cacheDir, path.relative('.', filename)).replace(/\.coffee$/, '.js');
  var mapPath = path.resolve(cachePath.replace(/\.js$/, '.map'));
  var content, header;

  // Try and stat the files for their last modified time
  try {
    var sourceTime = fs.statSync(filename).mtime;
    var cacheTime = fs.statSync(cachePath).mtime;
    if (cacheTime > sourceTime) {
      // We can return the cached version
      content = fs.readFileSync(cachePath, 'utf8');
      // Check for a source map as well
      if (fs.existsSync(mapPath))
        header = sourceMapHeader(mapPath);
    }
  } catch (err) {
    // If the cached file was not created yet, this will fail, and that is okay
  }

  // If we don't have the content, we need to compile ourselves
  if (!content) {
    try {
      // Read from disk and then compile
      var compiled = coffee.compile(fs.readFileSync(filename, 'utf8'), {
        filename: filename,
        sourceMap: true
      });
      content = compiled.js;

      // Since we don't know which version of CoffeeScript we have, make sure
      // we handle the older versions that return just the compiled version.
      if (content == null)
        content = compiled;
      else if (compiled.v3SourceMap)
        header = sourceMapHeader(mapPath);

      // Try writing to cache
      mkpath.sync(path.dirname(cachePath));
      fs.writeFileSync(cachePath, content, 'utf8');
      if (mapPath)
        fs.writeFileSync(mapPath, compiled.v3SourceMap, 'utf8');
    } catch (err) {
      // Let's fail silently and use coffee's require if we need to
      if (!content)
        return coffeeExtension.apply(this, arguments);
    }
  }

  return wrapCompiled(content, filename, header, module);
};

// Export settings
module.exports = {
  setCacheDir: function(dir){
    cacheDir = dir;
    return this;
  }
};

@@ -634,989 +634,8 @@
 %5D;%0A%0A
-var wrapCompiled = function(content, filename, header, module) %7B%0A  // If we want to modify the header to include our source map, we must%0A  // overwrite the static Module.wrapper and then restore it. Ugly, but it's a%0A  // quick fix%0A  // We're taking module as an argument because we need to _compile with the module%0A  // of the thing being compiled, rather than with coffee-cache's module, or relative%0A  // paths will be all sorts of messed up%0A  var wrapMethod = module.constructor.wrap;%0A  module.constructor.wrap = function(script)%7B%0A    // Restore the old method immediately in the event we have another require%0A    // before we exit the method%0A    module.constructor.wrap = wrapMethod;%0A    return (header %7C%7C '') + wrapMethod(script);%0A  %7D;%0A  var mod = module._compile(content, filename);%0A  // Restore in the event it was not called%0A  module.constructor.wrap = wrapMethod;%0A  return mod;%0A%7D;%0A%0Avar sourceMapHeader = function(path) %7B%0A  return %22//@ sourceMappingURL=%22 + path + %22%5Cn%22;%0A%7D;%0A%0A
 // S
@@ -1097,24 +1097,16 @@
  content
-, header
 ;%0A%0A  // 
@@ -1397,125 +1397,8 @@
 ');%0A
-      // Check for a source map as well%0A      if (fs.existsSync(mapPath))%0A        header = sourceMapHeader(mapPath);%0A
     
@@ -2013,88 +2013,8 @@
 led;
-%0A      else if (compiled.v3SourceMap)%0A        header = sourceMapHeader(mapPath);
 %0A%0A  
@@ -2408,20 +2408,23 @@
 urn 
-wrapC
+module._c
 ompile
-d
 (con
@@ -2441,24 +2441,8 @@
 name
-, header, module
 );%0A%7D

Remove source map header additions