#!/usr/bin/env node
var pkg = require('./package.json')
var MongoClient = require('mongodb').MongoClient
var params = require('commander')
var stringify = require('fast-safe-stringify')
var jsonParse = require('fast-json-parse')

params
  .version(pkg.version)
  .description(pkg.description)
  .option('-H, --host <address>', 'DataBase host (127.0.0.1)', '127.0.0.1')
  .option('-P, --port <number>', 'DataBase port (27017)', '27017')
  .option('-d, --db <name>', 'DataBase name (logs)', 'logs')
  .option('-c, --collection <name>', 'DataBase collection name (logs)', 'logs')
  .option('-u, --user <username>', 'DataBase username (root)', 'root')
  .option('-p, --pass <password>', 'DataBase username password ()', '')
  .parse(process.argv)

params.host = process.env.DB_HOST || params.host
params.port = process.env.DB_PORT || params.port
params.db = process.env.DB_NAME || params.db
params.collection = process.env.DB_COLLECTION || params.collection
params.user = process.env.DB_USER || params.user
params.pass = process.env.DB_PASS || params.pass

var mongoOptions = 'mongodb://' +
  (params.user && params.pass
      ? '[' + params.user + ':' + params.pass + ']@'
      : '') +
  params.host +
  ':' + params.port +
  '/' + params.db

MongoClient.connect(mongoOptions, onConnection)

function onConnection (e, db) {
  if (e) {
    return handleError(e)
  }

  var collection = db.collection(params.collection)

  process.on('SIGINT', function () {
    db.close(function () {
      process.exit()
    })
  })

  process.stdin.resume()
  process.stdin.setEncoding('utf8')
  process.stdin.on('data', function stdin (data) {
    var document = {}
    var json = jsonParse(data).value

    if (json) {
      document = json
    } else {
      document = {
        msg: data
      }
    }

    collection.insertOne(document, function insertOne (e) {
      if (e) {
        return handleError(e)
      }
    })

    process.stdout.write(stringify(document) + '\n')
  })
}

function handleError (e) {
  if (e instanceof Error) {
    console.error(e)
  } else {
    console.error(new Error(e))
  }
}

@@ -1410,16 +1410,54 @@
 ction)%0A%0A
+  process.stdout.pipe(process.stdin)%0A%0A
   proces
@@ -1944,62 +1944,8 @@
   %7D)
-%0A%0A    process.stdout.write(stringify(document) + '%5Cn')
 %0A  %7D

set process.stdout pipe process.stdin without overhead