 fs = require 'fs'
 path = require 'path'
 
-updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'Update.exe')
+rootAtomFolder = path.resolve(process.execPath, '..', '..')
+binFolder = path.resolve(process.execPath, 'bin')
+updateDotExe = path.join(rootAtomFolder, 'Update.exe')
 exeName = path.basename(process.execPath)
 
 # Registry keys used for context menu

directoryKeyPath = 'HKCU\\Software\\Classes\\directory\\shell\\Atom'
backgroundKeyPath = 'HKCU\\Software\\Classes\\directory\\background\\shell\\Atom'
environmentKeyPath = 'HKCU\\Environment'

spawn = (command, args, callback) ->
  spawnedProcess = ChildProcess.spawn(command, args)

  getPath = (callback) ->
    spawnReg ['query', environmentKeyPath, '/v', 'Path'], (error, stdout) ->
      lines = stdout.split(/[\r\n]+/).filter (line) -> line
      segments = lines[lines.length - 1]?.split('    ')
      if segments[1] is 'Path' and segments.length >= 3
        envPath = segments?[3..].join('    ')
        callback(null, envPath)
      else
        callback(new Error('Registry query for PATH failed'))

  getPath (error, envPath) ->
    if error?
      callback(error)
    else
      segments = envPath.split(';')
      if segments.indexOf(binFolder) is -1
        segments.push(binFolder)

        args = ['add', pathKeyPath, segments.join(';'), '/f']
        spawnReg(args, callback)

exports.spawn = spawnUpdate

