#!/bin/sh
#
# makepot - generate a .pot file for gettext translations from the sources
#

ensure_installed()
{
    local command=$1
    local package=$2

    if [ -z `type -p $command` ]; then
        echo
        echo "FATAL: Missing command \"$command\" - install package \"$package\""
        echo
        exit 1
    fi
}


# Check if all required commands are available

#                command    package
ensure_installed lupdate6   qt6-tools-linguist
ensure_installed lconvert6  qt6-tools-linguist
ensure_installed msguniq    gettext-tools


# Constants for files and direcories

TS="qdirstat.ts"
POT_RAW="qdirstat-raw.pot"
POT="qdirstat.pot"
SRC_DIR=.


# Extract messages from the Qt Designer .ui files and C++ source files using
# Qt6 Linguist tools that understand that file format but convert immediately
# to gettext .pot format.

lupdate6  -silent $SRC_DIR/*.ui $SRC_DIR/*.cpp -ts $TS
lconvert6 $TS -o $POT_RAW

# Remove msgctxt lines and merge duplicate msgids
sed -i -e '/^msgctxt/d' $POT_RAW
msguniq -o $POT $POT_RAW


# Clean up

# Comment out to keep the temp files for debugging
rm -f $TS $POT_RAW


# Stats

echo
ls -l *.pot
echo
echo -n "Number of messages in $POT: "
grep -c 'msgid' $POT
echo

