 
   getDefaultGateway = (callback) ->
     getAdapterConfigs (error, records) ->
-      return callback(error) if error
+      return callback(error) if error?
       data = {}
       for record in records
         continue if not record['IPEnabled']?

exec = require('child_process').exec
async = require('async')

wmic = (cls, keys, callback) ->
  command = "wmic path #{cls} get #{keys.join(',')} /format:csv"
  exec command, (error, stdout, stderr) ->
    return callback(error) if error
    parseCSV stdout, {
      columns: true
      rowDelimiter: '\r\r\n'
      skip_empty_lines: true
      trim: true
    } , (error, records) ->
      callback(error, records)

getOnWin32 = (callback) ->

  getAdapterConfigs = (callback) ->
    wmic 'Win32_NetworkAdapterConfiguration',


  getInterface = (index, callback) ->
    getAdapters (error, records) ->
      return callback(error) if error?
      for record in records
        if record['Index'] == index
          return callback(null, record['NetConnectionID'])
      callback(new Error("inteface #{index} is not available"))

  getDefaultGateway (error, gateways) ->
    return callback(error) if error?
    indexes = Object.keys(gateways)
    async.map indexes,
      (index, callback) ->
        getInterface index, (error, name) ->
          callback(error) if error?
          callback(error, {index: index, name: name} )
      (error, ifaces) ->
        callback(error) if error?
        data = {}
        for iface in ifaces
          data[iface.name] = gateways[iface.index]
        callback(null, data)

module.exports =
  collector: collector
