var correl8 = require("correl8");
var fs = require('fs');
var lockFile = require('lockfile');
var nopt = require('nopt');
var noptUsage = require("nopt-usage");
var path = require('path');
var prompt = require('prompt');

var knownOpts = {
  'adapter': [String],
  'settings': Boolean,
  'help': Boolean,
  'initialize': Boolean,
  'clear': Boolean,
  'from': Date,
  'to': Date
},
shortHands = {
  'h': ['--help'],
  'i': ['--initialize'],
  'c': ['--clear'],
  'u': ['--url'],
  'f': ['--from'],
  's': ['--settings'],
  't': ['--to']
},
description = {
  'adapter': ' Name of the adapter to use (required)',
  'settings': ' Configure adapter and exit',
  'help': ' Display this usage text and exit',
  'init': ' Create the index and exit',
  'clear': ' Clear all data in the index',
  'from': ' Start date as YYYY-MM-DD',
  'to': ' End date as YYYY-MM-DD'
},
options = nopt(knownOpts, shortHands, process.argv, 2);
if (!options['adapter']) {
  console.error('Required parameter missing: adapter');
}
var firstDate = options['from'] || null;
var lastDate = options['to'] || null;
var adapter = require(options['adapter']);
var c8 = correl8(adapter.sensorName);
var types = adapter.types;

var lock = '/tmp/correl8-adapter-lock-' + adapter; // make configurable?
lockFile.lock(lock, {}, function(er) {
  if (er) {
    c8.release();
    console.error('Lockfile ' + lock + ' exists!');
    process.exit;
  }
  if (options['help']) {
    console.log('Usage: ');
    console.log(noptUsage(knownOpts, shortHands, description));
  }
  else if (options['settings']) {
    c8.config().then(function(res) {
      var conf = c8.trimResults(res);
      for (prop in adapter.promptProps.properties) {
        if (conf[prop]) {
          adapter.promptProps.properties[prop].default = conf[prop];
        }
      }
      prompt.start();
      prompt.message = '';
      prompt.get(adapter.promptProps, function (err, result) {
        if (err) {
          console.trace(err);
        }
        else {
          // console.log(c8.config);
          adapter.storeConfig(c8, result);
        }
      });
    })
  }
  else if (options['clear']) {
    for (var i=0; i<types.length; i++) {
      c8.type(types[i].name).clear().then(function(res) {
        console.log('Index for type ' + types[i] + ' cleared.');
      }).catch(function(error) {
        console.trace(error);
        c8.release();
      });
    }
  }
  else if (options['initialize']) {
    for (var i=0; i<types.length; i++) {
      var type = types[i];
      var fields = type.fields;
      // console.log('Initializing ' + type.name + ' with ' + JSON.stringify(fields));
      c8.type(type.name).init(fields).then(function(res) {
        // console.log('Index initialized.');
      }).catch(function(error) {
        console.trace(error);
        c8.release();
      });
    }
  }
  else {
    c8.isInitialized().then(function() {
      c8.config().then(function(res) {
        // console.log(res);
        var conf = c8.trimResults(res);
        var opts = {firstDate: firstDate, lastDate: lastDate};
        if (conf) {
          // console.log(adapter);
          adapter.importData(c8, conf, opts).then(function() {
            console.log('Import succesful!');
          }).catch(function(error) {
            console.log('Import unsuccesful!');
            console.trace(error);
          });
        }
        else {
          var msg = 'Configure first! Run\n node ' + process.argv[1] +
            ' --adapter ' + options['adapter'] + ' --settings'
          console.log(msg);
          // console.log('Usage: ');
          // console.log(noptUsage(knownOpts, shortHands, description));
          c8.release();
        }
      });
    }).catch(function(error) {
      console.trace(error);
      c8.release();
    });
  }
  lockFile.unlock(lock, function (er) {
    if (er) {
      console.error('Cannot release lockfile ' + lock + '!');
    }
  })
});

@@ -2120,24 +2120,46 @@
 'clear'%5D) %7B%0A
+    var c8array = %5B%5D;%0A
     for (var
@@ -2147,35 +2147,35 @@
  = %5B%5D;%0A    for (
-var
+let
  i=0; i%3Ctypes.le
@@ -2187,32 +2187,78 @@
  i++) %7B%0A      c8
+array%5Bi%5D = new correl8(type);%0A      c8array%5Bi%5D
 .type(types%5Bi%5D.n
@@ -2339,16 +2339,21 @@
 types%5Bi%5D
+.name
  + ' cle
@@ -2424,38 +2424,16 @@
 error);%0A
-        c8.release();%0A
       %7D)
@@ -2476,24 +2476,46 @@
 ialize'%5D) %7B%0A
+    var c8array = %5B%5D;%0A
     for (var
@@ -2511,19 +2511,19 @@
    for (
-var
+let
  i=0; i%3C
@@ -2549,19 +2549,19 @@
 %7B%0A      
-var
+let
  type = 
@@ -2568,24 +2568,29 @@
 types%5Bi%5D
+.name
 ;%0A      
 var fiel
@@ -2581,19 +2581,19 @@
 ;%0A      
-var
+let
  fields 
@@ -2598,16 +2598,20 @@
 s = type
+s%5Bi%5D
 .fields;
@@ -2654,21 +2654,16 @@
 ' + type
-.name
  + ' wit
@@ -2703,26 +2703,124 @@
     
-c8.type(type.name)
+// new c8 instance for each type for parallel async execution%0A      c8array%5Bi%5D = new correl8(type);%0A      c8array%5Bi%5D
 .ini
@@ -2849,35 +2849,32 @@
 n(res) %7B%0A       
- //
  console.log('In
@@ -2876,16 +2876,29 @@
 g('Index
+ ' + type + '
  initial
@@ -2969,38 +2969,16 @@
 error);%0A
-        c8.release();%0A
       %7D)

use multiple c8 instances inside loops