#!/usr/bin/env bash

set -x
set -e

BIN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

REPO_ROOT=$BIN_DIR/../..
cd $REPO_ROOT
swift run --package-path Scripts/translation-tool translation-tool download-resources
swift build --package-path Scripts/translation-validator

LOCALIZATION_ROOT=$REPO_ROOT/Signal/translations
cd $LOCALIZATION_ROOT

# Parse the PluralAware.stringsdict files to ensure they're not malformed.
lang_errors=()
for dir in *.lproj
do
  pushd "$dir"
  if [ -e PluralAware.stringsdict ]; then
    plutil PluralAware.stringsdict || lang_errors+=("$dir")
  fi
  popd
done
if [ "${#lang_errors[@]}" -gt 0 ]; then
  1>&2 echo "Some languages have malformed .stringsdict files: ${lang_errors[*]}"
  exit 1
fi

../../Scripts/translation-validator/.build/debug/translation-validator $LOCALIZATION_ROOT/en.lproj/Localizable.strings || true
../../Scripts/translation-validator/.build/debug/translation-validator $LOCALIZATION_ROOT/en.lproj/InfoPlist.strings || true

echo "Make sure you register any new localizations in XCode! (Go to Project > Signal > Localizations > Add Localizations)"
