// Generated by IcedCoffeeScript 1.7.1-c
(function() {
  var SummarizedItem, XPlatformHash, constants, finfo_cache, iced, item_types, make_esc, path, utils, __iced_k, __iced_k_noop;

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

  path = require('path');

  make_esc = require('iced-error').make_esc;

  constants = require('./constants');

  item_types = require('./constants').item_types;

  utils = require('./utils');

  XPlatformHash = require('./x_platform_hash');

  finfo_cache = require('./file_info_cache');

  exports.SummarizedItem = SummarizedItem = (function() {
    function SummarizedItem(_arg) {
      var codesign, depth, fname, parent_path;
      parent_path = _arg.parent_path, fname = _arg.fname, codesign = _arg.codesign, depth = _arg.depth;
      this.parent_path = parent_path;
      this.fname = fname;
      this.codesign = codesign;
      this.depth = depth || 0;
      this.item_type = null;
      this.realpath = null;
      this.link = null;
      this.contents = null;
      this.finfo = null;
      this.hash = null;
      this.link_hash = null;
      this.binary = false;
    }

    SummarizedItem.prototype.load_traverse = function(cb) {
      var esc, f, fnames, ignore, p, si, subpath, ___iced_passed_deferral, __iced_deferrals, __iced_k;
      __iced_k = __iced_k_noop;
      ___iced_passed_deferral = iced.findDeferral(arguments);
      esc = make_esc(cb, "SummarizedItem::load");
      p = path.join(this.codesign.root_dir, this.parent_path, this.fname);
      this.realpath = path.resolve(p);
      (function(_this) {
        return (function(__iced_k) {
          __iced_deferrals = new iced.Deferrals(__iced_k, {
            parent: ___iced_passed_deferral,
            filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
            funcname: "SummarizedItem.load_traverse"
          });
          finfo_cache(_this.realpath, esc(__iced_deferrals.defer({
            assign_fn: (function(__slot_1) {
              return function() {
                return __slot_1.finfo = arguments[0];
              };
            })(_this),
            lineno: 32
          })));
          __iced_deferrals._fulfill();
        });
      })(this)((function(_this) {
        return function() {
          _this.item_type = _this.finfo.item_type;
          _this.link = _this.finfo.link;
          _this.binary = _this.finfo.is_binary();
          (function(__iced_k) {
            if (_this.item_type === item_types.FILE) {
              (function(__iced_k) {
                __iced_deferrals = new iced.Deferrals(__iced_k, {
                  parent: ___iced_passed_deferral,
                  filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
                  funcname: "SummarizedItem.load_traverse"
                });
                _this.finfo.hash(constants.hash.ALG, constants.hash.ENCODING, esc(__iced_deferrals.defer({
                  assign_fn: (function(__slot_1) {
                    return function() {
                      return __slot_1.hash = arguments[0];
                    };
                  })(_this),
                  lineno: 39
                })));
                __iced_deferrals._fulfill();
              })(__iced_k);
            } else {
              (function(__iced_k) {
                if (_this.item_type === item_types.DIR) {
                  _this.contents = [];
                  (function(__iced_k) {
                    __iced_deferrals = new iced.Deferrals(__iced_k, {
                      parent: ___iced_passed_deferral,
                      filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
                      funcname: "SummarizedItem.load_traverse"
                    });
                    _this.finfo.dir_contents(esc(__iced_deferrals.defer({
                      assign_fn: (function() {
                        return function() {
                          return fnames = arguments[0];
                        };
                      })(),
                      lineno: 42
                    })));
                    __iced_deferrals._fulfill();
                  })(function() {
                    (function(__iced_k) {
                      var _i, _len, _ref, _results, _while;
                      _ref = fnames;
                      _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 {
                          f = _ref[_i];
                          subpath = path.join(_this.realpath, f);
                          (function(__iced_k) {
                            __iced_deferrals = new iced.Deferrals(__iced_k, {
                              parent: ___iced_passed_deferral,
                              filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
                              funcname: "SummarizedItem.load_traverse"
                            });
                            _this.codesign._should_ignore(subpath, esc(__iced_deferrals.defer({
                              assign_fn: (function() {
                                return function() {
                                  return ignore = arguments[0];
                                };
                              })(),
                              lineno: 45
                            })));
                            __iced_deferrals._fulfill();
                          })(function() {
                            (function(__iced_k) {
                              if (!ignore) {
                                si = _this.subitem(f);
                                (function(__iced_k) {
                                  __iced_deferrals = new iced.Deferrals(__iced_k, {
                                    parent: ___iced_passed_deferral,
                                    filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
                                    funcname: "SummarizedItem.load_traverse"
                                  });
                                  si.load_traverse(esc(__iced_deferrals.defer({
                                    lineno: 48
                                  })));
                                  __iced_deferrals._fulfill();
                                })(function() {
                                  return __iced_k(_this.contents.push(si));
                                });
                              } else {
                                return __iced_k();
                              }
                            })(_next);
                          });
                        }
                      };
                      _while(__iced_k);
                    })(function() {
                      return __iced_k(_this.contents.sort(function(a, b) {
                        return a.fname.localeCompare(b.fname, 'us');
                      }));
                    });
                  });
                } else {
                  (function(__iced_k) {
                    if (_this.item_type === item_types.SYMLINK) {
                      (function(__iced_k) {
                        __iced_deferrals = new iced.Deferrals(__iced_k, {
                          parent: ___iced_passed_deferral,
                          filename: "/Users/max/src/keybase/codesign/src/summarized_item.iced",
                          funcname: "SummarizedItem.load_traverse"
                        });
                        _this.finfo.link_hash(constants.hash.ALG, constants.hash.ENCODING, esc(__iced_deferrals.defer({
                          assign_fn: (function(__slot_1) {
                            return function() {
                              return __slot_1.link_hash = arguments[0];
                            };
                          })(_this),
                          lineno: 54
                        })));
                        __iced_deferrals._fulfill();
                      })(__iced_k);
                    } else {
                      return __iced_k();
                    }
                  })(__iced_k);
                }
              })(__iced_k);
            }
          })(function() {
            return cb();
          });
        };
      })(this));
    };

    SummarizedItem.prototype.subitem = function(f) {
      return new SummarizedItem({
        fname: f,
        parent_path: this.parent_path.length ? "" + this.parent_path + "/" + this.fname : this.fname,
        codesign: this.codesign,
        depth: this.depth + 1
      });
    };

    SummarizedItem.prototype.obj_info = function() {

      /*
      anything NOT preceeded with an underscore is expected to survive a serialization/deserialization
      for signing and loading.
       */
      var info;
      info = {
        parent_path: this.parent_path,
        item_type: this.item_type,
        fname: this.fname,
        path: this.parent_path.length ? "" + this.parent_path + "/" + this.fname : this.fname,
        exec: this.finfo.is_user_executable_file(),
        _depth: this.depth
      };
      switch (this.item_type) {
        case item_types.FILE:
          info.hash = this.hash;
          info.size = this.finfo.lstat.size;
          info._possible_win_link = this.finfo.possible_win_link;
          break;
        case item_types.SYMLINK:
          info.link = this.link;
          info._link_hash = this.link_hash;
      }
      return info;
    };

    SummarizedItem.prototype.walk_to_array = function(_res) {

      /*
      returns an array of all items starting at this point,
      sorted in a predictable way;
       */
      var c, _i, _len, _ref;
      _res || (_res = []);
      if (this.item_type === item_types.DIR) {
        _res.push(this.obj_info());
        _ref = this.contents;
        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
          c = _ref[_i];
          c.walk_to_array(_res);
        }
      } else {
        _res.push(this.obj_info());
      }
      return _res;
    };

    return SummarizedItem;

  })();

}).call(this);
