#!/usr/bin/env bash
set -euo pipefail

export TEXTDOMAIN="traduia"
export TEXTDOMAINDIR="/usr/share/locale"

_() {
    gettext -- "$1"
}

show_help() {
    echo "$(_ "Usage: install-models-traduia [options] [mode]")"
    echo ""
    echo "$(_ "  (no parameters)  this help")"
    echo "$(_ "  install           install the standard (Marian) model set from the")"
    echo "$(_ "                    configured source (default: internet)")"
    echo "$(_ "  optimized         install the optimized (CTranslate2) model set")"
    echo "$(_ "                    (experimental); combinable with \"install\"")"
    echo "$(_ "  all               install both model sets (Marian and optimized/CT2)")"
    echo "$(_ "                    in the same run; Marian takes priority; combinable")"
    echo "$(_ "                    with \"install\" and \"optimized\"")"
    echo "$(_ "  --url <url>       repository base URL (contains manifest.json)")"
    echo "$(_ "  --dir <path>      local repository directory (USB, mounted drive)")"
    echo "$(_ "  --from <url|path> auto-detect: URL or local path")"
    echo "$(_ "  --detect          print model set state (complete:* / partial / none)")"
    echo "$(_ "  help, --help, -h  this help")"
}

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PYTHON_VER=$(python3 -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}');")
cd "$SCRIPT_DIR"

SHARED_HF="${TRADUIA_MODELS_DIR:-/opt/ai/traduia/models}"
TRADUIA_DIR="/usr/lib/traduia"
TRADUIA_VENV="/opt/ai/traduia/venv"
CONFIG_FILE="/etc/traduia/models-repo.conf"

MARIAN_PAIRS=(
    "es-en" "es-fr" "es-de" "es-ru" "es-ar"
    "es-uk" "es-ro" "es-it" "ca-en" "ca-es"
)

BASE_PYTHON_PKGS=(
    fastapi
    "uvicorn[standard]"
    python-multipart
    numpy
    sounddevice
    faster-whisper
    transformers
    sentencepiece
    huggingface_hub
    psutil
)

CT2_PYTHON_PKGS=(
    ctranslate2
)

FINAL_PYTHON_PKGS=(
    torch
    sacremoses
)

# =========================================================
# Argumentos
# =========================================================
USE_CT2=false
USE_ALL=false
CLI_URL=""
DETECT_ONLY=false
# Args originales para la cabecera del log (el parser hace shift).
IMT_RUN_ARGS="$*"

# Sin argumentos: ayuda (uso explícito con "install" o "optimized")
if [ $# -eq 0 ]; then
    show_help
    exit 0
fi

while [ $# -gt 0 ]; do
    case "$1" in
        install)
            shift
            ;;
        optimized)
            USE_CT2=true
            shift
            ;;
        all)
            USE_ALL=true
            shift
            ;;
        help|-h|--help)
            show_help
            exit 0
            ;;
        --url)
            if [ $# -lt 2 ]; then
                echo "[ERROR] $(_ "--url requires a value: --url http://host/path")" >&2
                exit 1
            fi
            CLI_URL="$2"
            shift 2
            ;;
        --url=*)
            CLI_URL="${1#--url=}"
            shift
            ;;
        --dir)
            if [ $# -lt 2 ]; then
                echo "[ERROR] $(_ "--dir requires a value: --dir /path/to/models/repo")" >&2
                exit 1
            fi
            CLI_DIR="$2"
            shift 2
            ;;
        --dir=*)
            CLI_DIR="${1#--dir=}"
            shift
            ;;
        --from)
            if [ $# -lt 2 ]; then
                echo "[ERROR] $(_ "--from requires a value: --from <url|path>")" >&2
                exit 1
            fi
            CLI_FROM="$2"
            shift 2
            ;;
        --from=*)
            CLI_FROM="${1#--from=}"
            shift
            ;;
        --detect)
            DETECT_ONLY=true
            shift
            ;;
        *)
            echo "[ERROR] $(_ "Unknown argument:") $1" >&2
            exit 1
            ;;
    esac
done

# --url, --dir y --from son mutuamente excluyentes
SOURCE_OPTS=0
[ -n "${CLI_URL:-}" ] && SOURCE_OPTS=$((SOURCE_OPTS + 1))
[ -n "${CLI_DIR:-}" ] && SOURCE_OPTS=$((SOURCE_OPTS + 1))
[ -n "${CLI_FROM:-}" ] && SOURCE_OPTS=$((SOURCE_OPTS + 1))
if [ "$SOURCE_OPTS" -gt 1 ]; then
    echo "[ERROR] $(_ "Options --url, --dir and --from are mutually exclusive.")" >&2
    exit 1
fi

# --from: autodetección url vs ruta local
if [ -n "${CLI_FROM:-}" ]; then
    case "$CLI_FROM" in
        http://*|https://*)
            CLI_URL="$CLI_FROM"
            ;;
        file://*)
            CLI_DIR="${CLI_FROM#file://}"
            ;;
        *)
            CLI_DIR="$CLI_FROM"
            ;;
    esac
fi

if [ -n "${CLI_DIR:-}" ] && [ ! -d "$CLI_DIR" ]; then
    echo "[ERROR] $(_ "Source directory does not exist:") $CLI_DIR" >&2
    exit 1
fi

# =========================================================
# Configuración: /etc/traduia/models-repo.conf
# Formato: TRADUIA_MODELS_URL=http://servidor:puerto/public/models/traduia
# Prioridad de URL: CLI > variable de entorno > fichero de config
# =========================================================
if [ -z "${TRADUIA_MODELS_URL:-}" ] && [ -f "$CONFIG_FILE" ]; then
    # shellcheck disable=SC1090
    source "$CONFIG_FILE"
fi

MODELS_URL="${CLI_URL:-${TRADUIA_MODELS_URL:-}}"
MODELS_DIR="${CLI_DIR:-}"

# =========================================================
# Logging integral a /var/log/traduia-install.log (en común con
# traduia-config). Todo el stdout/stderr va a consola y al fichero;
# si el fichero no es escribible (p. ej. sin root) se continúa sin
# teeing. Cuando se ejecuta bajo traduia-config (TRADUIA_CONFIG_LOGGING=1)
# el padre ya tee-nea la salida y solo se escribe la cabecera directa.
# Literal en inglés: no pasa por i18n.
# =========================================================
TRADUIA_IMT_LOG="${TRADUIA_IMT_LOG:-/var/log/traduia-install.log}"
if touch "$TRADUIA_IMT_LOG" 2>/dev/null; then
    printf 'Starting install-models-traduia (%s) at %s\n' \
        "$IMT_RUN_ARGS" "$(date '+%d/%m/%Y %H:%M:%S')" >>"$TRADUIA_IMT_LOG"
    if [ -z "${TRADUIA_CONFIG_LOGGING:-}" ]; then
        exec > >(tee -a "$TRADUIA_IMT_LOG") 2>&1
    fi
