#!/bin/sh
set -e

echo "[wpcli] Waiting for WordPress core install..."
until wp core is-installed --path=/var/www/html 2>/dev/null; do
  sleep 3
done

if ! wp language core is-installed pt_BR --path=/var/www/html 2>/dev/null; then
  echo "[wpcli] Installing pt_BR language pack..."
  wp language core install pt_BR --path=/var/www/html
fi

echo "[wpcli] Activating pt_BR site language..."
wp language core activate pt_BR --path=/var/www/html

echo "[wpcli] Setting Redis connection constants..."
wp config set WP_REDIS_HOST redis --type=constant --path=/var/www/html
wp config set WP_REDIS_PORT 6379 --type=constant --raw --path=/var/www/html

if ! wp plugin is-installed redis-cache --path=/var/www/html; then
  echo "[wpcli] Installing Redis Object Cache plugin..."
  wp plugin install redis-cache --path=/var/www/html
fi

echo "[wpcli] Activating Redis Object Cache plugin..."
wp plugin activate redis-cache --path=/var/www/html

echo "[wpcli] Enabling Redis object cache..."
wp redis enable --path=/var/www/html || wp redis update-dropin --path=/var/www/html

echo "[wpcli] Redis setup complete."

if ! wp plugin is-installed query-monitor --path=/var/www/html; then
  echo "[wpcli] Installing Query Monitor plugin..."
  wp plugin install query-monitor --path=/var/www/html
fi

echo "[wpcli] Activating Query Monitor plugin..."
wp plugin activate query-monitor --path=/var/www/html
