#!/usr/bin/env bash
set -euo pipefail
unset CDPATH
shopt -s nullglob
cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
root=$PWD

fail() { echo "$*" >&2; exit 1; }

(( $# <= 1 )) || fail "Usage: get_translations [--desktop]"
case "${1:-}" in
    '')
        directory=$root/translations
        english=("$directory/"*_en.ts)
        (( ${#english[@]} == 1 )) || fail "Expected one English source catalog in $directory"
        resource=${english[0]##*/}
        resource=${resource%_en.ts}
        source_file=${resource}_en.ts
        output=.
        extension=ts
        minimum=25
        lupdate=
        for tool in lupdate-qt6 /usr/lib/qt6/bin/lupdate lupdate6 lupdate; do
            if command -v "$tool" >/dev/null; then lupdate=$tool; break; fi
        done
        [[ -n $lupdate ]] || fail "Qt lupdate is required"
        sources=()
        for file in "$root/"*.{cpp,cc,cxx,h,hpp,ui,qml}; do
            case ${file##*/} in moc_*|qrc_*|ui_*.h) continue ;; esac
            sources+=("$file")
        done
        for dir in src qml; do
            [[ ! -d $root/$dir ]] || sources+=("$root/$dir")
        done
        (( ${#sources[@]} )) || fail "No application sources found in $root"
        ;;
    --desktop)
        directory=$root/translations-desktop-file
        templates=("$directory/pot/"*.pot)
        (( ${#templates[@]} == 1 )) || fail "Expected one desktop translation template in $directory/pot"
        resource=${templates[0]##*/}
        resource=${resource%.pot}
        source_file=pot/$resource.pot
        output=po
        extension=po
        minimum=''
        ;;
    *) fail "Usage: get_translations [--desktop]" ;;
esac

command -v tx >/dev/null || fail "The Transifex client (tx) is required"
exec 9<"$directory"
flock -x 9
# A sibling of translations/ preserves the source paths written by lupdate.
stage=$(mktemp -d "$root/.translation-refresh.XXXXXXXX")
trap 'rm -rf -- "$stage"' EXIT
cd -- "$stage"
mkdir -p .tx "$output" "$(dirname -- "$source_file")"
config=$directory/.tx/config
if [[ -s $config ]]; then
    # Older desktop configs point at a template in the working directory.
    sed -E "s|^([[:space:]]*source_file[[:space:]]*=).*|\\1 $source_file|; s|^([[:space:]]*file_filter[[:space:]]*=).*|\\1 $output/${resource}_<lang>.$extension|" "$config" > .tx/config
else
    cat > .tx/config <<CONFIG
[main]
host = https://www.transifex.com

[o:anticapitalista:p:antix-development:r:$resource]
file_filter = $output/${resource}_<lang>.$extension
source_file = $source_file
source_lang = en
type = $extension
CONFIG
fi
if [[ -n $minimum ]] && ! grep -Eq '^[[:space:]]*minimum_perc[[:space:]]*=' .tx/config; then
    echo "minimum_perc = $minimum" >> .tx/config
fi
cp -- "$directory/$source_file" "$source_file"
tx pull -r "antix-development.$resource" --all

if [[ $extension == ts ]]; then
    rm -f -- "$source_file" "${resource}_en_US.ts"
fi
downloads=("$output/${resource}_"*."$extension")
(( ${#downloads[@]} )) || fail "No translations downloaded for $resource"
for catalog in "${downloads[@]}"; do
    [[ -f $catalog && -s $catalog ]] || fail "Empty or invalid catalog: $catalog"
done
if [[ $extension == ts ]]; then
    python3 - "${downloads[@]}" <<'PYTHON'
import sys
import xml.etree.ElementTree as ET

for catalog in sys.argv[1:]:
    if ET.parse(catalog).getroot().tag != "TS":
        sys.exit(f"Invalid Qt translation catalog: {catalog}")
PYTHON
    cp -- "$directory/$source_file" "$source_file"
    "$lupdate" "${sources[@]}" -ts "$source_file"
else
    for catalog in "${downloads[@]}"; do msgfmt -o /dev/null "$catalog"; done
fi

# Install the complete download before pruning locales that disappeared upstream.
mkdir -p "$directory/$output"
obsolete=()
for file in "$directory/$output/${resource}_"*."$extension"; do
    [[ -f "$output/${file##*/}" ]] || obsolete+=("$file")
done
mv -f -- "$output/${resource}_"*."$extension" "$directory/$output/"
if (( ${#obsolete[@]} )); then rm -f -- "${obsolete[@]}"; fi
# Leave a usable config for tx commands run directly in the catalog directory.
if ! cmp -s .tx/config "$config"; then
    mkdir -p "$directory/.tx"
    mv -f -- .tx/config "$config"
fi
