#!/bin/bash
set -euo pipefail

PROGNAME=$(basename $0)

cd $(dirname $0)/..

TOOLS_DIR=$PWD/tools
PO_DIR=$PWD/android/assets/po
MESSAGES_POT=$PO_DIR/messages.pot

TEMP_DIR="$(mktemp -d --tmpdir "po-update-XXXXXX")"

delete_temp_dir() {
    rm -rf $TEMP_DIR
}

trap delete_temp_dir INT TERM EXIT

die() {
    echo "$PROGNAME: $*" >&2
    exit 1
}

usage() {
    if [ "$*" != "" ] ; then
        echo "Error: $*"
        echo
    fi

    cat << EOF
Usage: $PROGNAME [OPTION ...]
Updates $MESSAGES_POT from strings in the source code.

Options:
  -h, --help          display this usage message and exit
  -c, --check         check if $MESSAGES_POT is up-to-date, do not update it
  --force             update $MESSAGES_POT even if it looks up-to-date
EOF

    exit 1
}

CHECK_MODE=false
FORCE=false

parse_args() {
    while [ $# -gt 0 ] ; do
        case "$1" in
        -h|--help)
            usage
            ;;
        -c|--check)
            CHECK_MODE=true
            ;;
        --force)
            FORCE=true
            ;;
        -*)
            usage "Unknown option '$1'"
            ;;
        *)
            usage "Too many arguments"
            ;;
        esac
        shift
    done
}

list_files() {
    local output=$1

    echo "Listing translatable files"

    for dir in core desktop android ; do
        find $dir -name '*.java' | grep -v '/build/' >> $output
    done
    find android/assets -name '*.gdxui' >> $output
    find android/assets/championships -name '*.xml' >> $output
    find android/assets/vehicles -name '*.xml' >> $output
}

extract_messages() {
    local lst_file=$1
    local messages_pot=$2

    echo "Extracting messages"

    # Set GETTEXTDATADIR so that xgettext finds our .its files
    GETTEXTDATADIR=$TOOLS_DIR/gettext xgettext \
        --from-code=utf-8 \
        --keyword=tr --keyword=trn:1,2 --keyword=trc:1,2c \
        --sort-by-file \
        --output - \
        --files-from $lst_file \
    | sed '/^ *"POT-Creation-Date:/d' > $messages_pot

    # The sed command removes the "POT-Creation-Date:" line, which adds useless
    # noise to the diffs
}

update_messages() {
    for po_file in $PO_DIR/*.po ; do
        echo "Updating $po_file"
        msgmerge \
            --update $po_file $MESSAGES_POT
    done
}

# When we diff .pot files, we do not care about file locations or metadata,
# only msgid and msgstr lines, so keep only those.
filter_pot() {
    local file=$1
    egrep '^msg(id|str) ' $file
}

main() {
    parse_args $@

    lst_file=$TEMP_DIR/files
    list_files $lst_file

    tmp_new_pot=$TEMP_DIR/new-pot
    extract_messages $lst_file $tmp_new_pot
    if ! $FORCE && cmp --quiet <(filter_pot $MESSAGES_POT) <(filter_pot $tmp_new_pot) ; then
        echo "$MESSAGES_POT is up-to-date"
        exit 0
    fi

    if $CHECK_MODE ; then
        echo "$MESSAGES_POT is not up-to-date, run \`make po-update\` to update it"
        exit 1
    else
        mv $tmp_new_pot $MESSAGES_POT
        update_messages
    fi
}

main $@
