 {Directory} = require 'atom'
 DaemonItem = require "./daemon-item"
+{BufferedProcess} = require 'atom'
 
 module.exports =
 class ScanDeamons

                                                fileNameWithoutAfterDot, false, false, false
    atom.notifications.addInfo "#{daemonName} added"

#Scan Class for Brew
class ScanDaemonsBrew
  constructor: (@scanDeamons) ->
    dir = new Directory('/usr/local/opt/')
    if dir.isDirectory()
      atom.notifications.addInfo "Scan Brew like Daemons"
      @searchPlist dir

  searchPlist: (dir) ->
    re = /^([\w\.]+)\.plist$/
    searchPlist = (dir) =>
      dir.getEntries (err,entries) =>
        for entrie in entries
          if entrie.isDirectory()
            searchPlist entrie
          else
            result = re.exec(entrie.getBaseName())
            if result != null
              regNewPlist entrie, result[1]

    regNewPlist = (file,fileNameWithoutAfterDot) =>
      @scanDeamons.addDaemon file.getParent().getBaseName(), file.path, fileNameWithoutAfterDot
    searchPlist dir

    @defineScanFunction()

  defineScanFunction: ->
    if process.platform == "darwin" #mac
      @scanFunction = ScanDaemonsBrew
    else if /^linux/.test(process.platform) #linux
      @isLinuxDistribution "Ubuntu", =>
        @scanFunction = ScanDaemonsUbuntu
    #else if /^win/.test(process.platform) #win
      #atom.notifications.addInfo "There is no scan algorithm for your #{process.platform} platform, plaese add this!"
      #atom.notifications.addInfo "Add manual daemons with CMD+SHIFT+P -> Smart Daemon Control: New Daemon"
      #@daemonManagement.newDaemon()

  run: ->
    if @scanFunction?
      new @scanFunction(this)
    else
      #console.log process
      atom.notifications.addInfo "There is no scan-algorithm for your OS #{process.platform} right now."

  thereIsScanDaemonForOs: ->
    if @scanFunction == null
