#!/usr/bin/bash
# Manage which UI languages the Exaba Dashboard offers, by editing the
# `languages` array in languages.json (the runtime source of truth).
#
# A language can only be enabled if its translations actually ship in
# this build - i.e. assets/i18n/<code>.json exists. The canonical
# display name for each code comes from the LANG_CATALOG below, which is
# regenerated from assets/languages.full.json at RPM build time (see
# packaging/build_rpm.sh). The values are a snapshot from the last
# regeneration; running from the repo without a fresh build may show
# stale values if assets/languages.full.json has changed since.

set -e

# === BEGIN AUTO-GENERATED LANGUAGES ===
# "code|Display Name" pairs - the full set of languages shipped in this
# build. Regenerated from assets/languages.full.json at RPM build time.
LANG_CATALOG=("en_US|English (US)" "en_GB|English (UK)" "mi|Te Reo Māori" "zh_CN|中文" "hi|हिन्दी" "es|español" "fr|Français" "bn|বাংলা" "ru|Русский" "pt|Português" "id|Indonesia" "de|Deutsch" "ja|日本語" "sw|kiswahili" "sv|Svenska" "mr|मराठी" "te|తెలుగు" "ta|தமிழ்" "ko|한국인" "vi|Tiếng Việt")
# === END AUTO-GENERATED LANGUAGES ===

INSTALLED_LANGS=/usr/share/exaba/dashboard/assets/assets/languages.json
DEV_LANGS=./assets/languages.json

usage() {
  cat <<EOF
Usage: $(basename "$0") [-h] [--dev] [-e CODE | -d CODE]

Manage which UI languages the Exaba Dashboard offers (languages.json).

  -h, --help     Show this help.
  -e CODE        Enable language CODE (add it to languages.json).
  -d CODE        Disable language CODE (remove it from languages.json).
  --dev          Target the in-repo $DEV_LANGS instead of the
                 installed $INSTALLED_LANGS.

With no -e/-d options, prints the currently-enabled languages and the
other shippable languages that could be enabled.

A language can only be enabled if its translations ship in this build
(assets/i18n/<code>.json). Known languages:
$(for entry in "${LANG_CATALOG[@]}"; do printf '  %-8s %s\n' "${entry%%|*}" "${entry#*|}"; done)
EOF
}

DEV=0
ENABLE=""
DISABLE=""

while [ $# -gt 0 ]; do
  case "$1" in
    -h|--help) usage; exit 0 ;;
    --dev) DEV=1 ;;
    -e)
      shift
      [ -z "${1:-}" ] && { echo "Error: -e requires a language code" >&2; exit 1; }
      ENABLE="$1"
      ;;
    -d)
      shift
      [ -z "${1:-}" ] && { echo "Error: -d requires a language code" >&2; exit 1; }
      DISABLE="$1"
      ;;
    *) echo "Error: unknown argument: $1" >&2; usage >&2; exit 1 ;;
  esac
  shift
done

if [ -n "$ENABLE" ] && [ -n "$DISABLE" ]; then
  echo "Error: -e and -d are mutually exclusive" >&2
  exit 1
fi

if [ "$DEV" -eq 1 ]; then
  LANGS="$DEV_LANGS"
  if [ ! -f "$LANGS" ]; then
    echo "Error: $LANGS not found - run from the exaba_dashboard repo root" >&2
    exit 1
  fi
else
  LANGS="$INSTALLED_LANGS"
  if [ ! -f "$LANGS" ]; then
    echo "Error: $LANGS not found - exaba-dashboard is not installed as expected" >&2
    exit 1
  fi
fi

# Translations live alongside languages.json, under i18n/.
I18N_DIR="$(dirname "$LANGS")/i18n"

# Display name for a catalog code (empty + non-zero if unknown).
name_for() {
  local code="$1" entry
  for entry in "${LANG_CATALOG[@]}"; do
    if [ "${entry%%|*}" = "$code" ]; then
      echo "${entry#*|}"
      return 0
    fi
  done
  return 1
}

is_enabled() {
  local code="$1"
  jq -e --arg code "$code" \
    'any(.languages[]?; .code == $code)' "$LANGS" >/dev/null
}

enable_lang() {
  local code="$1" name
  if ! name=$(name_for "$code"); then
    echo "Error: unknown language '$code'" >&2
    echo "Known languages: $(for e in "${LANG_CATALOG[@]}"; do printf '%s ' "${e%%|*}"; done)" >&2
    exit 1
  fi
  if [ ! -f "$I18N_DIR/$code.json" ]; then
    echo "Error: language '$code' has no translations in this build ($I18N_DIR/$code.json missing)" >&2
    exit 1
  fi
  if is_enabled "$code"; then
    echo "Language '$code' is already enabled."
    return 0
  fi
  local tmp
  tmp=$(mktemp)
  jq --arg code "$code" --arg name "$name" \
     '.languages += [{"code": $code, "name": $name}]' \
     "$LANGS" > "$tmp"
  mv "$tmp" "$LANGS"
}

disable_lang() {
  local code="$1"
  if ! is_enabled "$code"; then
    echo "Language '$code' is not currently enabled."
    return 0
  fi
  local tmp
  tmp=$(mktemp)
  jq --arg code "$code" \
     '.languages |= map(select(.code != $code))' \
     "$LANGS" > "$tmp"
  mv "$tmp" "$LANGS"
}

[ -n "$ENABLE" ]  && enable_lang  "$ENABLE"
[ -n "$DISABLE" ] && disable_lang "$DISABLE"

echo "Languages in $LANGS:"
for entry in "${LANG_CATALOG[@]}"; do
  code="${entry%%|*}"
  name="${entry#*|}"
  if is_enabled "$code"; then
    state=enabled
  elif [ -f "$I18N_DIR/$code.json" ]; then
    state=disabled
  else
    state="disabled (no translations)"
  fi
  printf '  %-8s %-16s %s\n' "$code" "$name" "$state"
done
