#!/bin/bash

# top level directories:

# get package name from changelog - or set otherwise
CHNGLOG=$(find {,../,../../}debian/changelog  -type f -name changelog -print -quit 2>/dev/null)
PKGNAME=$(dpkg-parsechangelog -l "$CHNGLOG" -SSource)

# set pot-file name
POT=$PKGNAME.pot

[ -f $POT ] && mv $POT $POT~
touch $POT

# no-location: don't generate new POT if only location has changed
# translator comments in scripts: only thos with TRANSLATOR tag
#
# msgid-bugs-address
#SENDBUGS="translations@mxlinux.org"
SENDBUGS="https://forum.mxlinux.org/viewforum.php?f=96"

# script in bin-dir
BIN_DIR=../exec
POTFILES=(
        $BIN_DIR/$PKGNAME
        )
xgettext --package-name=$PKGNAME --language Shell --join-existing \
          --msgid-bugs-address="$SENDBUGS" \
          --no-location --no-wrap --add-comments=TRANSLATORS  \
          --output=$POT "${POTFILES[@]}"

# desktop file(s) in toplevel data-dir
DATA_DIR=../data
POTFILES=(
        $DATA_DIR/$PKGNAME.desktop
        )
xgettext --package-name=$PKGNAME --language=Desktop --join-existing \
          --msgid-bugs-address="$SENDBUGS" \
          --no-location --no-wrap --add-comments  \
          -k --keyword=Name --keyword=GenericName \
          --keyword=Comment  --keyword=Keywords \
          --output=$POT "${POTFILES[@]}"


BIN_DIR=../exec
POTFILES=(
        $BIN_DIR/uip-about.py
        )
xgettext --package-name=$PKGNAME --language Python --join-existing \
          --msgid-bugs-address="$SENDBUGS" \
          --no-location --no-wrap --add-comments=TRANSLATORS  \
          --keyword=_  --output=$POT "${POTFILES[@]}"

BIN_DIR=../exec
POTFILES=(
        $BIN_DIR/uip-about.py
        )


DATA_DIR=../data
POTFILES=(
        $DATA_DIR/org.mxlinux.user-installed-packages.policy
        )
ITS=/usr/share/gettext/its/polkit.its
xgettext --package-name=$PKGNAME --its=$ITS --join-existing  \
          --msgid-bugs-address="$SENDBUGS" \
          --no-location --no-wrap --add-comments  \
          --output=$POT "${POTFILES[@]}"


sed -i 's/charset=CHARSET/charset=UTF-8/' $POT

if [ -f $POT~ ]; then
    POT_CHANGE=$(diff ${POT} ${POT}~ | grep -vE '^([0-9]|-)|POT-Creation-Date:' | wc -l)
    if (( POT_CHANGE == 0 )); then
       echo "No change in POT-file: $POT"
       echo "Keeping existing POT-file"
       mv $POT~ $POT
    else
       echo "New POT-file generated:  $POT"
       echo "Old POT-file: $POT~"
       #rm $POT~
    fi
fi

unset PKGHELP

source ./pot-gettext

[ -n "$PKGHELP" ] || exit
POT=${PKGHELP}.pot
[ -f $POT ] && mv $POT $POT~
touch $POT

xgettext --package-name=${PKGHELP} --verbose --join-existing \
         --language Shell  --msgid-bugs-address="$SENDBUGS" \
          --no-location --no-wrap \
          --add-comments=TRANSLATOR  \
          --output=$POT - <<<$GETTEXT

if [ -n "$METATITLE" ]; then
    MSGID_METATITLE='msgid "'"$METATITLE"'"'
    if ! grep -q "$MSGID_METATITLE" "$POT"; then
        printf '\nmsgid "%s"\nmsgstr ""\n\n' "$METATITLE" >> "$POT"
    fi
fi

sed -i 's/charset=CHARSET/charset=UTF-8/' $POT

if [ -f $POT~ ]; then
    POT_CHANGE=$(diff $POT $POT~ | grep -vE '^([0-9]|-)|POT-Creation-Date:' | wc -l)
    if (( POT_CHANGE == 0 )); then
       echo "No change in POT-file: $POT"
       echo "Keeping existing POT-file"
       mv $POT~ $POT
    else
       echo "New POT-file generated:  $POT"
       echo "Old POT-file: $POT~"
       #rm $POT~
    fi
fi
[ -n "$TEMPMO" ] && [ -d "$TEMPMO" ] && rm -r "$TEMPMO"