fi

# =========================================================
# Validación temprana del repositorio (--url/--dir/--from)
# Comprueba que la fuente es una exportación de repositorio
# válida (manifest.json con files[] y al menos un set ct2|marian)
# ANTES de crear el venv e instalar dependencias (minutos).
# La validación profunda (completitud del set, sha256) la hace
# cada modo de instalación.
# =========================================================
validate_models_source() {
    local manifest=""
    local tmp=""

    if [ -n "$MODELS_DIR" ]; then
        manifest="$MODELS_DIR/manifest.json"
        if [ ! -f "$manifest" ]; then
            echo "[ERROR] No manifest.json found in source directory: $MODELS_DIR" >&2
            return 1
        fi
    elif [ -n "$MODELS_URL" ]; then
        tmp="$(mktemp)"
        if ! curl -fsSL --connect-timeout 15 -o "$tmp" "${MODELS_URL%/}/manifest.json"; then
            rm -f "$tmp"
            echo "[ERROR] Could not fetch manifest.json from: ${MODELS_URL%/}/manifest.json" >&2
            return 1
        fi
        manifest="$tmp"
    else
        # Sin fuente de repositorio (internet): no hay nada que validar.
        return 0
    fi

    if ! python3 - "$manifest" <<'PY'
import json
import sys

path = sys.argv[1]
try:
    with open(path, encoding="utf-8") as fh:
        manifest = json.load(fh)
except (json.JSONDecodeError, OSError) as e:
    print(f"[ERROR] Invalid manifest.json: {e}", file=sys.stderr)
    sys.exit(1)

files = manifest.get("files", [])
if not isinstance(files, list) or not files:
    print("[ERROR] manifest.json contains no files.", file=sys.stderr)
    sys.exit(1)

if not any(f.get("path", "").startswith(("ct2/", "marian/")) for f in files):
    print("[ERROR] manifest.json contains no model set (ct2/ or marian/).", file=sys.stderr)
    sys.exit(1)
PY
    then
        [ -n "$tmp" ] && rm -f "$tmp"
        return 1
    fi

    [ -n "$tmp" ] && rm -f "$tmp"
    return 0
}

if ! validate_models_source; then
    exit 1
fi

# True si el manifest del repositorio contiene algún fichero del set dado
# (ct2|marian). Se usa con el modo 'all' para la pasada no fatal de ct2.
repo_has_set() {
    python3 - "$1" "$2" <<'PY'
import json
import sys

path, set_name = sys.argv[1], sys.argv[2]
try:
    with open(path, encoding="utf-8") as fh:
        manifest = json.load(fh)
except (json.JSONDecodeError, OSError):
    sys.exit(1)
for f in manifest.get("files", []):
    if f.get("path", "").startswith(set_name + "/"):
        sys.exit(0)
sys.exit(1)
PY
}

# =========================================================
# Wheels Python del repositorio (USB/LAN): permiten instalar
# TODAS las dependencias (pip, deps, torch, sacremoses) sin
# internet (--no-index --find-links). Los repos sin wheels
# mantienen el comportamiento histórico (internet).
# =========================================================
WHEELS_LINKS=""
WHEELS_TMP_DIR=""

repo_has_wheels() {
    python3 - "$1" <<'PY'
import json
import sys

try:
    with open(sys.argv[1], encoding="utf-8") as fh:
        manifest = json.load(fh)
except Exception:
    sys.exit(1)
sys.exit(0 if any(f.get("path", "").startswith("wheels/") for f in manifest.get("files", [])) else 1)
PY
}

# Descarga las entradas wheels/* del repo HTTP verificando size+sha256.
fetch_repo_wheels() {
    python3 - "$1" "${MODELS_URL%/}" "$2" <<'PY'
import hashlib
import json
import os
import subprocess
import sys

manifest_path, base_url, dest = sys.argv[1], sys.argv[2], sys.argv[3]
with open(manifest_path, encoding="utf-8") as fh:
    manifest = json.load(fh)

entries = [e for e in manifest.get("files", []) if e.get("path", "").startswith("wheels/")]
if not entries:
    print("[ERROR] manifest.json contains no wheels.", file=sys.stderr)
    sys.exit(1)


def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1024 * 1024), b""):
            h.update(chunk)
    return h.hexdigest()


for entry in entries:
    rel = entry["path"]
    size = entry.get("size")
    sha = entry.get("sha256")
    local = os.path.join(dest, rel)
    os.makedirs(os.path.dirname(local), exist_ok=True)
    if os.path.exists(local) and size is not None and os.path.getsize(local) == size:
        if not sha or sha256_file(local) == sha:
            print(f"[wheels] {rel} (already present)")
            continue
    url = f"{base_url}/{rel}"
    print(f"[wheels] downloading {rel}")
    rc = subprocess.run(
        ["curl", "-fSL", "--connect-timeout", "15", "-o", local, url]
    ).returncode
    if rc != 0:
        subprocess.run(["rm", "-f", local])
        print(f"[ERROR] Failed to download {url}", file=sys.stderr)
        sys.exit(1)
    if size is not None and os.path.getsize(local) != size:
        print(f"[ERROR] Size mismatch for {rel}", file=sys.stderr)
        sys.exit(1)
    if sha and sha256_file(local) != sha:
        print(f"[ERROR] SHA256 mismatch for {rel}", file=sys.stderr)
        sys.exit(1)
sys.exit(0)
PY
}

