#!/bin/bash

#I wrote this because autoconf is confusing as bloody hell.
#I just need to copy some files into a place where gettext will expect them
#I Shouldn't need to create some mega ultimate makefile.am for that, then
#follow some random poorly documented hierarchy and naming system. (Looking at you gettextize!)

#extract program name from sources -- Now with added case sensitivity hack!
PROGRAM_NAME=`cat ../src/common/basics.cpp | grep PROGRAM_NAME | awk -F= '{print $2}' | sed 's/;//g' | sed 's/\"//g' | sed 's/;//' | sed 's/^\s*//'`
#Where do we want to install the translations? (if using this script).
TRANSLATION_INSTALL="/usr/share/locale/"

#Space separated list of maintained locales
MAINTAINED_LOCALES="de_DE"

echo "Program name is $PROGRAM_NAME"



if [ $# -eq 0 ] ; then

	echo "Creating binary translation files.."
	#Create binary versions of gettext translation files
	for i in *.po
	do
		#Quick check to see if we have any "<span>" elements in the files
		if  [ x"`grep '<span' $i `" != x"" ] ; then
			echo "WARNING: Detected \"span\" element in translation file. This is probably an error, due to encoding of special characters by transifex"
		fi
		msgfmt -c -v -o `echo $i | sed 's/po$/mo/'` $i
	done
	echo "done"
	exit 0
fi



if [ $# -eq 1 ] ; then
	if [ $1  == "install" ] ; then
		for i in $MAINTAINED_LOCALES
		do

			if [ ! -f ${PROGRAM_NAME}_${i}.mo ] ; then
				echo "WARNING: ${PROGRAM_NAME}-$i.mo is missing!!!"
			else
				cp ${PROGRAM_NAME}_$i.mo $TRANSLATION_INSTALL/$i/LC_MESSAGES/$PROGRAM_NAME.mo
			fi
		done
	fi
	
	
	if [ $1  == "update" ] ; then
		xgettext --from-code=utf-8 -k_ -kTRANS -kwxTRANS -kNTRANS -kwxNTRANS `find ../src/ -name *.cpp` `find ../src/ -name *.h` ../data/startup-tips.txt -o update.pot

		echo "Merging with maintained locales"
		for i in $MAINTAINED_LOCALES
		do
			msgmerge -N ${PROGRAM_NAME}_${i}.po update.pot > tmp.pot
			mv tmp.pot ${PROGRAM_NAME}_${i}.po

		done
		
		echo "Replacing base template"
		msgmerge -N ${PROGRAM_NAME}_base.pot update.pot > tmp.pot
		mv tmp.pot ${PROGRAM_NAME}_base.pot
		rm update.pot
	fi
fi
