// Generated by CoffeeScript 1.7.1
(function() {
  var Datastore, child_process, db, gui, mkdirp, path;

  path = require('path');

  child_process = require('child_process');

  mkdirp = require('mkdirp');

  gui = require('nw.gui');

  Datastore = require('nw_nedb');

  db = new Datastore({
    filename: path.join(gui.App.dataPath, 'apps.db'),
    autoload: true
  });

  angular.module('maotama.controllers', []).controller('AppsListController', function($scope) {
    return db.find({}, (function(_this) {
      return function(err, docs) {
        if (err) {
          throw err;
        }
        $scope.apps = docs;
        return $scope.$digest();
      };
    })(this));
  }).controller('AppsShowController', [
    '$scope', '$routeParams', function($scope, $routeParams) {
      db.findOne({
        id: $routeParams.app_id
      }, function(err, doc) {
        var download, lang, _ref;
        if (err) {
          throw err;
        }
        $scope.app = doc;
        console.log($scope.app);
        $scope.app.default_installation_path = path.join(process.cwd(), 'apps', $scope.app.id);
        $scope.app.extra_languages = {};
        _ref = $scope.app.languages;
        for (lang in _ref) {
          download = _ref[lang];
          console.assert($scope.app.download.url);
          console.assert($scope.app.download.size);
          if (download !== true) {
            $scope.app.has_extra_languages = true;
            $scope.app.extra_languages[lang] = download;
          }
        }
        $scope.installing = {};
        return $scope.$digest();
      });
      $scope.add = function(installation) {
        $scope.app.installation = path.dirname(installation);
        return db.update({
          id: $scope.app.id
        }, {
          $set: {
            installation: $scope.app.installation
          }
        }, function(err, numReplaced, newDoc) {
          if (err) {
            throw err;
          }
          return $scope.$digest();
        });
      };
      $scope.install = function() {
        $scope.installing[$scope.app.id] = {
          process: 0,
          label: '正在连接'
        };
        return mkdirp(path.join(process.cwd(), 'cache'), function(err) {
          var aria2c;
          if (err) {
            throw err;
          }
          aria2c = child_process.spawn('bin/aria2c', ["--dir=cache", "--enable-color=false", "-c", $scope.app.download.url]);
          aria2c.stdout.setEncoding('utf8');
          aria2c.stderr.setEncoding('utf8');
          aria2c.stdout.on('data', function(data) {
            var connections, d, downloaded, eta, matches, progress, speed, total;
            console.log(data);
            if (matches = data.match(/\[(?:#\w+ )?(\w+)\/(\w+)\((\d+)%\) CN:(\d+) DL:(\w+) ETA:(\w+)\]/)) {
              d = matches[0], downloaded = matches[1], total = matches[2], progress = matches[3], connections = matches[4], speed = matches[5], eta = matches[6];
              $scope.installing[$scope.app.id].progress = progress;
              $scope.installing[$scope.app.id].label = "" + progress + "% " + speed + "/s";
              return $scope.$digest();
            }
          });
          aria2c.stderr.on('data', function(data) {
            return console.log('err: ', data);
          });
          return aria2c.on('close', function(code) {
            var p;
            if (code === 0) {
              $scope.installing[$scope.app.id].progress = 100;
              $scope.installing[$scope.app.id].label = '正在安装';
              $scope.$digest();
              p = path.join("apps/" + $scope.app.id);
              return mkdirp(p, function(err) {
                var p7zip;
                if (err) {
                  throw err;
                }
                console.log(["x", "-y", "-o" + p, "cache/" + (path.basename($scope.app.download.url))]);
                console.log(p7zip = child_process.spawn('bin/7za', ["x", "-y", "-o" + p, "cache/" + (path.basename($scope.app.download.url))]));
                p7zip.stdout.setEncoding('utf8');
                p7zip.stderr.setEncoding('utf8');
                p7zip.stdout.on('data', function(data) {
                  return console.log(data);
                });
                p7zip.stderr.on('data', function(data) {
                  return console.log('err: ', data);
                });
                return p7zip.on('close', function(code) {
                  console.log(code);
                  if (code === 0) {
                    delete $scope.installing[$scope.app.id];
                    return $scope.add(path.join(p, $scope.app.main));
                  } else {
                    throw "安装失败: " + code;
                  }
                });
              });
            } else {
              throw "下载失败: " + code;
            }
          });
        });
      };
      return $scope.run = function() {
        console.log($scope.app);
        $scope.app.running = true;
        return child_process.execFile($scope.app.main, {
          cwd: $scope.app.installation
        }, function(error, stdout, stderr) {
          if (error) {
            throw error;
          }
          $scope.app.running = false;
          return $scope.$digest();
        });
      };
    }
  ]);

  if (false) {
    db.remove({}, {
      multi: true
    }, function(err, numRemoved) {
      if (err) {
        throw err;
      }
      return db.insert([
        {
          "id": "th135",
          "category": "game",
          "name": "东方心绮楼",
          "network": {
            "proto": "udp",
            "port": 10800
          },
          "main": "th135.exe",
          "summary": "喵喵喵喵喵帕斯 nyanpass nyanpass",
          "download": {
            "url": "http://test2.my-card.in/downloads/maotama/th135_1.33.7z",
            "size": 313177239
          },
          "main": 'th135.exe',
          "languages": {
            "ja-JP": true,
            "zh-CN": {
              url: "http://test2.my-card.in/downloads/maotama/th135_lang_zh-CN_1.33.7z",
              size: 74751963
            }
          }
        }, {
          "id": "th123",
          "category": "game",
          "name": "东方非想天则",
          "network": {
            "proto": "udp",
            "port": 10800
          },
          "summary": "",
          "download": {
            url: "http://test2.my-card.in/downloads/maotama/th123_1.10a.7z",
            "size": 313177239
          },
          "main": 'th123.exe',
          "languages": {
            "ja-JP": true
          }
        }
      ], function(err, newDocs) {
        if (err) {
          throw err;
        }
        return console.log(newDocs);
      });
    });
  }

}).call(this);

//# sourceMappingURL=controllers.map
