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

# Helper functions
source "$ENVIRONMENT_ROOT/bin/inc/helpers.sh"
source "$ENVIRONMENT_ROOT/bin/inc/redis-select-db.sh"

# Environment
ENVIRONMENT_ETC="$ENVIRONMENT_ROOT/etc/$ENVIRONMENT"

# Magento
MAGENTO_ROOT="$ENVIRONMENT_ROOT/www"
MAGENTO_ETC="$MAGENTO_ROOT/app/etc"

# Shared with other environments
SHARED_MEDIA="$PROJECT_ROOT/common/media"

# FLAGS
MAGENTO_INSTALLED=$(test_file -e $MAGENTO_ETC/local.xml)
BACKEND_CACHE_CONFIGURED=$(test_file -e $ENVIRONMENT_ETC/Mage_Cache_Backend_Redis.xml)
BACKEND_CACHE_LINKED=$(test_file -e $MAGENTO_ETC/Mage_Cache_Backend_Redis.xml)
SESSIONS_CONFIGURED=$(test_file -e $ENVIRONMENT_ETC/Cm_RedisSession.xml)
SESSIONS_LINKED=$(test_file -e $MAGENTO_ETC/Cm_RedisSession.xml)

# if [ ! MAGENTO_INSTALLED ]; then

  unset DB_NAME
  unset DB_USER
  unset DB_PASS

  # Get list of existing MySQL Databases
  list_mysql_databases=$(mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
  # Put list in array
  read -a mysql_databases <<<$list_mysql_databases

  # Configuration for the installer
  SAMPLE_DATA="no"
  DB_HOST="localhost"
  MAGENTO_VERSION="magento-mirror-1.9.2.1"

  # Request / generate database configuration
  while [[ "${DB_NAME}" != magento_* ]]; do
    # Request database name
    printf "Please enter the name of the database you would like to use for this installation.\n"
    printf "This needs to be prefixed with 'magento_'.\n"
    style_message hint "A good name would be something like 'magento_$ENVIRONMENT'"
    read DB_NAME
    # Check database name doesn't already exist
    if in_array mysql_databases "${DB_NAME}"; then
      style_message error "The database '${DB_NAME}' already exists and so can't be used :("
      unset DB_NAME
    # Check database name is valid (starts with 'magento_')
    elif [[ "${DB_NAME}" != magento_* ]]; then
      style_message error "The Database name must start with 'magento_')"
      unset DB_NAME
    # Check database name is Alpha Numeric and underscore only
    elif [[ "${DB_NAME}" == "^[a-zA-Z0-9_]*$" ]]; then
      style_message error "The Database name must be Alpha Numeric, but with the exception of underscores"
      unset DB_NAME
    # All is good, accepted DB_NAME
    else
      # Use DB_NAME as DB_USER
      DB_USER="${DB_NAME}"
      # Generated password will contain at least:
      #   1 Special
      #   1 Number
      #   1 Lower Alpha
      #   1 Upper Alpha
      # The password will be between 16 and 23 characters long
      # http://stackoverflow.com/questions/26665389/random-password-generator-bash#answer-26665585
      choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
      DB_PASS=$({
          choose '!@#$%^\&'
          choose '<PASSWORD>'
          choose 'abcdefghijklmnopqrstuvwxyz'
          choose 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          for i in $( seq 1 $(( 12 + RANDOM % 8 )) ); do
            choose '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
          done
      } | sort -R | awk '{printf "%s",$1}')
    fi
  done

  # Magento database details
  printf "${FORMAT[lightgreen]}Magento Database details...${FORMAT[nf]}\n"
  style_config "Database Name" $DB_NAME
  style_config "Database User" $DB_USER
  style_config "Database Password" $<PASSWORD>

  # Magento Base URL
  printf "Please enter the Magento Base URL inc protocol and trailing slash (ie - http://www.domain.com/):\n"
  read MAGENTO_BASE_URL
# fi
exit

# Redis Cache
printf "${FORMAT[lightgreen]}Redis Backend Cache${FORMAT[nf]}\n"
printf "Please select which Redis Database you would like to use for the Backend Cache.\n"
printf "To see a list of existing database in use start another SSH session and type 'redis-cli INFO keyspace.'\n"
printf "You should choose a NEW keyspace NOT in this, unless of course you wish to overite an existing one.\n"
printf "The keyspace should be in the form of an integer (ie - 0, 1, 2, 3, etc.)\n"
printf "Redis Backend Cache Database keyspace:\n"
redis_select_db
CACHE_DATABASE="${get_db}"
CACHE_PERSISTENT="cache-db$CACHE_DATABASE"

# Redis Sessions
printf "${FORMAT[lightgreen]}Redis Sessions${FORMAT[nf]}\n"
printf "Please select which Redis Database you would like to use for the Sessions.\n"
printf "To see a list of existing database in use start another SSH session and type 'redis-cli INFO keyspace.'\n"
printf "You should choose a NEW keyspace NOT in this, unless of course you wish to overite an existing one.\n"
printf "The keyspace should be in the form of an integer (ie - 0, 1, 2, 3, etc.)\n"
printf "Redis Sessions Database keyspace:\n"
redis_select_db
SESSION_DB="${get_db}"
SESSION_PERSISTENT="session-db$SESSION_DB"<commit_msg>Style line helper function test<commit_after>#!/usr/bin/env bash

# Helper functions
source "$ENVIRONMENT_ROOT/bin/inc/helpers.sh"
source "$ENVIRONMENT_ROOT/bin/inc/redis-select-db.sh"

# Environment
ENVIRONMENT_ETC="$ENVIRONMENT_ROOT/etc/$ENVIRONMENT"

# Magento
MAGENTO_ROOT="$ENVIRONMENT_ROOT/www"
MAGENTO_ETC="$MAGENTO_ROOT/app/etc"

# Shared with other environments
SHARED_MEDIA="$PROJECT_ROOT/common/media"

# FLAGS
MAGENTO_INSTALLED=$(test_file -e $MAGENTO_ETC/local.xml)
BACKEND_CACHE_CONFIGURED=$(test_file -e $ENVIRONMENT_ETC/Mage_Cache_Backend_Redis.xml)
BACKEND_CACHE_LINKED=$(test_file -e $MAGENTO_ETC/Mage_Cache_Backend_Redis.xml)
SESSIONS_CONFIGURED=$(test_file -e $ENVIRONMENT_ETC/Cm_RedisSession.xml)
SESSIONS_LINKED=$(test_file -e $MAGENTO_ETC/Cm_RedisSession.xml)

# if [ ! MAGENTO_INSTALLED ]; then

  unset DB_NAME
  unset DB_USER
  unset DB_PASS

  # Get list of existing MySQL Databases
  list_mysql_databases=$(mysql --user=root --password=$MYSQL_ROOT_PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
  # Put list in array
  read -a mysql_databases <<<$list_mysql_databases

  # Configuration for the installer
  SAMPLE_DATA="no"
  DB_HOST="localhost"
  MAGENTO_VERSION="magento-mirror-1.9.2.1"

  # Request / generate database configuration
  while [[ "${DB_NAME}" != magento_* ]]; do
    # Request database name
    printf "Please enter the name of the database you would like to use for this installation.\n"
    printf "This needs to be prefixed with 'magento_'.\n"
    style_message hint "A good name would be something like 'magento_$ENVIRONMENT'"
    read DB_NAME
    # Check database name doesn't already exist
    if in_array mysql_databases "${DB_NAME}"; then
      style_message error "The database '${DB_NAME}' already exists and so can't be used :("
      unset DB_NAME
    # Check database name is valid (starts with 'magento_')
    elif [[ "${DB_NAME}" != magento_* ]]; then
      style_message error "The Database name must start with 'magento_')"
      unset DB_NAME
    # Check database name is Alpha Numeric and underscore only
    elif [[ "${DB_NAME}" == "^[a-zA-Z0-9_]*$" ]]; then
      style_message error "The Database name must be Alpha Numeric, but with the exception of underscores"
      unset DB_NAME
    # All is good, accepted DB_NAME
    else
      # Use DB_NAME as DB_USER
      DB_USER="${DB_NAME}"
      # Generated password will contain at least:
      #   1 Special
      #   1 Number
      #   1 Lower Alpha
      #   1 Upper Alpha
      # The password will be between 16 and 23 characters long
      # http://stackoverflow.com/questions/26665389/random-password-generator-bash#answer-26665585
      choose() { echo ${1:RANDOM%${#1}:1} $RANDOM; }
      DB_PASS=$({
          choose '!@#$%^\&'
          choose '0123456789'
          choose 'abcdefghijklmnopqrstuvwxyz'
          choose 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
          for i in $( seq 1 $(( 12 + RANDOM % 8 )) ); do
            choose '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
          done
      } | sort -R | awk '{printf "%s",$1}')
    fi
  done

  # Magento database details
  style_line lightgreen "Magento Database details..."
  style_config "Database Name" $DB_NAME
  style_config "Database User" $DB_USER
  style_config "Database Password" $<PASSWORD>

  # Magento Base URL
  printf "Please enter the Magento Base URL inc protocol and trailing slash (ie - http://www.domain.com/):\n"
  read MAGENTO_BASE_URL
# fi
exit

# Redis Cache
printf "${FORMAT[lightgreen]}Redis Backend Cache${FORMAT[nf]}\n"
printf "Please select which Redis Database you would like to use for the Backend Cache.\n"
printf "To see a list of existing database in use start another SSH session and type 'redis-cli INFO keyspace.'\n"
printf "You should choose a NEW keyspace NOT in this, unless of course you wish to overite an existing one.\n"
printf "The keyspace should be in the form of an integer (ie - 0, 1, 2, 3, etc.)\n"
printf "Redis Backend Cache Database keyspace:\n"
redis_select_db
CACHE_DATABASE="${get_db}"
CACHE_PERSISTENT="cache-db$CACHE_DATABASE"

# Redis Sessions
printf "${FORMAT[lightgreen]}Redis Sessions${FORMAT[nf]}\n"
printf "Please select which Redis Database you would like to use for the Sessions.\n"
printf "To see a list of existing database in use start another SSH session and type 'redis-cli INFO keyspace.'\n"
printf "You should choose a NEW keyspace NOT in this, unless of course you wish to overite an existing one.\n"
printf "The keyspace should be in the form of an integer (ie - 0, 1, 2, 3, etc.)\n"
printf "Redis Sessions Database keyspace:\n"
redis_select_db
SESSION_DB="${get_db}"
SESSION_PERSISTENT="session-db$SESSION_DB"
{'fraction_non_alphanumeric': 0.10267049686136823, 'fraction_numerical': 0.012980104266411321, 'mean_word_length': 3.9604221635883907, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 10}, 'pii_count': 2, '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': '729688', 'n_tokens_mistral': 3206, 'n_tokens_neox': 3081, 'n_words': 960}