<commit_before>'use strict';

var env = { };
var crypto = require('crypto');
var consts = require('./lib/constants');
var fs = require('fs');
// Module to constrain all config and environment parsing to one spot.
function config ( ) {

  /*
   * First inspect a bunch of environment variables:
   *   * PORT - serve http on this port
   *   * MONGO_CONNECTION, CUSTOMCONNSTR_mongo - mongodb://... uri
   *   * CUSTOMCONNSTR_mongo_collection - name of mongo collection with "sgv" documents
   *   * CUSTOMCONNSTR_mongo_settings_collection - name of mongo collection to store configurable settings
   *   * API_SECRET - if defined, this passphrase is fed to a sha1 hash digest, the hex output is used to create a single-use token for API authorization
   *   * NIGHTSCOUT_STATIC_FILES - the "base directory" to use for serving
   *     static files over http.  Default value is the included `static`
   *     directory.
   */
  var software = require('./package.json');
  var git = require('git-rev');

  if (readENV('APPSETTING_ScmType') == readENV('ScmType') && readENV('ScmType') == 'GitHub') {
    env.head = require('./scm-commit-id.json');
    console.log("SCM COMMIT ID", env.head);
  } else {
    git.short(function record_git_head (head) {
      console.log("GIT HEAD", head);
      env.head = head || readENV('SCM_COMMIT_ID') || readENV('COMMIT_HASH', '');
    });
  }
  env.version = software.version;
  env.name = software.name;
  env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
  env.PORT = readENV('PORT', 1337);
  env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI');
  env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
  env.MQTT_MONITOR = readENV('MQTT_MONITOR', null);
  if (env.MQTT_MONITOR) {
    var hostDbCollection = [env.mongo.split('mongodb://').pop().split('@').pop( ), env.mongo_collection].join('/');
    var mongoHash = crypto.createHash('sha1');
    mongoHash.update(hostDbCollection);
    env.mqtt_client_id = mongoHash.digest('hex');
    console.info('Using Mongo host/db/collection to create the default MQTT client_id', hostDbCollection);
    if (env.MQTT_MONITOR.indexOf('?clientId=') == -1) {
      console.info('Set MQTT client_id to: ', env.mqtt_client_id);
    } else {
      console.info('MQTT configured to use a custom client id, it will override the default: ', env.mqtt_client_id);
    }
  }
  env.settings_collection = readENV('MONGO_SETTINGS_COLLECTION', 'settings');
  env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
  env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
  env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');

  env.enable = readENV('ENABLE', "");

  env.defaults = { // currently supported keys must defined be here
    'units': 'mg/dL'
    , 'timeFormat': '12'
    , 'nightMode': false
    , 'showRawbg': 'never'
    , 'customTitle': 'Nightscout'
    , 'theme': 'default'
    , 'alarmUrgentHigh': true
    , 'alarmHigh': true
    , 'alarmLow': true
    , 'alarmUrgentLow': true
    , 'language': 'en' // not used yet
  } ;

  // add units from separate variable<commit_msg>don't let the client id get longer than 23 chars<commit_after>'use strict';

var env = { };
var crypto = require('crypto');
var consts = require('./lib/constants');
var fs = require('fs');
// Module to constrain all config and environment parsing to one spot.
function config ( ) {

  /*
   * First inspect a bunch of environment variables:
   *   * PORT - serve http on this port
   *   * MONGO_CONNECTION, CUSTOMCONNSTR_mongo - mongodb://... uri
   *   * CUSTOMCONNSTR_mongo_collection - name of mongo collection with "sgv" documents
   *   * CUSTOMCONNSTR_mongo_settings_collection - name of mongo collection to store configurable settings
   *   * API_SECRET - if defined, this passphrase is fed to a sha1 hash digest, the hex output is used to create a single-use token for API authorization
   *   * NIGHTSCOUT_STATIC_FILES - the "base directory" to use for serving
   *     static files over http.  Default value is the included `static`
   *     directory.
   */
  var software = require('./package.json');
  var git = require('git-rev');

  if (readENV('APPSETTING_ScmType') == readENV('ScmType') && readENV('ScmType') == 'GitHub') {
    env.head = require('./scm-commit-id.json');
    console.log("SCM COMMIT ID", env.head);
  } else {
    git.short(function record_git_head (head) {
      console.log("GIT HEAD", head);
      env.head = head || readENV('SCM_COMMIT_ID') || readENV('COMMIT_HASH', '');
    });
  }
  env.version = software.version;
  env.name = software.name;
  env.DISPLAY_UNITS = readENV('DISPLAY_UNITS', 'mg/dl');
  env.PORT = readENV('PORT', 1337);
  env.mongo = readENV('MONGO_CONNECTION') || readENV('MONGO') || readENV('MONGOLAB_URI');
  env.mongo_collection = readENV('MONGO_COLLECTION', 'entries');
  env.MQTT_MONITOR = readENV('MQTT_MONITOR', null);
  if (env.MQTT_MONITOR) {
    var hostDbCollection = [env.mongo.split('mongodb://').pop().split('@').pop( ), env.mongo_collection].join('/');
    var mongoHash = crypto.createHash('sha1');
    mongoHash.update(hostDbCollection);
    //some MQTT servers only allow the client id to be 23 chars
    env.mqtt_client_id = mongoHash.digest('base64').substring(0, 23);
    console.info('Using Mongo host/db/collection to create the default MQTT client_id', hostDbCollection);
    if (env.MQTT_MONITOR.indexOf('?clientId=') == -1) {
      console.info('Set MQTT client_id to: ', env.mqtt_client_id);
    } else {
      console.info('MQTT configured to use a custom client id, it will override the default: ', env.mqtt_client_id);
    }
  }
  env.settings_collection = readENV('MONGO_SETTINGS_COLLECTION', 'settings');
  env.treatments_collection = readENV('MONGO_TREATMENTS_COLLECTION', 'treatments');
  env.profile_collection = readENV('MONGO_PROFILE_COLLECTION', 'profile');
  env.devicestatus_collection = readENV('MONGO_DEVICESTATUS_COLLECTION', 'devicestatus');

  env.enable = readENV('ENABLE', "");

  env.defaults = { // currently supported keys must defined be here
    'units': 'mg/dL'
    , 'timeFormat': '12'
    , 'nightMode': false
    , 'showRawbg': 'never'
    , 'customTitle': 'Nightscout'
    , 'theme': 'default'
    , 'alarmUrgentHigh': true
    , 'alarmHigh': true
    , 'alarmLow': true
    , 'alarmUrgentLow': true
    , 'language': 'en' // not used yet
  } ;

  // add units from separate variable
{'fraction_non_alphanumeric': 0.14272488813454715, 'fraction_numerical': 0.00416602376176516, 'mean_word_length': 3.903177004538578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4635886', 'n_tokens_mistral': 2207, 'n_tokens_neox': 2124, 'n_words': 519}