var csv = require('csv'),
  fs = require('fs'),
  path = require('path'),
  mongoose = require('mongoose'),
  CableType = require('../model/meta.js').CableType;

var fileName;
if (process.argv.length === 3) {
  fileName = process.argv[2];
} else {
  console.log(process.argv.length);
  console.warn('only needs one arguments');
  process.exit(1);
}

var filePath = path.resolve(__dirname, fileName);

console.log(filePath);

if (!fs.existsSync(filePath)) {
  console.warn(filePath + ' does not exist');
  process.exit(1);
}

mongoose.connect('mongodb://localhost/cable');

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

var stream = fs.createReadStream(__dirname + '/' + fileName);

db.once('open', function () {
  console.log('db connected');
});

var types = [];

csv()
  .from.stream(stream)
  .on('record', function (row, index) {
    if (index === 0) {
      console.log('headers');
    } else {
      types.push({
        name: row[0],
        characteristics: row[1],
        diameter: row[2],
        service: row[3],
        voltage: row[4],
        insulation: row[5],
        jacket: row[6],
        raceway: row[7],
        tid: row[8],
        model: row[9],
        comments: row[10],
        spec: row[11],
        request: row[12],
        revision: row[13],
        date: row[14]
      });
    }
  })
  .on('end', function (count) {
    console.log('lines: ' + count);
    CableType.create(types, function (err, docs) {
      if (err) {
        console.log(err.message);
      } else {
        console.log('write to the db finished. ');
        CableType.count(function (err, count) {
          if (err) {
            console.log(err);
          } else {
            console.log('There are ' + count + ' cable types now.');
            mongoose.disconnect(function (err) {
              if (err) {
                console.log(err.message);
              } else {
                console.log('mongoose disconnected');
              }
            });
          }
        });
      }
    });
  })
  .on('error', function (error) {
    console.log(error.message);
  });

/**
 * @fileOverview read a csv file with cable type details and insert the cable types in mongo.
 * @author Dong Liu
 */

var csv = require('csv'),
  fs = require('fs'),
  path = require('path'),
  mongoose = require('mongoose'),
  CableType = require('../model/meta.js').CableType;

var inputPath, realPath, db, stream, types = [],
  parser;

if (process.argv.length === 3) {
  inputPath = process.argv[2];
} else {
  console.warn('needs exact one argument for the input csv file path');
  process.exit(1);
}

realPath = path.resolve(__dirname, inputPath);

if (!fs.existsSync(realPath)) {
  console.warn(realPath + ' does not exist.');
  console.warn('Please input a valid csv file path.');
  process.exit(1);
}

mongoose.connect('mongodb://localhost/cable_frib');

db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function () {
  console.log('db connected');
});

// stream = fs.createReadStream(__dirname + '/' + fileName);

function createType(types, i) {
  CableType.create({
    name: types[i][4] + 'C_' + types[i][5] + '_' + types[i][6] + '_' + types[i][1],
    service: types[i][3],
    conductorNumber: types[i][4],
    conductorSize: types[i][5],
    fribType: types[i][6],
    typeNumber: types[i][1],
    pairing: types[i][7],
    shielding: types[i][8],
    outerDiameter: types[i][9],
    voltageRating: types[i][10],
    raceway: types[i][11],
    tunnelHotcell: (types[i][12] === 'yes'),
    otherRequirements: types[i][13],
    createdBy: 'system',
    createdOn: Date.now()
  }, function (err, doc) {
    if (err) {
      return console.log(err);
    }
    console.log('New type created with id: ' + doc.id);
  });
}


parser = csv.parse();

parser.on('readable', function () {
  var record;
  do {
    record = parser.read();
    if (record.length > 2 && record[1].length === 3) {
      types.push(record);
    }
  } while (!!record);
});

parser.on('error', function (err) {
  console.log(err.message);
});

parser.on('finish', function () {
  var i;
  for (i = 0; i < types.length; i += 1) {
    createType(types, i);
  }
});

use csv 0.4.0 api