#!/usr/bin/env bash

set -e
cd -- "$(dirname -- "${BASH_SOURCE[0]}")"
APPNAME=formatusb

# Prepare transifex
if [ ! -s  .tx/config ]; then
   mkdir -p .tx
   cat <<EOF > .tx/config
[main]
host = https://www.transifex.com

[o:anticapitalista:p:antix-development:r:$APPNAME]
file_filter = ./${APPNAME}_<lang>.ts
source_file = ${APPNAME}_en.ts
source_lang = en
type        = ts
minimum_perc = 25
EOF
fi

# Migrate percent checking
if [ -z "$(grep minimum_perc .tx/config)" ]; then
        echo "minimum_perc = 25" >> .tx/config
fi

bash ../scripts/pull-catalogs "$APPNAME" . ts "${APPNAME}_en.ts"

# Find lupdate command in common locations
LUPDATE=""
for cmd in lupdate lupdate-qt6 /usr/lib/qt6/bin/lupdate /usr/bin/lupdate /usr/local/bin/lupdate; do
    if command -v "$cmd" >/dev/null 2>&1; then
        LUPDATE="$cmd"
        break
    fi
done

if [ -z "$LUPDATE" ]; then
    echo "Error: lupdate command not found. Please install Qt development tools."
    exit 1
fi

echo "Using lupdate: $LUPDATE"

# src.pro lists the application sources, the UI form and the English catalog.
cd ..
"$LUPDATE" src.pro
