   onConfig: (device) =>
     @setOptions device.options
 
-  setOptions: (@options={}) =>
+  setOptions: (options={}) =>
+    @options = options
+    projectDirName = path.basename @options.gitRepo, '.git'
+    @dir = path.join @tmpDir, projectDirName
+
   setUuid: (@uuid={}) =>
 
   startCommand: (callback=->) =>

    @messageSchema = MESSAGE_SCHEMA
    @optionsSchema = OPTIONS_SCHEMA
    @commands = {start: @startCommand, stop: @stopCommand}
    @dir = path.join __dirname, 'tmp'

  onMessage: (message) =>
    payload = message.payload;
    command = @commands[payload.command]
    return unless command?
    command (error, stdout) =>
      if error?
        @emit 'error', error
        @emit 'message', error: error
        return

      @runCommand @options.start, callback

  setup: (callback=->) =>
    @setupDirs (error) =>
      return callback error if error?
      @runCommand @options.setup, callback

  setupDirs: (callback=->) =>
    mkdirp @tmpDir, (error) =>
      return callback error if error?
      command =  "git clone #{@options.gitRepo}"
      @runCommand command, @tmpDir, callback

  runCommand: (command, cwd, callback=->) =>
    if _.isFunction cwd
      callback = cwd
      cwd      = @dir

    child_process.exec command, cwd: cwd, (error, stdout, stderr) =>
      if error?
        error.stdout = stdout
        error.stderr = stderr
