<commit_before>#!/bin/bash

# setup-config.sh
# Sets up deployment config files for further customization.

# This script's real path.
SCRIPT_FILE=$(readlink -f "$0")

# This script's source directory.
SOURCE_DIR=$(readlink -f `dirname "$SCRIPT_FILE"`/..)

# Debug mode
DEBUG=0

# Test only mode
TEST_ONLY=0

#
# Prints usage information.
#
usage() {
  cat <<-End

  Usage: ./setup-config.sh [options]

  Options:
    -d, --debug : Show additional debug info.
    -h, --help  : Show this usage info.
    -t, --test  : Run in test-only mode.

End
}

#
# Runs the given command, or just prints if test mode active.
#
run() {
  if [[ $TEST_ONLY -eq 1 ]]; then
    echo "would run: $@"
  else
    "$@"
  fi
}

#
# Gets command line options.
#
getOpts() {
  while [[ "$1" == -* ]]; do
    case "$1" in
      -d | --debug  ) DEBUG=1; shift ;;
      -h | --help   ) usage; exit 0 ;;
      -t | --test   ) TEST_ONLY=1; shift ;;
      *             ) shift ;;
    esac
  done

  if [[ $DEBUG -eq 1 ]]; then
    echo "TEST_ONLY: $TEST_ONLY"
  fi
}

#
# Generates an appSecret as a 60 character random hex string.
#
generateAppSecret() {
  local secret=`node -p -e "require('crypto').randomBytes(30).toString('hex');"`
  echo "$secret"
}

#
# Main program.
#
main() {
  getOpts "$@"

  run echo "Initializing config/deploy"
  mkdir -p "${SOURCE_DIR}/config/deploy"

  # Copy the config for each mode and generate an appSecret
  for mode in development testing staging production; do
    local config="${SOURCE_DIR}/config/modes/${mode}.coffee"
    local deploy="${SOURCE_DIR}/config/deploy/${mode}.coffee"

    if [[ ! -f "$deploy" ]]; then
      run echo "Creating '$deploy'"
      run cp "$config" "$deploy"

      local secret=$(generateAppSecret);
      run sed -i -r "s/^( +)([^ #])/\1# \2/" "$deploy"
      run sed -i -r "s/# (appSecret.*)SECRET(.*)/\1${secret}\2/" "$deploy"
    else
      run echo "Found '$deploy'"
    fi
  done

  run echo "Done!"
}

# Let's do this!
main "$@"<commit_msg>Update setup-config script to output json.<commit_after>#!/bin/bash

# setup-config.sh
# Sets up deployment config files for further customization.

# This script's real path.
SCRIPT_FILE=$(readlink -f "$0")

# This script's source directory.
SOURCE_DIR=$(readlink -f `dirname "$SCRIPT_FILE"`/..)

# Debug mode
DEBUG=0

# Test only mode
TEST_ONLY=0

#
# Prints usage information.
#
usage() {
  cat <<-End

  Usage: ./setup-config.sh [options]

  Options:
    -d, --debug : Show additional debug info.
    -h, --help  : Show this usage info.
    -t, --test  : Run in test-only mode.

End
}

#
# Runs the given command, or just prints if test mode active.
#
run() {
  if [[ $TEST_ONLY -eq 1 ]]; then
    echo "would run: $@"
  else
    "$@"
  fi
}

#
# Gets command line options.
#
getOpts() {
  while [[ "$1" == -* ]]; do
    case "$1" in
      -d | --debug  ) DEBUG=1; shift ;;
      -h | --help   ) usage; exit 0 ;;
      -t | --test   ) TEST_ONLY=1; shift ;;
      *             ) shift ;;
    esac
  done

  if [[ $DEBUG -eq 1 ]]; then
    echo "Debug mode enabled."
    set -x
  fi
}

#
# Generates an appSecret as a 60 character random hex string.
#
generateAppSecret() {
  local secret=`node -p -e "require('crypto').randomBytes(30).toString('hex');"`
  echo "$secret"
}

#
# Renders config file from template and given params.
#
renderConfig() {
  local secret="$1"
  local user="$2"
  local db="$3"
  cat <<-END
	{
	  "appSecret":   "${secret}",
	  "postgresUrl": "postgres://${user}:PASSWORD@localhost:5432/${db}"
	}
END
}

#
# Main program.
#
main() {
  getOpts "$@"

  run echo "Initializing config/deploy"
  mkdir -p "${SOURCE_DIR}/config/deploy"

  # Copy the config for each mode and generate an appSecret
  for mode in development testing staging production; do
    local config="${SOURCE_DIR}/config/modes/${mode}.json"
    local deploy="${SOURCE_DIR}/config/deploy/${mode}.json"

    if [[ ! -f "$deploy" ]]; then
      run echo "Creating '$deploy'"
      run cp "$config" "$deploy"

      if [[ $mode == 'production' ]]
      then local db="argumenta"
      else local db="argumenta_${mode}"
      fi

      local user="$db"
      local secret=$(generateAppSecret)
      local config=$(renderConfig $secret $user $db)
      echo "$config" > "$deploy"
    else
      run echo "Found '$deploy'"
    fi
  done

  run echo "Done!"
}

# Let's do this!
main "$@"
{'fraction_non_alphanumeric': 0.16552040348464006, 'fraction_numerical': 0.008940852819807428, 'mean_word_length': 2.6880811496196113, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 4, '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': '6569300', 'n_tokens_mistral': 1570, 'n_tokens_neox': 1482, 'n_words': 461}