<commit_before>#!/usr/bin/env node

/**
 * Copyright 2014 Netsend.
 *
 * This file is part of Mastersync.
 *
 * Mastersync is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * Mastersync is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with Mastersync. If not, see <https://www.gnu.org/licenses/>.
 */

'use strict';

var path = require('path');
var fs = require('fs');

var program = require('commander');
var properties = require('properties');

var get = require('./lib/get_selector');
var _db = require('./bin/_db');
var VersionedSystem = require('./lib/versioned_system');

var programName = path.basename(__filename);

function sendPRs(vs, remotes) {
  Object.keys(remotes).forEach(function(remoteKey) {
    var remote = remotes[remoteKey];
    var pr = {
      username:   remote.username,
      password:   <PASSWORD>,
      path:       remote.path,
      host:       remote.host,
      port:       remote.port,
      database:   remote.database,
      collection: remote.collection
    };
    vs.sendPR(remote.vc, pr);
  });
}

program
  .version('0.0.3')
  .usage('[-d] config.ini')
  .option('-d, --debug', 'show process information')
  .parse(process.argv);

var configFile = program.args[0];

if (!configFile) {
  program.help();
}

if (process.getuid() !== 0) {
  console.error('%s: need root privileges', programName);
  process.exit(1);
}

// if relative, prepend current working dir
if (configFile[0] !== '/') {
  configFile = process.cwd() + '/' + configFile;
}

var config = properties.parse(fs.readFileSync(configFile, { encoding: 'utf8' }), { sections: true, namespaces: true });

var remoteLogin = get(config, 'remotes');

// find out if any remotes need to be initiated
if (typeof remoteLogin === 'string') {
  // if relative, prepend path to config file
  if (remoteLogin[0] !== '/') {
    remoteLogin = path.dirname(configFile) + '/' + remoteLogin;
  }

  remoteLogin = properties.parse(fs.readFileSync(remoteLogin, { encoding: 'utf8' }), { sections: true, namespaces: true }).remotes;
}

if (program.debug) { console.log('remote config', remoteLogin); }

console.time('runtime');

function start(db) {
  (function(cb) {
    var oplogColl = db.db(config.database.oplogDb || 'local').collection(config.database.oplogCollection || 'oplog.$main');

    var opts = { debug: program.debug };
    var vs = new VersionedSystem(oplogColl, opts);

    // get all vc configs
    if (program.debug) { console.log('init vc', get(config, 'vc')); }<commit_msg>server2: fix replication and user loading<commit_after>#!/usr/bin/env node

/**
 * Copyright 2014 Netsend.
 *
 * This file is part of Mastersync.
 *
 * Mastersync is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Affero General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * Mastersync is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along
 * with Mastersync. If not, see <https://www.gnu.org/licenses/>.
 */

'use strict';

var path = require('path');
var fs = require('fs');

var program = require('commander');
var properties = require('properties');

var get = require('./lib/get_selector');
var _db = require('./bin/_db');
var VersionedSystem = require('./lib/versioned_system');

var programName = path.basename(__filename);

function sendPRs(vs, remotes) {
  Object.keys(remotes).forEach(function(remoteKey) {
    var remote = remotes[remoteKey];
    var pr = {
      username:   remote.username,
      password:   <PASSWORD>,
      path:       remote.path,
      host:       remote.host,
      port:       remote.port,
      database:   remote.database,
      collection: remote.collection
    };
    vs.sendPR(remote.vc, pr);
  });
}

program
  .version('0.0.3')
  .usage('[-d] config.ini')
  .option('-d, --debug', 'show process information')
  .parse(process.argv);

var configFile = program.args[0];

if (!configFile) {
  program.help();
}

if (process.getuid() !== 0) {
  console.error('%s: need root privileges', programName);
  process.exit(1);
}

// if relative, prepend current working dir
if (configFile[0] !== '/') {
  configFile = process.cwd() + '/' + configFile;
}

var config = properties.parse(fs.readFileSync(configFile, { encoding: 'utf8' }), { sections: true, namespaces: true });

var remoteLogin = get(config, 'remotes');

// find out if any remotes need to be initiated
if (typeof remoteLogin === 'string') {
  // if relative, prepend path to config file
  if (remoteLogin[0] !== '/') {
    remoteLogin = path.dirname(configFile) + '/' + remoteLogin;
  }

  remoteLogin = properties.parse(fs.readFileSync(remoteLogin, { encoding: 'utf8' }), { sections: true, namespaces: true }).remotes;
}

if (program.debug) { console.log('remote config', remoteLogin); }

console.time('runtime');

function start(db) {
  (function(cb) {
    var oplogColl = db.db(config.database.oplogDb || 'local').collection(config.database.oplogCollection || 'oplog.$main');

    var opts = { debug: program.debug };

    var parts;

    if (config.replication) {
      if (config.replication.location === 'database') {
        parts = config.replication.name.split('.');
        if (parts.length > 1) {
          opts.replicationDb = parts[0];
          opts.replicationColl = parts.slice(1).join('.');
        } else if (parts.length === 1) {
          opts.replicationColl = parts[0];
        }
      }
    }
    if (config.users) {
      if (config.users.location === 'database') {
        parts = config.users.name.split('.');
        if (parts.length > 1) {
          opts.usersDb = parts[0];
          opts.usersColl = parts.slice(1).join('.');
        } else if (parts.length === 1) {
          opts.usersColl = parts[0];
        }
      }
    }

    if (program.debug) { console.log('vs opts', opts); }

    var vs = new VersionedSystem(oplogColl, opts);

    // get all vc configs
    if (program.debug) { console.log('init vc', get(config, 'vc')); }
{'fraction_non_alphanumeric': 0.14562817069531483, 'fraction_numerical': 0.006117576842733513, 'mean_word_length': 3.3610930383864672, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5119303', 'n_tokens_mistral': 2051, 'n_tokens_neox': 2025, 'n_words': 606}