// Set up module to run in browser and in Node.js
// Based loosely on https://github.com/umdjs/umd/blob/master/nodeAdapter.js
if (typeof module === 'object' && typeof define !== 'function') {
    // We are in Node.js

    var ChildProcess = require('child_process');
    var Promise = require('promise');

    var api = function(cmdList, method, url, data) {
        return new Promise(function(fulfill, reject) {
            var params = cmdList.concat(['--json']);
            var conda = ChildProcess.spawn('conda', params, {});
            var buffer = [];
            conda.stdout.on('data', function(data) {
                buffer.push(data);
            });
            conda.on('close', function() {
                try {
                    fulfill(JSON.parse(buffer.join('')));
                }
                catch(ex) {
                    reject({
                        'exception': ex,
                        'result': buffer.join('')
                    });
                }
            });
        });
    };

    // if (process.argv.length == 3 && process.argv[2] == '--server') {
    //     var express = require('express');
    //     var app = express();
    //     process.argv = [];
    //     console.log('running as server')

    //     var _url = null;
    //     var intercept = function(cmdList, method, url, data) {
    //         console.log('Setting up /api/' + url.join('/'));
    //         _url = '/api/' + url.join('/');
    //     };

    //     var intercepted = factory(intercept);
    //     var normal = factory(api);
    //     for (var name in intercepted) {
    //         var thing = intercepted[name];
    //         if (typeof thing === 'function') {
    //             thing();
    //             (function(url, name) {
    //                 app.get(url, function(req, res) {
    //                     normal[name]().then(function(data) {
    //                         res.send(data);
    //                     });
    //                 });
    //             })(_url, name);
    //         }
    //     }

    //     var fs = require('fs');
    //     app.get('/', function(req, res) {
    //         res.sendfile(__dirname + '/test.html');
    //     });
    //     app.get('/conda.js', function(req, res) {
    //         res.sendfile(__dirname + '/conda.js');
    //     });
    //     app.listen(8000);
    // }

    module.exports = factory(api);
}
else {
    // We are in the browser
    var api = function(cmdList, method, url, data) {
        return Promise.resolve($.ajax({
            data: JSON.stringify(data),
            dataType: 'json',
            type: method,
            url: window.conda.API_ROOT + url.join('/')
        }));
    };

    window.conda = factory(api);
}

function factory(api) {
    var Env = (function() {
        function Env(name, prefix) {
            this.name = name;
            this.prefix = prefix;
        }

        Env.prototype.linked = function() {
            return api(['list', '--prefix', this.prefix],
                       'get', [this.name, 'linked']);
        };

        Env.prototype.revisions = function() {
        };

        Env.prototype.install = function() {
        };

        Env.prototype.remove = function() {
        };

        Env.create = function(name) {
        };
        return Env;
    });

    var Package = (function() {
        function Package() {
        }

        return Package;
    });

    var info = function() {
        return api(['info'], 'get', ['info']);
    };

    return {
        info: info,
        API_ROOT: '/api/'
    };
}

@@ -456,24 +456,67 @@
 '--json'%5D);%0A
+            console.log('Running', params)%0A
             
@@ -3082,16 +3082,24 @@
 'get', %5B
+'envs', 
 this.nam
@@ -3164,32 +3164,170 @@
  = function() %7B%0A
+            return api(%5B'list', '--prefix', this.prefix, '--revisions'%5D,%0A                       'get', %5B'envs', this.name, 'revisions'%5D);%0A
         %7D;%0A%0A    
@@ -3355,36 +3355,173 @@
 tall = function(
+pkg
 ) %7B%0A
+            return api(%5B'install', '--prefix', this.prefix, pkg%5D,%0A                       'get', %5B'envs', this.name, 'install', pkg%5D);%0A
         %7D;%0A%0A    
@@ -3548,32 +3548,35 @@
 move = function(
+pkg
 ) %7B%0A        %7D;%0A%0A
@@ -3575,49 +3575,727 @@
     
-%7D;%0A%0A        Env.create = function(name) %7B
+    return api(%5B'remove', '--prefix', this.prefix, pkg%5D,%0A                       'get', %5B'envs', this.name, 'install', pkg%5D);%0A        %7D;%0A%0A        Env.create = function(name) %7B%0A        %7D;%0A%0A        Env.getEnvs = function() %7B%0A            return info().then(function(info) %7B%0A                var envs = %5Bnew Env('root', info%5B'default_prefix'%5D)%5D;%0A                var prefixes = info%5B'envs'%5D;%0A                for (var i = 0; i %3C prefixes.length; i++) %7B%0A                    var prefix = prefixes%5Bi%5D;%0A                    var name = prefix.split('/'); // Windows?%0A                    name = name%5Bname.length - 1%5D;%0A                    envs.push(new Env(name, prefix));%0A                %7D%0A                return envs;%0A            %7D);
 %0A   
@@ -4320,32 +4320,34 @@
 turn Env;%0A    %7D)
+()
 ;%0A%0A    var Packa
@@ -4554,16 +4554,34 @@
 : info,%0A
+        Env: Env,%0A
         

Add Env object/methods