#!/usr/bin/env bash
# Direnv bootstrap for DDEV development
# 1) Install direnv and hook into your shell. 2) (Optional) whitelist this file in ~/.config/direnv/direnv.toml
# When you cd into this dir, DDEV binaries and development tools are added to PATH.

set -euo pipefail

# Pre-reqs
if ! command -v go >/dev/null 2>&1; then
  echo "Go is not available." >&2; exit 1
fi

mkdir -p .gotmp/bin

# Prepend ddev build bin to PATH
BIN_DIR="$PWD/.gotmp/bin/$(go env GOHOSTOS)_$(go env GOHOSTARCH)"
export PATH="$BIN_DIR:$PATH"

# Add global development tools to PATH if they exist
DEV_TOOLS_DIR="$HOME/.ddev-dev-tools"
if [[ -d "$DEV_TOOLS_DIR/python/bin" && -d "$DEV_TOOLS_DIR/node/bin" ]]; then
  export PATH="$DEV_TOOLS_DIR/python/bin:$DEV_TOOLS_DIR/node/bin:$PATH"
else
  # Check if any development tools are missing and show helpful message
  missing_tools=()
  if ! command -v mkdocs >/dev/null 2>&1; then missing_tools+=("mkdocs"); fi
  if ! command -v pyspelling >/dev/null 2>&1; then missing_tools+=("pyspelling"); fi
  if ! command -v markdownlint >/dev/null 2>&1; then missing_tools+=("markdownlint"); fi
  if ! command -v textlint >/dev/null 2>&1; then missing_tools+=("textlint"); fi
  if ! command -v linkspector >/dev/null 2>&1; then missing_tools+=("linkspector"); fi
  
  if [[ ${#missing_tools[@]} -gt 0 ]]; then
    echo "📝 Missing development tools: ${missing_tools[*]}"
    echo "   Install them with: scripts/install-dev-tools.sh"
    echo "   Or see: https://docs.ddev.com/en/stable/developers/testing-docs/"
  fi
fi
