#!/usr/bin/env bash
. "$(dirname "${BASH_SOURCE[0]}")/lib/harness.sh"
set -eo pipefail

SECTION_JS="$ROOT_DIR/luci-app-tachyon/htdocs/luci-static/resources/view/tachyon/section.js"
SOURCE_PO="$ROOT_DIR/fe-app-tachyon/locales/tachyon.ru.po"
PACKAGE_PO="$ROOT_DIR/luci-app-tachyon/po/ru/tachyon.po"
SOURCE_POT="$ROOT_DIR/fe-app-tachyon/locales/tachyon.pot"
PACKAGE_POT="$ROOT_DIR/luci-app-tachyon/po/templates/tachyon.pot"

if grep -Fq '_("Dismiss")' "$SECTION_JS"; then
  fail "Tachyon modals must use Close instead of the shared LuCI Dismiss key"
fi
grep -Fq '_("Close")' "$SECTION_JS" ||
  fail "Tachyon section settings modal must expose a Close action"

grep -Fq 'socks4/5://, http/s://, hy2/hysteria2:// links' "$SECTION_JS" ||
  fail "Connection URL help must list the supported proxy links"

for po in "$SOURCE_PO" "$PACKAGE_PO"; do
  awk '
    $0 == "msgid \"Close\"" {
      getline
      if ($0 == "msgstr \"Закрыть\"")
        found = 1
    }
    END { exit found ? 0 : 1 }
  ' "$po" || fail "Close must be translated as Закрыть in $po"

  if awk '
    $0 == "msgid \"Dismiss\"" {
      getline
      if ($0 == "msgstr \"Отмена\"")
        bad = 1
    }
    END { exit bad ? 0 : 1 }
  ' "$po"; then
    fail "Dismiss must not override LuCI alert closing with Отмена in $po"
  fi

  awk '
    $0 == "msgid \"Cannot save settings\"" {
      getline
      if ($0 == "msgstr \"Не удалось сохранить настройки\"")
        found = 1
    }
    END { exit found ? 0 : 1 }
  ' "$po" || fail "Cannot save settings must have a Russian translation in $po"
done

cmp -s "$SOURCE_PO" "$PACKAGE_PO" ||
  fail "source and packaged Russian catalogs must stay synchronized"

printf 'LuCI localization checks passed\n'
