#!/bin/sh
#
# SPDX-FileCopyrightText: 2024 SUSE LLC
#
# SPDX-License-Identifier: Apache-2.0

export LC_ALL=C.UTF-8
export LANG=C.UTF-8
for MODULE in mgrctl mgradm mgrpxy shared; do
    # Generate the pot file
    echo "Generate locale/${MODULE}/${MODULE}.pot"
    find ${MODULE} -type f -not -name '*_test.go' -name '*.go' | sort | xargs xgettext --no-wrap --keyword="PL:1c,2" --keyword="NL:1,2" --keyword="L" --language=Javascript --from-code=UTF-8 -o locale/${MODULE}/${MODULE}.pot -
    msguniq --no-wrap -o locale/${MODULE}/${MODULE}-uniq.pot locale/${MODULE}/${MODULE}.pot
    mv locale/${MODULE}/${MODULE}-uniq.pot locale/${MODULE}/${MODULE}.pot

    # Update the po files
    for PO in locale/${MODULE}/*.po; do
        echo -n "Update ${PO}"
        if msgmerge --previous --no-wrap --update ${PO} locale/${MODULE}/${MODULE}.pot;
        then
            if test -f ${PO}~; then
                rm ${PO}~
            fi
        else
            echo "msgmerge for ${PO} failed"
        fi
    done
done

# Commit the changes
for change in `git diff --numstat | awk '{print $1}'`; do
    if [ $change -gt 1 ]; then
        git add -u
        git commit -m "update strings for translations"
        exit
    fi
done
git reset --hard