detect_repo_wheels() {
    if [ -n "$MODELS_DIR" ]; then
        if [ -d "$MODELS_DIR/wheels" ] && [ -n "$(ls -A "$MODELS_DIR/wheels" 2>/dev/null)" ] \
            && repo_has_wheels "$MODELS_DIR/manifest.json"; then
            WHEELS_LINKS="$MODELS_DIR/wheels"
            echo "[setup] $(_ "Using repository wheels for Python dependencies (offline)")"
        fi
    elif [ -n "$MODELS_URL" ]; then
        local tmp_manifest tmp_dir
        tmp_manifest="$(mktemp)"
        if curl -fsSL --connect-timeout 15 -o "$tmp_manifest" "${MODELS_URL%/}/manifest.json" 2>/dev/null \
            && repo_has_wheels "$tmp_manifest"; then
            tmp_dir="$(mktemp -d)"
            if fetch_repo_wheels "$tmp_manifest" "$tmp_dir"; then
                # Los wheels se descargan en $tmp_dir/wheels (el prefijo
                # "wheels/" viaja en el manifest); --find-links solo
                # escanea el primer nivel del directorio.
                WHEELS_LINKS="$tmp_dir/wheels"
                WHEELS_TMP_DIR="$tmp_dir"
                echo "[setup] $(_ "Using repository wheels for Python dependencies (offline)")"
            else
                rm -rf "$tmp_dir"
                echo "[ERROR] $(_ "Repository declares Python wheels, but they could not be downloaded or verified.")" >&2
                rm -f "$tmp_manifest"
                return 1
            fi
        fi
        rm -f "$tmp_manifest"
    fi
}

if ! detect_repo_wheels; then
    exit 1
fi

validate_local_wheels_manifest() {
    [ -n "$MODELS_DIR" ] || return 0
    [ -n "$WHEELS_LINKS" ] || return 0

    echo "[setup] $(_ "Validating repository wheels against manifest.json")"

    python3 - "$MODELS_DIR" <<'PY'
import hashlib
import json
import os
import sys

repo = os.path.abspath(sys.argv[1])
manifest_path = os.path.join(repo, "manifest.json")
wheels_dir = os.path.join(repo, "wheels")

try:
    with open(manifest_path, encoding="utf-8") as fh:
        manifest = json.load(fh)
except (OSError, json.JSONDecodeError) as exc:
    print(f"[ERROR] Invalid manifest.json: {exc}", file=sys.stderr)
    sys.exit(1)

entries = {
    e["path"]: e
    for e in manifest.get("files", [])
    if e.get("path", "").startswith("wheels/")
}

if not entries:
    print("[ERROR] manifest.json contains no wheel entries.", file=sys.stderr)
    sys.exit(1)


def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1024 * 1024), b""):
            h.update(chunk)
    return h.hexdigest()


errors = 0

for rel, entry in sorted(entries.items()):
    path = os.path.join(repo, rel)

    if not os.path.isfile(path):
        print(f"[ERROR] Wheel listed in manifest but missing: {rel}", file=sys.stderr)
        errors += 1
        continue

    expected_size = entry.get("size")
    if expected_size is not None and os.path.getsize(path) != expected_size:
        print(f"[ERROR] Size mismatch: {rel}", file=sys.stderr)
        errors += 1
        continue

    expected_sha = entry.get("sha256")
    if expected_sha and sha256_file(path) != expected_sha:
        print(f"[ERROR] SHA256 mismatch: {rel}", file=sys.stderr)
        errors += 1


actual = set()

for root, _, files in os.walk(wheels_dir):
    for name in files:
        if not name.endswith(".whl"):
            continue
        path = os.path.join(root, name)
        actual.add(os.path.relpath(path, repo))

extra = sorted(actual - set(entries))

for rel in extra:
    print(
        f"[ERROR] Wheel exists but is not listed in manifest.json: {rel}",
        file=sys.stderr,
    )
    errors += 1

if errors:
    print(
        "[ERROR] Repository wheels and manifest.json are inconsistent.",
        file=sys.stderr,
    )
    sys.exit(1)

print(f"[ok] {len(entries)} wheel entries verified.")
PY
}

validate_offline_wheelhouse() {
    [ -n "$WHEELS_LINKS" ] || return 0

    local tmp_dir
    local test_venv
    local test_python
    local test_log
    local requirements=("${BASE_PYTHON_PKGS[@]}")

    if [ "$USE_CT2" = "true" ] || [ "$USE_ALL" = "true" ]; then
        requirements+=("${CT2_PYTHON_PKGS[@]}")
    fi

    requirements+=("${FINAL_PYTHON_PKGS[@]}")

    tmp_dir="$(mktemp -d)"
    test_venv="$tmp_dir/venv"
    test_log="$tmp_dir/pip-preflight.log"

    echo "[setup] $(_ "Validating offline Python dependencies for Python") $PYTHON_VER"

    if ! python3 -m venv "$test_venv" >"$test_log" 2>&1; then
        echo "[ERROR] $(_ "Could not create temporary validation venv.")" >&2
        cat "$test_log" >&2
        rm -rf "$tmp_dir"
        return 1
    fi

    test_python="$test_venv/bin/python"

    if ! "$test_python" -m pip install \
        --no-cache-dir \
        --no-index \
        --find-links "$WHEELS_LINKS" \
        --upgrade \
        pip setuptools wheel \
        >>"$test_log" 2>&1
    then
        echo "[ERROR] $(_ "The offline wheels repository cannot install pip/setuptools/wheel.")" >&2
        tail -n 60 "$test_log" >&2
        rm -rf "$tmp_dir"
        return 1
    fi

    if ! "$test_python" -m pip install \
        --dry-run \
        --no-cache-dir \
        --no-index \
        --find-links "$WHEELS_LINKS" \
        "${requirements[@]}" \
        >>"$test_log" 2>&1
    then
        echo "[ERROR] $(_ "Offline Python repository is incomplete for Python") $PYTHON_VER." >&2
        echo "[ERROR] $(_ "pip dependency resolution output:")" >&2
        tail -n 60 "$test_log" >&2
        rm -rf "$tmp_dir"
        return 1
    fi

    rm -rf "$tmp_dir"

    echo "[ok] $(_ "Offline Python dependencies are complete for Python") $PYTHON_VER."
}

# =========================================================
# Detección de modelos ya presentes en disco
# =========================================================
MISSING_FILES=()
DETECT_TMP="$(mktemp)"
trap 'rm -f "$DETECT_TMP"; [ -n "${WHEELS_TMP_DIR:-}" ] && rm -rf "$WHEELS_TMP_DIR" || true' EXIT

