 fs       = require 'fs-extra'
 path     = require 'path'
 
-class LXC
-  
-  constructor: (@name) ->
-    @pathToContainers = "/var/lib/lxc"
-    console.log "......LXC constructor",  @name
+class Deployment
+  @pathToContainers = "/var/lib/lxc"
+
+  constructor: (@id, @kiteName, @zipUrl) ->
+    console.log "......Deployment constructor"
+    @kitesPath = path.join Deployment.pathToContainers, @id, "overlay", "opt", "kites"
 
   createLxc: (callback) ->
-    cmd = spawn "create-lxc", [@name]
+    cmd = spawn "create-lxc", [@kiteName]
     console.log 'create lxc'
     cmd.stdout.on 'data', (data) ->
       console.log 'stdout: ', data.toString()

      console.log 'create lxc process exited with code ', code
      callback()

class Deployment
  constructor: (@zipUrl, @name, @lxc) ->
    console.log "......Deployment constructor",  @lxc
    @kitePath = path.join @lxc.pathToContainers, @lxc.name, "overlay", "opt", "kites"

  downloadAndExtractKite: (callback) ->
    {kitePath, zipUrl, name} = this
    fs.mkdirsSync(kitePath) ## TODO mkdir creates 0777, exists check
    
    filepath = path.join kitePath, "#{name}.zip"
    file     = fs.createWriteStream filepath

    console.log "downloading url", zipUrl, " to ", filepath

          
          cmd.on 'close', (code) ->
            console.log 'unzip process exited with code ', code
            callback()

  runKite: () ->
    kiteInLxc = path.join "/opt", "kites", @kiteName
    runner    = path.join(@pathToContainers, @name, "overlay", "opt", "runner")
    fs.writeFileSync runner, "#!/bin/bash -l \n export HOME=/root \n /usr/sbin/kd.sh #{kiteInLxc} > /tmp/kd.out & \n"
    fs.chmodSync runner, "0755"
    cmd = spawn "/usr/bin/lxc-start", ["-d", "-n", @name]
    
    cmd.stdout.on 'data', (data) ->
