#!/usr/bin/env node
var Stack = require('../stack')
var async = require('async');

var utils = require('component-consoler');
var path = require('path');
var fs = require('fs');
var config = require('../config')()
var etcdjs = require('etcdjs')
var Registry = require('../services/registry')
var Deployment = require('../deployment')

module.exports = function(options){

  options = options || {}

  var etcd = etcdjs(options.etcd || config.network.etcd)
  var deployment = Deployment(config, etcd)

  var deploy = {

    core:function(){

      var job = Registry(config)
      deployment.ensureJob(job, function(err, r){
        console.log('-------------------------------------------');
        console.dir(err)
        console.dir(r)
      })
    },

    stack:function(){
      var exists = fs.existsSync || path.existsSync;
      var folder = options.app || process.cwd()

      var stack = Stack(config, folder, {
        config:options.config,
        dev:options.dev,
        tag:options.tag,
        registry:options.registry
      })


      console.log('-------------------------------------------');
      console.log('do something with the stack here')

      console.dir(stack._bootOrder)
    },

    boot:function(){
      var cmd = options._[3]

      if(cmd=='core'){
        deploy.core()
      }
      else{
        deploy.stack()
      }
    }
  }

  return deploy
}

@@ -1041,17 +1041,61 @@
     %7D)%0A%0A
-%0A
+      stack.load(function(err)%7B%0A          %0A  
       co
@@ -1154,24 +1154,26 @@
 --');%0A      
+  
 console.log(
@@ -1211,24 +1211,26 @@
 re')%0A%0A      
+ 
+ 
 console.dir(
@@ -1246,16 +1246,30 @@
 otOrder)
+   %0A      %7D)%0A%0A
 %0A    %7D,%0A

load stack