detect_local_models() {
    local whisper_ok=false
    local ct2_ok=true
    local marian_ok=true
    local any=false

    if [ -s "$SHARED_HF/whisper-small/model.bin" ] && [ -s "$SHARED_HF/whisper-small/config.json" ]; then
        whisper_ok=true
    fi

    for pair in "${MARIAN_PAIRS[@]}"; do
        if [ ! -s "$SHARED_HF/ct2/opus-mt-$pair/model.bin" ]; then
            ct2_ok=false
        fi
        if [ ! -s "$SHARED_HF/marian/opus-mt-$pair/pytorch_model.bin" ]; then
            marian_ok=false
        fi
    done

    for d in whisper-small ct2 marian; do
        if [ -d "$SHARED_HF/$d" ] && [ -n "$(ls -A "$SHARED_HF/$d" 2>/dev/null)" ]; then
            any=true
        fi
    done

    printf 'whisper_ok=%s\nct2_ok=%s\nmarian_ok=%s\n' "$whisper_ok" "$ct2_ok" "$marian_ok" > "$DETECT_TMP"

    if $whisper_ok && $ct2_ok && $marian_ok; then
        echo "complete:both"
    elif $whisper_ok && $ct2_ok; then
        echo "complete:ct2"
    elif $whisper_ok && $marian_ok; then
        echo "complete:marian"
    elif $any; then
        echo "partial"
    else
        echo "none"
    fi
}

DETECT_STATE="$(detect_local_models)"
# shellcheck disable=SC1090
. "$DETECT_TMP"

# =========================================================
# Modo --detect (utilidad de depuración; el zero-center ya
# siempre pregunta y la descarga decide por-set)
# =========================================================
if [ "$DETECT_ONLY" = "true" ]; then
    echo "$DETECT_STATE"
    exit 0
fi

# =========================================================
# Marcador del modo efectivo (ct2 => .use_ct2, marian => .use_marian)
# =========================================================
set_mode_marker() {
    if [ "$USE_CT2" = "true" ]; then
        touch "$SHARED_HF/.use_ct2"
        rm -f "$SHARED_HF/.use_marian"
    else
        touch "$SHARED_HF/.use_marian"
        rm -f "$SHARED_HF/.use_ct2"
        if [ "$USE_ALL" = "true" ]; then
            echo "[WARN] $(_ "Both model sets installed. To use the optimized mode, rename the .use_marian marker to .use_ct2 manually in:") $SHARED_HF" >&2
        fi
    fi
}

# =========================================================
# Dependencias Python: desde los wheels del repositorio
# (offline total) cuando existen, o desde internet. Todos los
# fallos de pip son fatales y quedan registrados en PIP_LOG.
# =========================================================
PIP_LOG="/var/log/traduia-install.log"

show_pip_error() {
    echo "[ERROR] $(_ "Python dependency installation failed.")" >&2
    echo "[ERROR] $(_ "Last lines of:") $PIP_LOG" >&2
    tail -n 60 "$PIP_LOG" >&2 || true
}

run_pip_fatal() {
    printf '\n===== %s =====\n' "$(date -Is)" >>"$PIP_LOG"
    printf 'python -m pip' >>"$PIP_LOG"
    printf ' %q' "$@" >>"$PIP_LOG"
    printf '\n' >>"$PIP_LOG"

    if ! python -m pip "$@" >>"$PIP_LOG" 2>&1; then
        show_pip_error
        return 1
    fi
}

install_python_deps() {
    local offline_args=()
    local requirements=("${BASE_PYTHON_PKGS[@]}")

    if [ "$USE_CT2" = "true" ] || [ "$USE_ALL" = "true" ]; then
        requirements+=("${CT2_PYTHON_PKGS[@]}")
    fi

    mkdir -p "$(dirname "$PIP_LOG")"
    touch "$PIP_LOG"

    if [ -n "$WHEELS_LINKS" ]; then
        offline_args=(
            --no-index
            --find-links "$WHEELS_LINKS"
        )
    elif [ -n "$MODELS_DIR$MODELS_URL" ]; then
        echo "[WARN] $(_ "No wheels in repository; Python dependencies will be downloaded from the internet.")" >&2
    fi

    echo "[setup] $(_ "Updating pip/setuptools/wheel")"

    run_pip_fatal install \
        --no-cache-dir \
        "${offline_args[@]}" \
        --upgrade \
        pip setuptools wheel \
        || return 1

    echo "[setup] $(_ "Installing Python dependencies")"

    run_pip_fatal install \
        --no-cache-dir \
        "${offline_args[@]}" \
        "${requirements[@]}" \
        || return 1

    echo "[setup] $(_ "Installing torch")"

    if [ -n "$WHEELS_LINKS" ]; then
        run_pip_fatal install \
            --no-cache-dir \
            "${offline_args[@]}" \
            torch \
            || return 1
    else
        run_pip_fatal install \
            --no-cache-dir \
            --index-url https://download.pytorch.org/whl/cpu \
            torch \
            || return 1
    fi

    echo "[setup] $(_ "Installing sacremoses")"

    run_pip_fatal install \
        --no-cache-dir \
        "${offline_args[@]}" \
        sacremoses \
        || return 1

    echo "[setup] $(_ "Checking Python dependency consistency")"

    if ! python -m pip check >>"$PIP_LOG" 2>&1; then
        echo "[ERROR] $(_ "pip check failed: installed dependencies are inconsistent.")" >&2
        tail -n 60 "$PIP_LOG" >&2
        return 1
    fi

    echo "[ok] $(_ "Python dependencies installed and verified.")"
}

mkdir -p "$SHARED_HF"/{hub,transformers,datasets} || true

# Validar completamente el repositorio de wheels antes de modificar
# el entorno definitivo de TraduIA.
if [ -n "$WHEELS_LINKS" ]; then
    if [ -n "$MODELS_DIR" ]; then
        validate_local_wheels_manifest || exit 1
    fi

    validate_offline_wheelhouse || exit 1
fi

echo "[setup] $(_ "Creating clean venv") $TRADUIA_VENV"

if ! python3 -m venv --clear "$TRADUIA_VENV"; then
    echo "[ERROR] $(_ "Could not create TraduIA virtual environment.")" >&2
    exit 1
fi

echo "[setup] $(_ "Activating venv")"

echo "$TRADUIA_DIR" \
    > "$TRADUIA_VENV/lib/python${PYTHON_VER}/site-packages/extra_paths.pth"

source "$TRADUIA_VENV/bin/activate"

if ! install_python_deps; then
    echo "[ERROR] $(_ "TraduIA Python environment could not be prepared.")" >&2
    exit 1
fi

