#!/bin/sh
. ./01_SetVariables.sh
PRGNAM=installer-translations
BUILD=$(date -u +%Y%m%d)
ARCH=noarch
TMP=/tmp/build-installer-translations
rm -rf $TMP
PKG=$TMP/package-$PRGNAM
OUTPUT="$ROOTDIR"/packages
rm -rf  $PKG
mkdir -p "$PKG"/wip

# Build the PO files
# We first need to format the PO files in such a way that msggrep will
# work. msggrep ignores references like:

# File: <source file name>, line: <line number>

# But expects instead:
#: <source file name>:<line number>
#
# shellcheck disable=SC1090
. "$ROOTDIR"/files-in-initrd/SeTlocales
cd "$WIP"/auto || exit 1
for i in *po; do
sleep 1
  sed "/^#/{s/# File: /#: /;s/, line:[ ]*/:/}" "$i" > "$PKG"/wip/"$i"
  msggrep -o "$PKG"/"${i%%.*}"."${DOMAINNAME}".po -N ./*auto "$PKG"/wip/"$i"
done
cd "$WIP"/rc.S || exit 1
for i in *po; do
  sed "/^#/{s/# File: /#: /;s/, line:[ ]*/:/}" "$i" > "$PKG"/wip/"$i"
  msggrep -o "$PKG"/"${i%%.*}"."${DOMAINNAME}".po -N ./*rc.S "$PKG"/wip/"$i"
done
cd "$WIP"/SeTkeymap || exit 1
for i in *SeTkeymap.po; do
  sed "/^#/{s/# File: /#: /;s/, line:[ ]*/:/}" "$i" > "$PKG"/wip/"$i"
  msggrep -o "$PKG"/"${j%%.*}"."${DOMAINNAME}".po -N ./*SeTkeymap "$PKG"/wip/"$i"
done
cd "$WIP"/slint-scripts || exit 1
for i in *slint-scripts.po; do
  sed "/^#/{s/# File: /#: /;s/, line:[ ]*/:/}" "$i" > "$PKG"/wip/"$i"
  msggrep -o "$PKG"/"${j%%.*}"."${DOMAINNAME}".po -N ./*slint-scripts "$PKG"/wip/"$i"
done
cd "$PKG"/wip || exit 1
# Compile the message catalogs
# We will use translated strings tagged fuzzy.
for k in auto rc.S SeTkeymap slint-scripts; do
	DOMAINNAME=$k
	for j in *"$k".po; do
		ll_TT="${j%%.*}"
		SeTLocaleDir
		# running SeTLocaleDir sets LocaleDir
		# shellcheck disable=SC2154
		if [ "$LocaleDir" = "missing" ]; then
			printf %b "I don't know where to install the message catalog for ${ll_TT}.\n"
			printf %b "Please request that the file $CWD/files-ib-initrd/SeTlocales be completed\n"
			exit
		fi
		MO_DIR="$PKG"/usr/share/locale/"$LocaleDir"/LC_MESSAGES
		mkdir -p "$MO_DIR"
		msgfmt --strict -c -v -f --statistics -o "$MO_DIR"/"${DOMAINNAME}".mo "$j" 2>&1|tee error
		if ! grep -q " translated messages" error; then
			translated=0
		else
			grep "^.*$j: " error > stat
			translated=$(sed "s@.*: @@;s@ .*@@" stat)
		fi
		if ! grep -q "untranslated" stat; then
			untranslated=0
		else
			untranslated=$(sed "s@.* translated message[s]\{0,1\}, @@;s@ .*@@" stat)
			total=$((translated+untranslated))
			percentage=$((translated*100/total))
			if [ $percentage -lt 85 ]; then
				rm "$MO_DIR"/"${DOMAINNAME}".mo
				echo "$j rejected, only ${percentage}% translated."
			else
				chown root:root "$MO_DIR"/"${DOMAINNAME}".mo
				chmod 644 "$MO_DIR"/"${DOMAINNAME}".mo
			fi
		fi
	done
done
cd "$CWD" || exit 1
rm -rf "$PKG"/wip
rm -f "$PKG"/*po
mkdir -p "$PKG"/install
cat << EOF > "$PKG"/install/slack-desc
                      |-----handy-ruler------------------------------------------------------|
installer-translations: installer-translations (translations of messages in Slint installer)
installer-translations:
installer-translations: This package contains translations of messages displayed by
installer-translations: configurations scripts included in the Slint installer, thanks to the
installer-translations: translators of the Slint project. Visit our website: http://slint.fr
installer-translations:
installer-translations:
installer-translations:
installer-translations:
installer-translations:
EOF
rm -f "$OUTPUT"/"$PRGNAM"*txz
( cd $PKG || exit 1
/sbin/makepkg -l y -c n "$OUTPUT"/"$PRGNAM"-"$SLINTVERSION"-"$ARCH"-"${BUILD}"slint.txz
)
