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

process.title = 'mongodbBackup';
var VERSION = require('../package.json').version;
var cli = require('cli');
var backup = require('mongodb-backup');

cli
    .parse({
      verbose: [ false, 'Save internal reporting into a logfile', 'file' ],
      host: [ false, 'Specifies a resolvable hostname for the mongod', 'string' ],
      cursors: [ 'c', 'Num of cursors', 'int', 0 ],
      parser: [ 'p', 'Data parser (bson, json)', 'string', 'bson' ],
      out: [ 'o', ' Specifies the directory where saves the output', 'string',
        'dump/' ],
      tar: [ 't', 'Pack files into a .tar file', 'string' ],
      collections: [ 'c', 'Specifies a collection to backup', 'string' ],
      query: [ 'q', 'Query that optionally limits the documents included',
        'string' ],
      metadata: [ 'm', 'Save metadata of collections as Index, ecc' ],
      version: [ 'v', 'Display the current version' ]
    });

cli.setApp(process.title, VERSION).main(
  function(args, options) {

    var self = this;

    if (options.version) {
      return self.info(process.title + ' v' + VERSION);
    } else if (args.length === 0 && !options.host) {
      self.fatal('Missing uri option');
    }

    self.spinner('Working..');
    try {
      backup({
        uri: args[0] || options.host,
        root: options.out,
        parser: options.parser,
        numCursors: options.cursors,
        collections: options.collections ? JSON.parse(options.collections)
          : null,
        tar: options.tar,
        query: options.query ? JSON.parse(options.query) : null,
        logger: options.verbose,
        metadata: options.metadata,
        callback: function(err) {

          if (err) {
            self.spinner('Working.. error\n', true);
            self.error(err.message);
          } else {
            self.spinner('Working.. done\n', true);
          }
        }
      });
    } catch (e) {
      self.spinner('Working.. error\n', true);
      self.error(e.message);
    }
  });

@@ -182,12 +182,53 @@
 );%0A%0A
+cli.enable('timeout').enable('version');%0A
 cli%0A
-
     
@@ -628,17 +628,17 @@
 tar: %5B '
-t
+r
 ', 'Pack
@@ -915,63 +915,8 @@
 ecc'
- %5D,%0A      version: %5B 'v', 'Display the current version'
  %5D%0A 
@@ -1767,21 +1767,21 @@
    self.
-error
+fatal
 (err.mes
@@ -1966,21 +1966,21 @@
    self.
-error
+fatal
 (e.messa

add timeout options