echo "[setup] $(_ "Exporting shared cache variables (for this process)")"
export HF_HOME="$SHARED_HF"
export HF_HUB_CACHE="$SHARED_HF/hub"
export TRANSFORMERS_CACHE="$SHARED_HF/transformers"
export HF_DATASETS_CACHE="$SHARED_HF/datasets"
export ALICIA_MODEL_CACHE="$SHARED_HF/hub"

# Durante la precarga NO ponemos offline=1 (si falta algo, debe descargar).
unset HF_HUB_OFFLINE || true
unset TRANSFORMERS_OFFLINE || true

function download_inet_models(){
    echo "[setup] $(_ "Preloading models into shared cache...")"
    python - <<'PY'
import os
from huggingface_hub import snapshot_download, model_info
from huggingface_hub.utils import disable_progress_bars, RepositoryNotFoundError
disable_progress_bars()

print("[preload] HF_HOME:", os.environ.get("HF_HOME"))
print("[preload] HF_HUB_CACHE:", os.environ.get("HF_HUB_CACHE"))

# === WHISPER MODEL ===
# Primary: mijuanlo/whisper-small-ct2-int8
# Fallback: Systran/faster-whisper-small
# To force original method: comment mijuanlo block, uncomment Systran block below

WHISPER_LOCAL = os.environ.get("TRADUIA_MODELS_DIR", "/opt/ai/traduia/models") + "/whisper-small"
_whisper_repo = None
try:
    model_info("mijuanlo/whisper-small-ct2-int8")
    _whisper_repo = "mijuanlo/whisper-small-ct2-int8"
    print(f"[preload] Using whisper from: {_whisper_repo}")
except (RepositoryNotFoundError, Exception):
    _whisper_repo = "Systran/faster-whisper-small"
    print(f"[preload] mijuanlo whisper not available, fallback to: {_whisper_repo}")

print(f"[preload] Downloading: {_whisper_repo} -> {WHISPER_LOCAL}")
snapshot_download(repo_id=_whisper_repo, local_dir=WHISPER_LOCAL)

# === MARIANMT MODELS (only when not using CTranslate2) ===
# Primary: mijuanlo/opus-mt-{pair}
# Fallback: Helsinki-NLP/opus-mt-{pair}

use_ct2 = os.environ.get("USE_CT2_MODE", "false").strip().lower() == "true"
if not use_ct2:
    marian_pairs = [
        "es-en", "es-fr", "es-de", "es-ru", "es-ar",
        "es-uk", "es-ro", "es-it", "ca-en", "ca-es",
    ]
    marian_base = os.environ.get("TRADUIA_MODELS_DIR", "/opt/ai/traduia/models") + "/marian"
    for pair in marian_pairs:
        local = f"{marian_base}/opus-mt-{pair}"
        try:
            repo = f"mijuanlo/opus-mt-{pair}"
            model_info(repo)
            print(f"[preload] Downloading: {repo} -> {local}")
            snapshot_download(repo_id=repo, local_dir=local)
        except (RepositoryNotFoundError, Exception):
            repo = f"Helsinki-NLP/opus-mt-{pair}"
            print(f"[preload] Fallback: {repo} -> {local}")
            snapshot_download(repo_id=repo, local_dir=local)

print("[preload] OK. Models cached.")
PY
}

function download_http_models(){
    local base_url="$1"
    local mode="$2"
    echo "[setup] $(_ "Downloading models from HTTP repository:") $base_url"
    echo "[setup] $(_ "Fetching manifest:") $base_url/manifest.json"

    local tmp_manifest
    tmp_manifest="$(mktemp)"
    if ! curl -fsSL --connect-timeout 15 -o "$tmp_manifest" "$base_url/manifest.json"; then
        rm -f "$tmp_manifest"
        echo "[ERROR] $(_ "Could not fetch manifest.json from:") $base_url/manifest.json" >&2
        echo "[ERROR] $(_ "Check TRADUIA_MODELS_URL or /etc/traduia/models-repo.conf")" >&2
        exit 1
    fi

    python3 - "$tmp_manifest" "$base_url" "$SHARED_HF" "$mode" <<'PY'
import hashlib
import json
import os
import subprocess
import sys

manifest_path, base_url, local_base, mode = sys.argv[1], sys.argv[2].rstrip("/"), sys.argv[3], sys.argv[4]

MARIAN_PAIRS = [
    "es-en", "es-fr", "es-de", "es-ru", "es-ar",
    "es-uk", "es-ro", "es-it", "ca-en", "ca-es",
]
WHISPER_FILES = ["model.bin", "config.json"]
CT2_MANDATORY_FILES = ["model.bin", "config.json", "source.spm", "target.spm"]
MARIAN_REQUIRED_FILES = ["pytorch_model.bin", "config.json", "source.spm", "target.spm", "vocab.json"]

try:
    with open(manifest_path, encoding="utf-8") as fh:
        manifest = json.load(fh)
except (json.JSONDecodeError, OSError) as e:
    print(f"[ERROR] Invalid manifest.json: {e}", file=sys.stderr)
    sys.exit(1)

files = manifest.get("files", [])
if not files:
    print("[ERROR] manifest.json contains no files.", file=sys.stderr)
    sys.exit(1)

print(f"[manifest] version={manifest.get('version')} mode={manifest.get('mode')} files={len(files)}")

# --- Validación de completitud del set pedido (antes de descargar) ---
required_files = [f"whisper-small/{x}" for x in WHISPER_FILES]
if mode == "ct2":
    for pair in MARIAN_PAIRS:
        for x in CT2_MANDATORY_FILES:
            required_files.append(f"ct2/opus-mt-{pair}/{x}")
else:
    for pair in MARIAN_PAIRS:
        for x in MARIAN_REQUIRED_FILES:
            required_files.append(f"marian/opus-mt-{pair}/{x}")

manifest_paths = {f["path"] for f in files}
missing = [p for p in required_files if p not in manifest_paths]

# Vocabulario ct2: vocabulary.txt (conversión local) o vocab.json (pre-convertido)
if mode == "ct2":
    for pair in MARIAN_PAIRS:
        base = f"ct2/opus-mt-{pair}"
        if f"{base}/vocabulary.txt" not in manifest_paths and f"{base}/vocab.json" not in manifest_paths:
            missing.append(f"{base}/vocabulary.txt o vocab.json")

if missing:
    print(f"[ERROR] Repository does not contain the complete '{mode}' model set. Missing:", file=sys.stderr)
    for m in missing:
        print(f"  - {m}", file=sys.stderr)
    print("[ERROR] Publish the set with traduia-make-repo or fix TRADUIA_MODELS_URL.", file=sys.stderr)
    sys.exit(1)

def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1024 * 1024), b""):
            h.update(chunk)
    return h.hexdigest()

