#!/bin/bash
#  $Id: make_locale 33508 2016-01-14 16:23:50Z astarostin $
# -----------------------------------------------------------------------------
#  Name: Internationalization
#  Note: create .po file from all .php adn .js files, create json localisation files
#  Call xgettext and filter out some warnings (which is produced by
#  normal JavaScript source because JavaScript is not C)
#
# -----------------------------------------------------------------------------
#  Author: Andrey Starostin
#  Edited by:
#  QA by:
#  Copyright: videoNEXT LLC
# -----------------------------------------------------------------------------

if [ -n $value ]; then
	export APL=/opt/sarch
fi

function createLocale
{
	LANGUAGE=$1
	DOMAIN=$2

	# create .po

	LOCALE=$APL/conf/etc/locale
	BIN_DIR=$APL/imp/bin
	PO_DIR=$LOCALE/$LANGUAGE/LC_MESSAGES
	PO=$PO_DIR/$DOMAIN\.po
	PO_OUTPUT=$PO_DIR/$DOMAIN\_output.po
	PO_TEMP=$PO_DIR/$DOMAIN\_temp.po
	PO_DB_OUTPUT=$PO_DIR/$DOMAIN\_db_output.po
	PO_DB_TEMP=$PO_DIR/$DOMAIN\_db_temp.txt
	TMP_FILE=$PO_DIR/$DOMAIN.tmp

	mkdir -p $PO_DIR

	echo ""
	echo "Language $LANGUAGE"
	echo "Create $PO"
	echo ''> $PO_OUTPUT # xgettext needs that file, and we need it empty

	echo "Create PO from PHP"

	find $APL -type f -iname "*.php" | \
	xgettext --language=PHP \
		--from-code=utf-8 \
		--add-comments=i18n \
		--keyword=__:1 \
		--keyword=__:1,2c \
		--keyword=__n:1,2 \
		--keyword=__n:1,2,4c \
		--output=$PO_OUTPUT \
		--join-existing \
		--force-po \
		--no-wrap \
		--files-from=- \
		2>$TMP_FILE

	echo "Create PO from JS"

	find $APL -type f -iname "*.js" | \
	xgettext --language=C \
		--from-code=utf-8 \
		--add-comments=i18n \
		--keyword=__:1 \
		--keyword=__:1,2c \
		--keyword=__n:1,2 \
		--keyword=__n:1,2,4c \
		--output=$PO_OUTPUT \
		--join-existing \
		--force-po \
		--no-wrap \
		--files-from=- \
		2>>$TMP_FILE

	cat $TMP_FILE \
		| grep -v 'warning: file .* extension .* is unknown;' \
		| grep -v 'warning: unterminated character constant' \
		| grep -v 'warning: unterminated string literal' \
		| grep -v 'warning: Charset "CHARSET" is not a portable encoding name' \
		| grep -v "Message conversion to user's charset might not work"
	rm -f $TMP_FILE


	echo "Create PO from DB"

	$APL/db/bin/db_exec \
		-c "SELECT name from auditcategory WHERE name IS NOT NULL AND name !=''" \
		--quiet --tuples-only --no-align skm_local > $PO_DB_TEMP

	$APL/db/bin/db_exec \
		-c "SELECT digest from auditcategory WHERE digest IS NOT NULL AND name !=''" \
		--quiet --tuples-only --no-align skm_local >> $PO_DB_TEMP

	echo '#' > $PO_DB_OUTPUT
	echo 'msgid ""' >> $PO_DB_OUTPUT
	echo 'msgstr ""' >> $PO_DB_OUTPUT
	echo '"Project-Id-Version: PACKAGE VERSION\n"' >> $PO_DB_OUTPUT
	echo '"Report-Msgid-Bugs-To: \n"' >> $PO_DB_OUTPUT
	echo '"POT-Creation-Date: 2011-06-23 12:17+0000\n"' >> $PO_DB_OUTPUT
	echo '"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"' >> $PO_DB_OUTPUT
	echo '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"' >> $PO_DB_OUTPUT
	echo '"Language-Team: LANGUAGE <LL@li.org>\n"' >> $PO_DB_OUTPUT
	echo '"MIME-Version: 1.0\n"' >> $PO_DB_OUTPUT
	echo '"Content-Type: text/plain; charset=UTF-8\n"' >> $PO_DB_OUTPUT
	echo '"Content-Transfer-Encoding: 8bit\n"' >> $PO_DB_OUTPUT
	echo "" >> $PO_DB_OUTPUT

	sort $PO_DB_TEMP | uniq | sed 's/"/\\"/g' \
	| sed 's/^/\n#\: DB\:\:auditcategory \nmsgctxt "Audit"\nmsgid "/;s/$/"\nmsgstr ""\n/' >> $PO_DB_OUTPUT

	cat $PO_OUTPUT | \
			sed 's/charset=CHARSET/charset=utf-8/' | \
			sed 's/nplurals=INTEGER; plural=EXPRESSION;/nplurals=2; plural=(n != 1);/' > $PO_TEMP
	mv $PO_TEMP $PO_OUTPUT

	echo "Create .po file from templates"

	TEMPLATES_DIR=$APL/conf/etc
	TEMPLATE_PO=$LOCALE/$LANGUAGE/LC_MESSAGES/TEMPLATE_$DOMAIN\.po
	$APL/conf/bin/locale/cfg2po.pl $TEMPLATES_DIR > $TEMPLATE_PO

	echo "Combines several message catalogs"

	msgcat --no-wrap \
		--use-first \
		$PO_OUTPUT $TEMPLATE_PO $PO_DB_OUTPUT > $PO_TEMP
	mv $PO_TEMP $PO_OUTPUT
	rm $TEMPLATE_PO $PO_DB_OUTPUT $PO_DB_TEMP

	# Merge new PO with old PO
	if [ -f $PO ]; then
		echo "Merging:"
		msgmerge --no-wrap \
			--no-fuzzy-matching \
			$PO $PO_OUTPUT > $PO_TEMP
		mv $PO_TEMP $PO
		rm $PO_OUTPUT
	else
		mv $PO_OUTPUT $PO
	fi

	echo "Create MO"
	# create .mo
	MO=$LOCALE/$LANGUAGE/LC_MESSAGES/$DOMAIN\.mo
	echo "Created $MO, $PO"
	msgfmt $PO --output-file=$MO

	# create JSON
	echo "Create JSON"
		JSON=$APL/sdi/html/locale/$LANGUAGE\.json
		$APL/conf/bin/locale/po2json -p $PO > $JSON
}

# create admin.po
createLocale "en_US" "admin"
createLocale "ru_RU" "admin"
createLocale "es_ES" "admin"
createLocale "ar_AE" "admin"
createLocale "it_IT" "admin"
