#!/usr/bin/env node
'use strict';

const JSONStream = require('JSONStream');
const co = require('co');
const commander = require('commander');
const dookie = require('../');
const fs = require('fs');
const path = require('path');
const yaml = require('js-yaml');

commander.
  usage('dookie (pull|push) --file <file> --db <database name>').
  option('-f, --file <file>', 'File to read/write from').
  option('-d, --db <database>', 'Database to read/write from').
  option('-u, --uri <uri>', 'MongoDB URI to use (mongodb://localhost:27017 by default)').
  parse(process.argv);

const cmd = process.argv[2];
if (!cmd || ['pull', 'push'].indexOf(cmd) === -1) {
  console.log('Must specify either pull (pull data from db into file) ' +
    'or push (push data into db from file).');
  process.exit(1);
}

if (!commander.file) {
  console.log('Must specify --file');
  process.exit(1);
}

if (!commander.db && !commander.uri) {
  console.log('Must specify --db or --uri');
  process.exit(1);
}

if (cmd === 'pull') {
  const uri = commander.uri || `mongodb://localhost:27017/${commander.db}`;
  console.log(`Writing data from ${uri} to ${commander.file}`);
  co(function*() {
    const res = yield dookie.pull(uri);
    const stringify = JSONStream.stringifyObject();
    stringify.pipe(fs.createWriteStream(commander.file)).on('finish', () => { process.exit(0); }).
      on('error', error => { console.error(error.stack); process.exit(1); });
    for (const coll in res) {
      stringify.write([coll, res[coll]]);
    }
    stringify.end();
  }).
  catch(function(error) {
    console.log('Error reading data:', error.stack);
    process.exit(1);
  });
} else if (cmd === 'push') {
  console.log(`Writing data from ${commander.file} to ${commander.db}`);

  co(function*() {
    const uri = commander.uri || `mongodb://localhost:27017/${commander.db}`;
    let data;
    if (path.extname(commander.file) === '.json') {
      data = JSON.parse(fs.readFileSync(commander.file));
    } else {
      data = yaml.safeLoad(fs.readFileSync(commander.file));
    }
    yield dookie.push(uri, data,
      commander.file);

    console.log('Success!');
    process.exit(0);
  }).catch(function(error) {
    console.log('Error writing data', error.stack);
    process.exit(1);
  });
}

@@ -1313,16 +1313,23 @@
 .file)).
+%0A      
 on('fini
@@ -1340,16 +1340,54 @@
  () =%3E %7B
+%0A        console.log('Done!');%0A       
  process
@@ -1395,16 +1395,22 @@
 exit(0);
+%0A     
  %7D).%0A   
@@ -1434,16 +1434,24 @@
 ror =%3E %7B
+%0A       
  console
@@ -1487,16 +1487,22 @@
 exit(1);
+%0A     
  %7D);%0A   

print when pull stream is done