#!/bin/bash

update_translations () {
	potfile=$1
	lang=$2
	files=$3
	xgettext --no-location --keyword=_ --keyword=_W --keyword=_TT -j -L $lang --keyword=dgettext:2 --keyword=ngettext:1,2 --keyword=dngettext:2,3 --keyword=pgettext:1c,2 --keyword=dpgettext:2c,3 --keyword=npgettext:1c,2,3 --keyword=dnpgettext:2c,3,4 --add-comments=TRANSLATORS --from-code utf-8 --files-from $files -o $potfile
}

create_potfile() {
	dirs=$1

	# Find JavaScript translations
	find ${dirs} -type f -name '*.js' ! -path "./deploy/*" -printf '%p\n' | sort > /tmp/JSFILES
	update_translations $POT_FILE JavaScript /tmp/JSFILES

	# Find PHP translations
	find . -type f -name '*.php' ! -path "./test/*" ! -path "./deploy/*" -printf '%P\n'   | sort > /tmp/PHPFILES
	update_translations $POT_FILE php /tmp/PHPFILES
}

function update_pos() {
	# Merge PO files and remove obsolete strings
	for lang in server/language/*.UTF-8/LC_MESSAGES/grommunio_web.po; do
		echo $lang
		msgmerge -N -q $lang $POT_FILE | msgattrib --no-obsolete -o $lang.new && mv $lang.new $lang
	done
}

function new_lang() {
	lang=$1
	pluginname=$2

	# Create the server/language/${lang}/LC_MESSAGES/ directory
	langdir="server/language/${lang}.UTF-8"
	poname='grommunio_web.po'
	if [ -n "${pluginname}" ]; then
		langdir=${langdir//server\/}
		poname="plugin_${pluginname}.po"
	fi

	mkdir -p "${langdir}/LC_MESSAGES"
	touch "${langdir}/language.txt"

	# Create po file
	pofile="${langdir}/LC_MESSAGES/${poname}"
	touch "${pofile}"
	msgmerge -N -q "${pofile}" "${POT_FILE}" > "${pofile}"
}

function usage {
	cat <<EOM
Usage: $(basename "$0") [OPTION]...

	-l lang     new language to be created
	-u          update a po files
	-h          display help
EOM
	exit 2
}

if [[ $1 == "" ]]; then
	usage
	exit 1
fi

while getopts ":h:l:p:u" arg; do
	case $arg in
	l)
		declare -r newlang=${OPTARG}
		;;
	u)
		declare -r update="1"
		;;
	h|*)
		usage
		exit 0
		;;
	esac
done

POT_FILE=webapp.pot
# Clean up pot file if script aborted.
[ -e "$POT_FILE" ] && rm $POT_FILE

# Otherwise xgettext complains
touch $POT_FILE

# Web case
if [ -d ".git" ]; then
	create_potfile "client/zarafa client/extjs-mod plugins"

	if [ -n "${newlang}" ]; then
		new_lang "${newlang}"
	fi

	if [ -n "${update}" ]; then
		update_pos
	fi
else # Plugin case
	if [ ! -f manifest.xml ]; then
		echo "Not in plugin directory"
		exit 1
	fi

	create_potfile "js"
	if [ -n "${newlang}" ]; then
		new_lang "${newlang}" $(basename $(pwd))
	fi

	if [ -n "${update}" ]; then
		echo 'Upating language is not supported for plugins'
	fi
fi

# TODO(jelle): use a trap
rm $POT_FILE
