// Generated by CoffeeScript 1.6.3
(function() {
  var TaskGroup, fsUtil, pathUtil, safefs, _base, _ref;

  fsUtil = require('graceful-fs');

  pathUtil = require('path');

  TaskGroup = require('taskgroup').TaskGroup;

  if (global.safefsGlobal == null) {
    global.safefsGlobal = {};
  }

  if ((_base = global.safefsGlobal).pool == null) {
    _base.pool = new TaskGroup().setConfig({
      concurrency: (_ref = process.env.NODE_MAX_OPEN_FILES) != null ? _ref : 100,
      pauseOnError: false
    }).run();
  }

  safefs = {
    openFile: function(fn) {
      global.safefsGlobal.pool.addTask(fn);
      return safefs;
    },
    closeFile: function() {
      console.log('safefs.closeFile has been deprecated, please use the safefs.openFile completion callback to close files');
      return safefs;
    },
    getParentPathSync: function(p) {
      var parentPath;
      parentPath = p.replace(/[\/\\]$/, '').replace(/[\/\\][^\/\\]+$/, '');
      return parentPath;
    },
    ensurePath: function(path, options, next) {
      if (next == null) {
        next = options;
        options = null;
      }
      if (options == null) {
        options = {};
      }
      if (options.mode == null) {
        options.mode = null;
      }
      safefs.exists(path, function(exists) {
        var parentPath;
        if (exists) {
          return next(null, true);
        }
        parentPath = safefs.getParentPathSync(path);
        return safefs.ensurePath(parentPath, options, function(err) {
          if (err) {
            return next(err, false);
          }
          return safefs.mkdir(path, options.mode, function(err) {
            return safefs.exists(path, function(exists) {
              if (!exists) {
                err = new Error("Failed to create the directory: " + path);
                return next(err, false);
              }
              return next(null, false);
            });
          });
        });
      });
      return safefs;
    },
    readFile: function(path, options, next) {
      if (next == null) {
        next = options;
        options = null;
      }
      safefs.openFile(function(closeFile) {
        return fsUtil.readFile(path, options, function(err, data) {
          closeFile();
          return next(err, data);
        });
      });
      return safefs;
    },
    writeFile: function(path, data, options, next) {
      if (next == null) {
        next = options;
        options = null;
      }
      safefs.ensurePath(pathUtil.dirname(path), options, function(err) {
        if (err) {
          return next(err);
        }
        return safefs.openFile(function(closeFile) {
          return fsUtil.writeFile(path, data, options, function(err) {
            closeFile();
            return next(err);
          });
        });
      });
      return safefs;
    },
    appendFile: function(path, data, options, next) {
      if (next == null) {
        next = options;
        options = null;
      }
      safefs.ensurePath(pathUtil.dirname(path), options, function(err) {
        if (err) {
          return next(err);
        }
        return safefs.openFile(function(closeFile) {
          return fsUtil.appendFile(path, data, options, function(err) {
            closeFile();
            return next(err);
          });
        });
      });
      return safefs;
    },
    mkdir: function(path, mode, next) {
      if (next == null) {
        next = mode;
        mode = null;
      }
      if (mode == null) {
        mode = 0x1ff & (~process.umask());
      }
      safefs.openFile(function(closeFile) {
        return fsUtil.mkdir(path, mode, function(err) {
          closeFile();
          return next(err);
        });
      });
      return safefs;
    },
    lstat: function(path, next) {
      safefs.openFile(function(closeFile) {
        return fsUtil.lstat(path, function(err, stat) {
          closeFile();
          return next(err, stat);
        });
      });
      return safefs;
    },
    stat: function(path, next) {
      safefs.openFile(function(closeFile) {
        return fsUtil.stat(path, function(err, stat) {
          closeFile();
          return next(err, stat);
        });
      });
      return safefs;
    },
    readdir: function(path, next) {
      safefs.openFile(function(closeFile) {
        return fsUtil.readdir(path, function(err, files) {
          closeFile();
          return next(err, files);
        });
      });
      return safefs;
    },
    unlink: function(path, next) {
      safefs.exists(path, function(exists) {
        if (exists === false) {
          return next();
        }
        return safefs.openFile(function(closeFile) {
          return fsUtil.unlink(path, function(err) {
            closeFile();
            return next(err);
          });
        });
      });
      return safefs;
    },
    rmdir: function(path, next) {
      safefs.openFile(function(closeFile) {
        return fsUtil.rmdir(path, function(err) {
          closeFile();
          return next(err);
        });
      });
      return safefs;
    },
    exists: function(path, next) {
      var exists;
      exists = fsUtil.exists || pathUtil.exists;
      safefs.openFile(function(closeFile) {
        return exists(path, function(exists) {
          closeFile();
          return next(exists);
        });
      });
      return safefs;
    },
    existsSync: function(path) {
      var existsSync, result;
      existsSync = fsUtil.existsSync || pathUtil.existsSync;
      result = existsSync(path);
      return result;
    }
  };

  module.exports = safefs;

}).call(this);
