 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)
+collector = (callback) ->
 
-getOnWin32 = (callback) ->
+  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)
 
   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, config) ->
    return callback(error) if error
    indexes = Object.keys(config)
    async.map indexes,
      (index, callback) ->
        getInterface index, (error, iface) ->
          callback(error, {index: index, iface: iface} )
      (error, list) ->
        data = {}
        for e in list
          data[e.iface] = config[e.index]
        callback(null, data)

module.exports =
  collector: getOnWin32


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