// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var fs, iced, mkdir_p, os, path, prng, rm_r, util, __iced_k, __iced_k_noop;

  iced = require('iced-runtime').iced;
  __iced_k = __iced_k_noop = function() {};

  path = require('path');

  fs = require('fs');

  os = require('os');

  util = require('./util');

  prng = require('crypto').prng;

  exports.mkdir_p = mkdir_p = function(d, mode, cb) {
    var cwd, err, made, p, parts, so, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    if (mode == null) {
      mode = 0x1ed;
    }
    parts = d.split(path.sep);
    cwd = [];
    if (parts.length && (parts[0].length === 0)) {
      cwd.push(path.sep);
    }
    err = null;
    made = 0;
    (function(_this) {
      return (function(__iced_k) {
        var _i, _len, _ref, _results, _while;
        _ref = parts;
        _len = _ref.length;
        _i = 0;
        _results = [];
        _while = function(__iced_k) {
          var _break, _continue, _next;
          _break = function() {
            return __iced_k(_results);
          };
          _continue = function() {
            return iced.trampoline(function() {
              ++_i;
              return _while(__iced_k);
            });
          };
          _next = function(__iced_next_arg) {
            _results.push(__iced_next_arg);
            return _continue();
          };
          if (!(_i < _len)) {
            return _break();
          } else {
            p = _ref[_i];
            if (err == null) {
              cwd.push(p);
              d = path.join.apply(null, cwd);
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                });
                fs.stat(d, __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      err = arguments[0];
                      return so = arguments[1];
                    };
                  })(),
                  lineno: 19
                }));
                __iced_deferrals._fulfill();
              })(function() {
                (function(__iced_k) {
                  if ((err != null) && (err.code === 'ENOENT')) {
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                      });
                      fs.mkdir(d, mode, __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            return err = arguments[0];
                          };
                        })(),
                        lineno: 21
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      return __iced_k(err == null ? made++ : void 0);
                    });
                  } else {
                    return __iced_k((err == null) && (typeof so !== "undefined" && so !== null) && !so.isDirectory() ? err = new Error("Path component " + d + " isn't a directory") : void 0);
                  }
                })(_next);
              });
            } else {
              return _continue();
            }
          }
        };
        _while(__iced_k);
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, made);
      };
    })(this));
  };

  exports.rm_r = rm_r = function(d, cb) {
    var err, file, files, full, stat, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
        });
        fs.readdir(d, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              err = arguments[0];
              return files = arguments[1];
            };
          })(),
          lineno: 30
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        (function(__iced_k) {
          if (err == null) {
            (function(__iced_k) {
              var _i, _len, _ref, _results, _while;
              _ref = files;
              _len = _ref.length;
              _i = 0;
              _results = [];
              _while = function(__iced_k) {
                var _break, _continue, _next;
                _break = function() {
                  return __iced_k(_results);
                };
                _continue = function() {
                  return iced.trampoline(function() {
                    ++_i;
                    return _while(__iced_k);
                  });
                };
                _next = function(__iced_next_arg) {
                  _results.push(__iced_next_arg);
                  return _continue();
                };
                if (!(_i < _len)) {
                  return _break();
                } else {
                  file = _ref[_i];
                  if (!(file === "." || file === "..")) {
                    full = path.join(d, file);
                    (function(__iced_k) {
                      __iced_deferrals = new iced.Deferrals(__iced_k, {
                        parent: ___iced_passed_deferral,
                        filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                      });
                      fs.stat(full, __iced_deferrals.defer({
                        assign_fn: (function() {
                          return function() {
                            err = arguments[0];
                            return stat = arguments[1];
                          };
                        })(),
                        lineno: 34
                      }));
                      __iced_deferrals._fulfill();
                    })(function() {
                      (function(__iced_k) {
                        if (err != null) {
                          return __iced_k();
                        } else {
                          (function(__iced_k) {
                            if (stat.isDirectory()) {
                              (function(__iced_k) {
                                __iced_deferrals = new iced.Deferrals(__iced_k, {
                                  parent: ___iced_passed_deferral,
                                  filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                                });
                                rm_r(full, __iced_deferrals.defer({
                                  assign_fn: (function() {
                                    return function() {
                                      return err = arguments[0];
                                    };
                                  })(),
                                  lineno: 37
                                }));
                                __iced_deferrals._fulfill();
                              })(__iced_k);
                            } else {
                              (function(__iced_k) {
                                __iced_deferrals = new iced.Deferrals(__iced_k, {
                                  parent: ___iced_passed_deferral,
                                  filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                                });
                                fs.unlink(full, __iced_deferrals.defer({
                                  assign_fn: (function() {
                                    return function() {
                                      return err = arguments[0];
                                    };
                                  })(),
                                  lineno: 39
                                }));
                                __iced_deferrals._fulfill();
                              })(__iced_k);
                            }
                          })(__iced_k);
                        }
                      })(function() {
                        (function(__iced_k) {
                          if (err != null) {
                            (function(__iced_k) {
_break()
                            })(__iced_k);
                          } else {
                            return __iced_k();
                          }
                        })(_next);
                      });
                    });
                  } else {
                    return _continue();
                  }
                }
              };
              _while(__iced_k);
            })(__iced_k);
          } else {
            return __iced_k();
          }
        })(function() {
          (function(__iced_k) {
            if (typeof err === "undefined" || err === null) {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/iced/iced-utils/src/fs.iced"
                });
                fs.rmdir(d, __iced_deferrals.defer({
                  assign_fn: (function() {
                    return function() {
                      return err = arguments[0];
                    };
                  })(),
                  lineno: 42
                }));
                __iced_deferrals._fulfill();
              })(__iced_k);
            } else {
              return __iced_k();
            }
          })(function() {
            return cb(err);
          });
        });
      };
    })(this));
  };

  exports.write_tmp_file = function(_arg, cb) {
    var base, data, dir, encoding, err, flag, fn, mode, opts, sffx, suffix_len, ___iced_passed_deferral, __iced_deferrals, __iced_k;
    __iced_k = __iced_k_noop;
    ___iced_passed_deferral = iced.findDeferral(arguments);
    data = _arg.data, dir = _arg.dir, base = _arg.base, mode = _arg.mode, encoding = _arg.encoding, suffix_len = _arg.suffix_len;
    suffix_len || (suffix_len = 12);
    mode || (mode = 0x1a4);
    encoding || (encoding = 'utf8');
    dir || (dir = os.tmpdir());
    flag = "wx";
    sffx = util.base64u.encode(prng(suffix_len));
    fn = path.join(dir, [base, sffx].join("."));
    opts = {
      mode: mode,
      encoding: encoding,
      flag: flag
    };
    (function(_this) {
      return (function(__iced_k) {
        __iced_deferrals = new iced.Deferrals(__iced_k, {
          parent: ___iced_passed_deferral,
          filename: "/Users/max/src/iced/iced-utils/src/fs.iced",
          funcname: "write_tmp_file"
        });
        fs.writeFile(fn, data, opts, __iced_deferrals.defer({
          assign_fn: (function() {
            return function() {
              return err = arguments[0];
            };
          })(),
          lineno: 56
        }));
        __iced_deferrals._fulfill();
      });
    })(this)((function(_this) {
      return function() {
        return cb(err, fn);
      };
    })(this));
  };

}).call(this);