def wanted(rel):
    # Saltar artefactos .cache de HuggingFace y pesos tf_model.h5 no usados
    # (repos generados con versiones antiguas)
    if ".cache/" in rel or rel.endswith(".h5"):
        return False
    return rel.startswith("whisper-small/") or rel.startswith(f"{mode}/")

for entry in files:
    rel = entry["path"]
    if not wanted(rel):
        print(f"[SKIP ] {rel} (not in mode {mode})")
        continue
    size = entry.get("size")
    sha = entry.get("sha256")
    local = os.path.join(local_base, rel)
    os.makedirs(os.path.dirname(local), exist_ok=True)

    # Si el fichero ya está completo y verificado, no se descarga.
    if os.path.exists(local) and size is not None and os.path.getsize(local) == size:
        if sha and sha256_file(local) == sha:
            print(f"[SKIP ] {rel} (already present and verified)")
            continue
        if not sha:
            print(f"[SKIP ] {rel} (already present, size OK)")
            continue

    url = f"{base_url}/{rel}"
    print(f"[FILE ] {rel}")

    # Intento con reanudación; si falla, descarga completa.
    rc = subprocess.run(
        ["curl", "-fSL", "-C", "-", "--connect-timeout", "15", "-o", local, url]
    ).returncode
    if rc != 0:
        subprocess.run(["rm", "-f", local])
        rc = subprocess.run(
            ["curl", "-fSL", "--connect-timeout", "15", "-o", local, url]
        ).returncode
    if rc != 0:
        print(f"[ERROR] Failed to download {url}", file=sys.stderr)
        sys.exit(1)

    if size is not None and os.path.getsize(local) != size:
        print(f"[ERROR] Size mismatch for {rel}: expected {size}, got {os.path.getsize(local)}", file=sys.stderr)
        sys.exit(1)

    if sha and sha256_file(local) != sha:
        print(f"[ERROR] SHA256 mismatch for {rel}", file=sys.stderr)
        print("[ERROR] The repository may be corrupted or served by a stale proxy cache.", file=sys.stderr)
        sys.exit(1)

print("[download] OK. All files downloaded and verified.")

# --- Manifest persistente para verificación posterior ---
def _set_complete_local(subdir, file_name):
    for pair in MARIAN_PAIRS:
        f = os.path.join(local_base, subdir, f"opus-mt-{pair}", file_name)
        if not os.path.isfile(f) or os.path.getsize(f) == 0:
            return False
    return True


ct2_ok = _set_complete_local("ct2", "model.bin")
marian_ok = _set_complete_local("marian", "pytorch_model.bin")

persisted = [e for e in files if e["path"].startswith("whisper-small/")]
if ct2_ok:
    persisted += [e for e in files if e["path"].startswith("ct2/")]
if marian_ok:
    persisted += [e for e in files if e["path"].startswith("marian/")]
persisted.sort(key=lambda e: e["path"])

if ct2_ok and marian_ok:
    persist_mode = "both"
elif ct2_ok:
    persist_mode = "ct2"
elif marian_ok:
    persist_mode = "marian"
else:
    persist_mode = "none"

persisted_manifest = {
    "version": manifest.get("version", 1),
    "generated": manifest.get("generated", ""),
    "mode": persist_mode,
    "files": persisted,
}
with open(os.path.join(local_base, "manifest.json"), "w", encoding="utf-8") as fh:
    json.dump(persisted_manifest, fh, indent=2, ensure_ascii=False)
    fh.write("\n")
print(f"[ok] Persistent manifest written ({len(persisted)} files)")
PY

    rm -f "$tmp_manifest"
    echo "[setup] $(_ "HTTP download finished.")"
}

