#!/usr/bin/env bash

## Description: Run cspell inside the web container (mirrors the GitLab CI cspell job)
## Usage: cspell [flags] [args]
## Example: "ddev cspell" or "ddev cspell --version"
## ExecRaw: true

# Project-maintained command (intentionally NOT marked #ddev-generated, so the
# ddev-drupal-contrib add-on does not overwrite or remove it). It runs Drupal
# core's cspell binary with the same Drupal config the CI `cspell` job uses,
# including this module's .cspell-project-words.txt. There is no upstream
# `ddev cspell`; see https://github.com/ddev/ddev-drupal-contrib/pull/55.
#
# If cspell is missing, prime core's node deps once:
#   ddev exec "cd web/core && yarn install"

set -eu -o pipefail

# Environment consumed by Drupal's prepare-cspell.php config generator.
export CI_PROJECT_NAME="$DDEV_SITENAME"
export _WEB_ROOT="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT"
export _CSPELL_DICTIONARY="${_CSPELL_DICTIONARY:-.cspell-project-words.txt}"
export _CSPELL_IGNORE_PATHS="${_CSPELL_IGNORE_PATHS:-"$DDEV_DOCROOT,.ddev,config/sync"}"

# Locate cspell. Support both classic node_modules and Yarn PnP (Yarn 4+ default,
# which creates no node_modules/.bin/ shims and needs `yarn` run from core/).
CORE_DIR="$DDEV_COMPOSER_ROOT/$DDEV_DOCROOT/core"
CSPELL_BIN="$CORE_DIR/node_modules/.bin/cspell"
USE_YARN_PNP=false

if "$CSPELL_BIN" --version >/dev/null 2>&1; then
  CSPELL="$CSPELL_BIN"
elif (cd "$CORE_DIR" && yarn cspell --version) >/dev/null 2>&1; then
  USE_YARN_PNP=true
  CSPELL="yarn cspell"
else
  echo "cspell is not available. Prime core's node deps first:"
  echo "  ddev exec \"cd $DDEV_DOCROOT/core && yarn cache clean && yarn install\""
  exit 1
fi

run_cspell() {
  if [ "$USE_YARN_PNP" = true ]; then
    (cd "$CORE_DIR" && $CSPELL "$@")
  else
    "$CSPELL" "$@"
  fi
}

# Fast path: `ddev cspell --version`.
if [ "${1:-}" = "--version" ]; then
  run_cspell --version
  exit $?
fi

cd "$DDEV_COMPOSER_ROOT"

# Track which config files we create so we can remove them afterwards and leave
# the project tree exactly as we found it.
TMP_DICTIONARY=$(test -e "$_CSPELL_DICTIONARY"; echo $?)
TMP_CONFIG=$(test -e .cspell.json; echo $?)
touch "$_CSPELL_DICTIONARY"
test -e .cspell.json || curl -OLs https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/assets/.cspell.json

# Generate the Drupal-expanded config as .cspell.json.ddev (does not touch any
# existing .cspell.json), then rename to a *.json name cspell will load.
curl -OLs https://git.drupalcode.org/project/gitlab_templates/-/raw/default-ref/scripts/prepare-cspell.php
php prepare-cspell.php -s .ddev
rm prepare-cspell.php
mv -f .cspell.json.ddev .cspell.ddev.json

# Let cspell expand the glob itself (quoted), and capture its status without
# tripping `set -e` so cleanup always runs.
STATUS=0
run_cspell -c "$DDEV_COMPOSER_ROOT/.cspell.ddev.json" --show-suggestions --show-context --no-progress "$DDEV_COMPOSER_ROOT/**" "$@" || STATUS=$?

# Clean up temporary files, leaving the project tree as we found it.
rm -f .cspell.ddev.json
[ "$TMP_DICTIONARY" = "1" ] && rm -f "$_CSPELL_DICTIONARY" || true
[ "$TMP_CONFIG" = "1" ] && rm -f .cspell.json || true

exit "$STATUS"
