<?php

// Get the multisite dirname.
if (empty($site_path)) {
  die('You must set the variable $site_path in settings.php. In Drupal 8 sites this should be set by Drupal. For D7 you must do this manually. Example: sites/default.');
}
else {
  $multisite_dir = basename($site_path);
}

if (getenv('DOCKER')) {
  $databases = array(
    'default' => array(
      'default' => array(
        'driver' => getenv('DB_DRIVER'),
        'database' => $multisite_dir,
        'username' => getenv('DB_USER'),
        'password' => getenv('DB_PASSWORD'),
        'host' => getenv('DB_HOST'),
        'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
        'prefix' => "",
        'port' => '3306',
      ),
    ),
  );

  $config['search_api.server.default_solr_server'] = [
    'backend_config' => [
      'connector_config' => [
        'host' => 'solr',
        'path' => '/solr',
        'core' => $multisite_dir,
        'port' => '8983',
      ],
    ],
  ];

  if (!empty(getenv('YSCI_DRUPAL_PUBLIC_FILES_DIR'))) {
    $settings['file_public_path'] = 'sites/' . $multisite_dir . '/' . getenv('YSCI_DRUPAL_PUBLIC_FILES_DIR');
  }
  else {
    $settings['file_public_path'] = 'sites/' . $multisite_dir . '/files';
  }
  $settings['file_private_path'] = '../../data/sites/' . $multisite_dir . '/files-private';

  // Note: this only works during install. For existing sites add the following
  // to .ysci/local/scripts/docker.sh:
  // drush -l ${MULTISITE_DIR} cset translation.path locale.settings "sites/${MULTISITE_DIR}/files/translations" -y
  $config['locale.settings']['translation.path'] = $settings['file_public_path'] . '/translations';

  if (empty(getenv('DRUPAL_INSTALLING'))) {
    if (file_exists(DRUPAL_ROOT . '/modules/contrib/memcache/memcache.info.yml')) {
      $settings['cache']['default'] = 'cache.backend.memcache';
      $settings['memcache']['servers'] = ['memcached:11211' => 'default'];
      $settings['memcache']['bins'] = ['default' => 'default'];
      $settings['memcache']['key_prefix'] = $multisite_dir;
    }
    elseif (class_exists('Redis')
      && filter_var(getenv('YSCI_REDIS_ENABLED'), FILTER_VALIDATE_BOOLEAN)) {
      $settings['cache']['default'] = 'cache.backend.redis';
      $settings['redis.connection']['interface'] = 'PhpRedis';
      $settings['redis.connection']['host'] = 'redis';
      $settings['redis.connection']['port'] = '6379';

      $settings['cache_prefix'] = $multisite_dir;

      $settings['container_yamls'][] = 'modules/contrib/redis/example.services.yml';
    }
  }

  $settings['trusted_host_patterns'][] = '^\S+\.localhost$';
  $settings['trusted_host_patterns'][] = $multisite_dir;

  $ysciEnv = getenv('YSCI_ENVIRONMENT');
  foreach (explode(',', getenv('YSCI_DOMAIN_NAMES_' . strtoupper($ysciEnv))) as $domain_name) {
    $settings['trusted_host_patterns'][] = '^' . str_replace('.', '\.', trim($domain_name)) . '$';
  }

  if (is_dir('/var/www/html/config/sync/' . $multisite_dir)) {
    $settings['config_sync_directory'] = '/var/www/html/config/sync/' . $multisite_dir;
  }

  $settings['container_yamls'][] = $app_root . '/' . $site_path . '/services.yml';

  if ($ysciEnv == "dev"
    || $ysciEnv == "test") {
    $config['system.logging']['error_level'] = 'verbose';
    $config['system.performance']['css']['preprocess'] = FALSE;
    $config['system.performance']['js']['preprocess'] = FALSE;

    $settings['container_yamls'][] = __DIR__ . '/development.services.yml';

    $settings['cache']['default'] = 'cache.backend.null';
    $settings['cache']['bins']['render'] = 'cache.backend.null';
    $settings['cache']['bins']['dynamic_page_cache'] = 'cache.backend.null';
    $settings['cache']['bins']['page'] = 'cache.backend.null';
  }

}

$databases['migrate']['default'] = $databases['default']['default'];
$databases['migrate']['default']['host'] = 'mariadb_d7';

$settings['media_migration_embed_token_transform_destination_filter_plugin'] = 'media_embed';
$settings['media_migration_embed_media_reference_method'] = 'uuid';

$settings['hash_salt'] = 'HearLi++leDarL1ngDon^tShedN0TearsNoOnionN0Cry';