text: // Generated by CoffeeScript 1.7.1
  (function() {
    var Q, api, config, db, extract, fs, os, path, prebundledManifestPromise, _expandFreezeDriedApps, _getFrozenApps, _manifestPromise;
  
    fs = require("fs-extra");
  
    os = require("os");
  
    path = require("path");
  
    api = require("./api.js");
  
    config = require("../../config/config.js");
  
    db = require("./db.js");
  
    extract = require("./extract.js");
  
    Q = require('q');
  
    _manifestPromise = void 0;
  
    prebundledManifestPromise = function() {
      var deferred, originalManifest;
      if (_manifestPromise) {
        return _manifestPromise;
      }
      deferred = Q.defer();
      _manifestPromise = deferred.promise;
      originalManifest = db.getItem(config.DbKeys.OriginalPrebundlingManifest);
      if (originalManifest) {
        deferred.resolve(JSON.parse(originalManifest));
      } else {
        _getFrozenApps(function(err, manifest) {
          console.log("Unzipped", (manifest ? manifest.length : 0), 'frozen apps');
          if (err) {
            return deferred.reject(err);
          } else {
            return deferred.resolve(manifest);
          }
        });
      }
      return _manifestPromise;
    };
  
    _getFrozenApps = function(cb) {
      var freezeDriedBundlePath;
      if (db.getItem(config.PrebundlingComplete)) {
        console.log("Prebundled apps already extracted.");
        return cb("Prebundled apps already extracted");
      }
      console.log("Expanding prebundled apps");
      freezeDriedBundlePath = _(config.FrozenAppPaths).find(function(bundlePath) {
        var err;
        try {
          console.log("Looking for prebundled path in: " + bundlePath);
          return fs.existsSync(bundlePath);
        } catch (_error) {
          err = _error;
          console.log("Prebundle path does not exist: " + bundlePath);
          return false;
        }
      });
      if (freezeDriedBundlePath) {
        console.log("\n\n\nFound freeze-dried preBundle: " + freezeDriedBundlePath);
        return _expandFreezeDriedApps(freezeDriedBundlePath, function(err, manifest) {
          var installErr;
          if (err) {
            console.error("Failed to expand prebundle. " + (err.stack || err));
            return typeof cb === "function" ? cb(new Error("Failed to expand prebundle.")) : void 0;
          } else if (manifest) {
            try {
              db.setItem(config.PrebundlingComplete, true);
              return typeof cb === "function" ? cb(null, manifest) : void 0;
            } catch (_error) {
              installErr = _error;
              console.error("Failed to initialize prebundled apps. " + (installErr.stack || installErr));
              return typeof cb === "function" ? cb(new Error("Failed to initialize prebundled apps.")) : void 0;
            }
          } else {
            console.error("Found prebundle but manifest is missing.");
            return typeof cb === "function" ? cb(new Error("Found prebundle but manifest is missing.")) : void 0;
          }
        });
      } else {
        console.log("No prebundle on this system.");
        return typeof cb === "function" ? cb(new Error("No prebundle on this system.")) : void 0;
      }
    };
  
    _expandFreezeDriedApps = function(bundlePath, cb) {
      var dest, manifest;
      dest = path.join(config.PlatformTempDirs[os.platform()], "frozen");
      manifest = void 0;
      return extract.unzipFile(bundlePath, dest, true, function(err) {
        if (err) {
          console.error("Failed to unzip " + bundlePath + ": " + (err.stack || err));
          return typeof cb === "function" ? cb(err) : void 0;
        } else {
          console.info("Unzipped prebundled apps at " + bundlePath + " to " + dest);
          try {
            console.log("Looking for prebundle manifest at " + path.join(dest, "myapps.json"));
            manifest = JSON.parse(fs.readFileSync(path.join(dest, "myapps.json"), {
              encoding: "utf8"
            }));
            if (manifest) {
              console.log("Caching prebundled manifest of", manifest.length, "apps");
              db.setItem(config.DbKeys.OriginalPrebundlingManifest, JSON.stringify(manifest));
              return typeof cb === "function" ? cb(null, manifest) : void 0;
            } else {
              return typeof cb === "function" ? cb(new Error("No freeze dried apps manifest found.")) : void 0;
            }
          } catch (_error) {
            err = _error;
            console.error("Corrupt myapps.json prebundled manifest: " + (err.stack || err));
            return typeof cb === "function" ? cb(err) : void 0;
          }
        }
      });
    };
  
    module.exports.prebundledManifestPromise = prebundledManifestPromise;
  
  }).call(this);
  