<commit_before>var async = require('async')
var clc = require('cli-color')
var prmpt = require('prompt')
var request = require('request')
var semver = require('semver')

var configStore = require('../core/config_store')
var util = require('./index')

/**
 * Checks if CouchDB is in admin party mode
 */

exports.isAdminParty = function (env_config, callback) {
  request.get(
    env_config.couch.url + '/_users/_all_docs',
    function (err, res) {
    if (err) {
      return callback(err)
    }

    callback(null, res.statusCode === 200)
  })
}

/**
 * Creates a Pocket admin user
 */

exports.saveAdminUser = function (env_config, couch_user, couch_pwd, user, callback) {
  request({
    url: env_config.couch.url + '/_config/admins/' + encodeURIComponent(user.name),
    method: 'PUT',
    body: user.password,
    json: true,
    auth: {
      user: couch_user,
      pass: <PASSWORD>
    }
  }, callback)
}

/**
 * Prompts the user to create a Hoodie admin account
 */

exports.promptAdminUser = function (callback) {
  if (process.env.CI) {
    // hardcode username as admin for now
    var result = {}
    result.name = 'admin'
    result.password = '<PASSWORD>'
    return callback(null, result)
  }

  prmpt.get({
    properties: {
      password: {
        description: 'Please set an admin password ',
        required: true,
        hidden: true
      }
    }
  }, function (err, result) {
    // hardcode username as admin for now
    result.name = 'admin'
    return callback(err, result)
  })
}

/**
 * Checks if CouchDB is in admin party mode
 */

exports.checkCouchCredentials = function (env_config, callback) {
  configStore.getCouchCredentials(env_config, function (err, username, password) {
    if (err) {
      return callback(err)
    }

    if (!username || !password) {
      // missing from config, return a failure
      return callback(null, false)
    }

    request({
      url: env_config.couch.url + '/_users/_all_docs',
      method: 'GET',
      auth: {
        user: username,
        pass: password
      }
    }, function (err, res) {
      if (err) {
        return callback(err)
      }
      callback(null, res.statusCode === 200)
    })
  })
}

/**
 * Check that the stored couchdb credentials still work, prmpt the user
 * to update them if not.
 */

exports.updateCouchCredentials = function (env_config, callback) {
  exports.checkCouchCredentials(env_config, function (err, admin) {
    if (err) {
      return callback(err)
    }

    if (admin) {
      // stored admin user still works
      return callback()
    }

    // stored admin credentials out of date
    exports.promptCouchCredentials(function (err, user, pass) {
      if (err) {
        return callback(err)
      }

      configStore.setCouchCredentials(env_config, user, pass, function (err) {
        if (err) {
          return callback(err)
        }

        // make sure the new credentials work
        exports.updateCouchCredentials(env_config, callback)
      })
    })
  })
}

/**
 * Checks CouchDB to see if it is at least version 1.2.0
 */

exports.checkCouchVersion = function (env_config, callback) {
  if (env_config.couch.vendor !== 'couchdb') {
    console.error(
      clc.yellow('Warning:') +
      ' You are not running the officical CouchDB distribution, ' +
      'but "' + env_config.couch.vendor + '". ' +
      'This might not be fully supported. Proceed at your own risk.'
    )

    return callback()
  }
  // 1.2.0 is our minimum supported version<commit_msg>style(typo): fixed small typo officical<commit_after>var async = require('async')
var clc = require('cli-color')
var prmpt = require('prompt')
var request = require('request')
var semver = require('semver')

var configStore = require('../core/config_store')
var util = require('./index')

/**
 * Checks if CouchDB is in admin party mode
 */

exports.isAdminParty = function (env_config, callback) {
  request.get(
    env_config.couch.url + '/_users/_all_docs',
    function (err, res) {
    if (err) {
      return callback(err)
    }

    callback(null, res.statusCode === 200)
  })
}

/**
 * Creates a Pocket admin user
 */

exports.saveAdminUser = function (env_config, couch_user, couch_pwd, user, callback) {
  request({
    url: env_config.couch.url + '/_config/admins/' + encodeURIComponent(user.name),
    method: 'PUT',
    body: user.password,
    json: true,
    auth: {
      user: couch_user,
      pass: <PASSWORD>
    }
  }, callback)
}

/**
 * Prompts the user to create a Hoodie admin account
 */

exports.promptAdminUser = function (callback) {
  if (process.env.CI) {
    // hardcode username as admin for now
    var result = {}
    result.name = 'admin'
    result.password = '<PASSWORD>'
    return callback(null, result)
  }

  prmpt.get({
    properties: {
      password: {
        description: 'Please set an admin password ',
        required: true,
        hidden: true
      }
    }
  }, function (err, result) {
    // hardcode username as admin for now
    result.name = 'admin'
    return callback(err, result)
  })
}

/**
 * Checks if CouchDB is in admin party mode
 */

exports.checkCouchCredentials = function (env_config, callback) {
  configStore.getCouchCredentials(env_config, function (err, username, password) {
    if (err) {
      return callback(err)
    }

    if (!username || !password) {
      // missing from config, return a failure
      return callback(null, false)
    }

    request({
      url: env_config.couch.url + '/_users/_all_docs',
      method: 'GET',
      auth: {
        user: username,
        pass: password
      }
    }, function (err, res) {
      if (err) {
        return callback(err)
      }
      callback(null, res.statusCode === 200)
    })
  })
}

/**
 * Check that the stored couchdb credentials still work, prmpt the user
 * to update them if not.
 */

exports.updateCouchCredentials = function (env_config, callback) {
  exports.checkCouchCredentials(env_config, function (err, admin) {
    if (err) {
      return callback(err)
    }

    if (admin) {
      // stored admin user still works
      return callback()
    }

    // stored admin credentials out of date
    exports.promptCouchCredentials(function (err, user, pass) {
      if (err) {
        return callback(err)
      }

      configStore.setCouchCredentials(env_config, user, pass, function (err) {
        if (err) {
          return callback(err)
        }

        // make sure the new credentials work
        exports.updateCouchCredentials(env_config, callback)
      })
    })
  })
}

/**
 * Checks CouchDB to see if it is at least version 1.2.0
 */

exports.checkCouchVersion = function (env_config, callback) {
  if (env_config.couch.vendor !== 'couchdb') {
    console.error(
      clc.yellow('Warning:') +
      ' You are not running the official CouchDB distribution, ' +
      'but "' + env_config.couch.vendor + '". ' +
      'This might not be fully supported. Proceed at your own risk.'
    )

    return callback()
  }
  // 1.2.0 is our minimum supported version
{'fraction_non_alphanumeric': 0.12334613743064447, 'fraction_numerical': 0.0034144259496372174, 'mean_word_length': 2.592233009708738, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4509191', 'n_tokens_mistral': 2206, 'n_tokens_neox': 2171, 'n_words': 607}