function copy_from_dir(){
    local src_dir="$1"
    local mode="$2"
    echo "[setup] $(_ "Copying models from local directory:") $src_dir"
    python3 - "$src_dir" "$SHARED_HF" "$mode" <<'PY'
import hashlib
import json
import os
import shutil
import sys

src_dir, local_base, mode = sys.argv[1].rstrip("/"), sys.argv[2], sys.argv[3]

MARIAN_PAIRS = [
    "es-en", "es-fr", "es-de", "es-ru", "es-ar",
    "es-uk", "es-ro", "es-it", "ca-en", "ca-es",
]
WHISPER_FILES = ["model.bin", "config.json"]
CT2_MANDATORY_FILES = ["model.bin", "config.json", "source.spm", "target.spm"]
MARIAN_REQUIRED_FILES = ["pytorch_model.bin", "config.json", "source.spm", "target.spm", "vocab.json"]

manifest_path = os.path.join(src_dir, "manifest.json")
if not os.path.exists(manifest_path):
    print(f"[ERROR] No manifest.json found in source directory: {src_dir}", file=sys.stderr)
    sys.exit(1)

try:
    with open(manifest_path, encoding="utf-8") as fh:
        manifest = json.load(fh)
except (json.JSONDecodeError, OSError) as e:
    print(f"[ERROR] Invalid manifest.json: {e}", file=sys.stderr)
    sys.exit(1)

files = manifest.get("files", [])
if not files:
    print("[ERROR] manifest.json contains no files.", file=sys.stderr)
    sys.exit(1)

# --- Validación de completitud del set pedido (igual que el cliente HTTP) ---
required = [f"whisper-small/{x}" for x in WHISPER_FILES]
if mode == "ct2":
    for pair in MARIAN_PAIRS:
        for x in CT2_MANDATORY_FILES:
            required.append(f"ct2/opus-mt-{pair}/{x}")
else:
    for pair in MARIAN_PAIRS:
        for x in MARIAN_REQUIRED_FILES:
            required.append(f"marian/opus-mt-{pair}/{x}")

manifest_paths = {f["path"] for f in files}
missing = [p for p in required if p not in manifest_paths]
if mode == "ct2":
    for pair in MARIAN_PAIRS:
        base = f"ct2/opus-mt-{pair}"
        if f"{base}/vocabulary.txt" not in manifest_paths and f"{base}/vocab.json" not in manifest_paths:
            missing.append(f"{base}/vocabulary.txt o vocab.json")

if missing:
    print(f"[ERROR] Source directory does not contain the complete '{mode}' model set. Missing:", file=sys.stderr)
    for m in missing:
        print(f"  - {m}", file=sys.stderr)
    if mode == "ct2":
        print("[ERROR] Use 'optimized' or point to a source that contains the ct2 set.", file=sys.stderr)
    else:
        print("[ERROR] Run without 'optimized' or point to a source that contains the marian set.", file=sys.stderr)
    sys.exit(1)

def sha256_file(path):
    h = hashlib.sha256()
    with open(path, "rb") as fh:
        for chunk in iter(lambda: fh.read(1024 * 1024), b""):
            h.update(chunk)
    return h.hexdigest()

def wanted(rel):
    return rel.startswith("whisper-small/") or rel.startswith(f"{mode}/")

copied = 0
skipped = 0
for entry in files:
    rel = entry["path"]
    if not wanted(rel):
        continue
    size = entry.get("size")
    sha = entry.get("sha256")
    src = os.path.join(src_dir, rel)
    dst = os.path.join(local_base, rel)
    if not os.path.exists(src):
        print(f"[ERROR] File listed in manifest but missing in source: {rel}", file=sys.stderr)
        sys.exit(1)
    # Si el fichero ya está completo y verificado en el destino, no se copia.
    if os.path.exists(dst) and size is not None and os.path.getsize(dst) == size:
        if sha and sha256_file(dst) == sha:
            print(f"[SKIP ] {rel} (already present and verified)")
            skipped += 1
            continue
        if not sha:
            print(f"[SKIP ] {rel} (already present, size OK)")
            skipped += 1
            continue
    os.makedirs(os.path.dirname(dst), exist_ok=True)
    shutil.copy2(src, dst)
    print(f"[COPY ] {rel}")
    copied += 1

print(f"[ok] {copied} copied, {skipped} skipped")

# --- Pasada final: verificar en el destino el set instalado ---
verify_errors = 0
for entry in files:
    rel = entry["path"]
    if not wanted(rel):
        continue
    size = entry.get("size")
    sha = entry.get("sha256")
    dst = os.path.join(local_base, rel)
    try:
        if not os.path.exists(dst) or os.path.getsize(dst) != size:
            print(f"[VERIFY] ERROR: {rel} (missing/size)")
            verify_errors += 1
            continue
        h = hashlib.sha256()
        with open(dst, "rb") as fh:
            for chunk in iter(lambda: fh.read(1024 * 1024), b""):
                h.update(chunk)
        if sha and h.hexdigest() != sha:
            print(f"[VERIFY] ERROR: {rel} (sha256 mismatch)")
            verify_errors += 1
        else:
            print(f"[VERIFY] OK: {rel}")
    except OSError as e:
        print(f"[VERIFY] ERROR: {rel} ({e})")
        verify_errors += 1
if verify_errors:
    print(f"[ERROR] Verification failed: {verify_errors} files with errors", file=sys.stderr)
    sys.exit(1)
print("[ok] All files verified in destination.")

# --- Manifest persistente para verificación posterior ---
def _set_complete_local(subdir, file_name):
    for pair in MARIAN_PAIRS:
        f = os.path.join(local_base, subdir, f"opus-mt-{pair}", file_name)
        if not os.path.isfile(f) or os.path.getsize(f) == 0:
            return False
    return True


ct2_ok = _set_complete_local("ct2", "model.bin")
marian_ok = _set_complete_local("marian", "pytorch_model.bin")

persisted = [e for e in files if e["path"].startswith("whisper-small/")]
if ct2_ok:
    persisted += [e for e in files if e["path"].startswith("ct2/")]
if marian_ok:
    persisted += [e for e in files if e["path"].startswith("marian/")]
persisted.sort(key=lambda e: e["path"])

if ct2_ok and marian_ok:
    persist_mode = "both"
elif ct2_ok:
    persist_mode = "ct2"
elif marian_ok:
    persist_mode = "marian"
else:
    persist_mode = "none"

persisted_manifest = {
    "version": manifest.get("version", 1),
    "generated": manifest.get("generated", ""),
    "mode": persist_mode,
    "files": persisted,
}
with open(os.path.join(local_base, "manifest.json"), "w", encoding="utf-8") as fh:
    json.dump(persisted_manifest, fh, indent=2, ensure_ascii=False)
    fh.write("\n")
print(f"[ok] Persistent manifest written ({len(persisted)} files)")
PY
    echo "[setup] $(_ "Local copy finished.")"
}

# =========================================================
# Descarga de modelos (por-set: skip solo si el set pedido
# ya está completo en disco; si no, se descarga/completa)
# =========================================================
echo "$(_ "Downloading models")"
export USE_CT2_MODE="$USE_CT2"

# Modo 'all': ambos sets, marian primero (prioridad). Con --dir/--url la
# pasada de ct2 es no fatal (si el repo no la trae, aviso y se continúa).
if [ "$USE_ALL" = "true" ]; then
    REQUESTED_SETS=(marian ct2)
elif [ "$USE_CT2" = "true" ]; then
    REQUESTED_SETS=(ct2)
else
    REQUESTED_SETS=(marian)
fi

for REQUESTED_SET in "${REQUESTED_SETS[@]}"; do
    if [ "$REQUESTED_SET" = "ct2" ]; then
        REQUESTED_SET_OK="$ct2_ok"
    else
        REQUESTED_SET_OK="$marian_ok"
    fi

    if [ "$whisper_ok" = "true" ] && [ "$REQUESTED_SET_OK" = "true" ]; then
        echo "[setup] $(_ "{} models already present on disk. Skipping downloads.")" | sed "s|{}|$REQUESTED_SET|"
        continue
    fi
    if [ "$REQUESTED_SET_OK" != "true" ]; then
        echo "[setup] $(_ "{} models incomplete on disk; completing download.")" | sed "s|{}|$REQUESTED_SET|"
    fi
    if [ -n "$MODELS_DIR" ]; then
        if [ "$USE_ALL" = "true" ] && [ "$REQUESTED_SET" = "ct2" ] \
            && ! repo_has_set "$MODELS_DIR/manifest.json" "ct2"; then
            echo "[WARN] $(_ "ct2 set not available in repository; skipping.")" >&2
            continue
        fi
        copy_from_dir "$MODELS_DIR" "$REQUESTED_SET"
    elif [ -n "$MODELS_URL" ]; then
        if [ "$USE_ALL" = "true" ] && [ "$REQUESTED_SET" = "ct2" ]; then
            tmp_manifest_url="$(mktemp)"
            if ! curl -fsSL --connect-timeout 15 -o "$tmp_manifest_url" "${MODELS_URL%/}/manifest.json"; then
                rm -f "$tmp_manifest_url"
                echo "[WARN] $(_ "Could not fetch manifest.json from:") ${MODELS_URL%/}/manifest.json" >&2
                continue
            elif ! repo_has_set "$tmp_manifest_url" "ct2"; then
                rm -f "$tmp_manifest_url"
                echo "[WARN] $(_ "ct2 set not available in repository; skipping.")" >&2
                continue
            fi
            rm -f "$tmp_manifest_url"
        fi
        download_http_models "$MODELS_URL" "$REQUESTED_SET"
    else
        # Internet + 'all': marian se descarga con download_inet_models
        # (whisper+marian); el set ct2 lo descarga el bloque CT2 posterior.
        if [ "$USE_ALL" = "true" ] && [ "$REQUESTED_SET" = "ct2" ]; then
            continue
        fi
        # Con 'all optimized' (USE_CT2=true) la pasada marian debe descargar
        # marian: download_inet_models saltaría los pares si USE_CT2_MODE=true.
        if [ "$REQUESTED_SET" = "marian" ]; then
            export USE_CT2_MODE=false
        fi
        download_inet_models
    fi
done
echo "$(_ "Models downloaded!")"

# Bloque CT2: con 'optimized' descarga pre-convertidos (mijuanlo) o
# convierte; con 'all' + internet descarga los pre-convertidos. Con
# 'all' + --dir/--url el set ct2 ya lo trata el bucle de descarga
# (no fatal si el repo no lo trae), así que este bloque no se ejecuta.
if [ "$USE_CT2" = "true" ] \
    || { [ "$USE_ALL" = "true" ] && [ -z "$MODELS_DIR" ] && [ -z "$MODELS_URL" ]; }; then
    echo "[setup] $(_ "Downloading/Converting Marian models to CTranslate2...")"
    mkdir -p "$SHARED_HF/ct2"

    python - <<'PY'
import os, json, subprocess, shutil, tempfile
from huggingface_hub import snapshot_download, model_info
from huggingface_hub.utils import disable_progress_bars, RepositoryNotFoundError
disable_progress_bars()

HF_HOME = os.environ.get("HF_HOME", "/opt/ai/traduia/models")
CT2_BASE = os.path.join(HF_HOME, "ct2")

MARIAN_PAIRS = [
    "es-en", "es-fr", "es-de", "es-ru", "es-ar",
    "es-uk", "es-ro", "es-it", "ca-en", "ca-es",
]

TOKENIZER_FILES = [
    "tokenizer_config.json",
    "vocab.json",
    "source.spm",
    "target.spm",
    "special_tokens_map.json",
    "tokenizer.json",
]


def _has_ct2_model(out_dir):
    model_bin = os.path.join(out_dir, "model.bin")
    if os.path.exists(model_bin) and os.path.getsize(model_bin) > 0:
        return True
    if os.path.isdir(os.path.join(out_dir, "model")):
        return True
    return False


for pair in MARIAN_PAIRS:
    out_dir = os.path.join(CT2_BASE, f"opus-mt-{pair}")

    if _has_ct2_model(out_dir):
        print(f"[CT2] opus-mt-{pair} already available, skipping.")
        continue

    # === Primary: download pre-converted from mijuanlo ===
    # Repo: mijuanlo/opus-mt-{pair}-ct2-int8
    # To force original method: comment this block, uncomment fallback block below
    try:
        repo = f"mijuanlo/opus-mt-{pair}-ct2-int8"
        model_info(repo)
        print(f"[CT2] Downloading pre-converted: {repo} -> {out_dir}")
        snapshot_download(repo_id=repo, local_dir=out_dir)
        print(f"[CT2] Done: opus-mt-{pair}")
        continue
    except (RepositoryNotFoundError, Exception):
        print(f"[CT2] mijuanlo pre-converted not available for {pair}, falling back to conversion.")

    # === Fallback: download from Helsinki-NLP and convert ===
    model_id = f"Helsinki-NLP/opus-mt-{pair}"
    os.makedirs(out_dir, exist_ok=True)

    print(f"[CT2] Converting {model_id} ...")
    subprocess.run([
        "ct2-transformers-converter",
        "--model", model_id,
        "--output_dir", out_dir,
        "--quantization", "int8",
        "--force",
    ], check=True)

    temp_dir = tempfile.mkdtemp(prefix=f"marian-{pair}-")
    try:
        snapshot_download(repo_id=model_id, local_dir=temp_dir)

        for fname in TOKENIZER_FILES:
            src = os.path.join(temp_dir, fname)
            if not os.path.exists(src):
                continue
            dst = os.path.join(out_dir, fname)
            if os.path.exists(dst):
                if fname.endswith(".json"):
                    with open(src, encoding="utf-8") as f_src, \
                         open(dst, encoding="utf-8") as f_dst:
                        src_data = json.load(f_src)
                        dst_data = json.load(f_dst)
                    merged = {**src_data, **dst_data}
                    with open(dst, "w", encoding="utf-8") as f_out:
                        json.dump(merged, f_out, indent=2, ensure_ascii=False)
                    print(f"[CT2]   Merged {fname}")
                else:
                    print(f"[CT2]   Skipped {fname} (CT2 already has it)")
            else:
                shutil.copy2(src, dst)
                print(f"[CT2]   Copied {fname}")

        hf_config = os.path.join(temp_dir, "config.json")
        ct2_config = os.path.join(out_dir, "config.json")
        if os.path.exists(hf_config) and os.path.exists(ct2_config):
            with open(hf_config, encoding="utf-8") as f_src, \
                 open(ct2_config, encoding="utf-8") as f_dst:
                hf_data = json.load(f_src)
                ct2_data = json.load(f_dst)
            merged = {**hf_data, **ct2_data}
            with open(ct2_config, "w", encoding="utf-8") as f_out:
                json.dump(merged, f_out, indent=2, ensure_ascii=False)
            print(f"[CT2]   Merged config.json")
    finally:
        shutil.rmtree(temp_dir, ignore_errors=True)

print("[CT2] All Marian models ready.")
PY
fi

# Marcador del modo efectivo (ct2 => .use_ct2, marian => .use_marian)
set_mode_marker

echo ""
echo "[setup] $(_ "Ready.")"
echo ""
echo "$(_ "Offline execution (the launcher already exports it):")"
echo "  HF_HUB_OFFLINE=1"
echo "  TRANSFORMERS_OFFLINE=1"
