#!/bin/sh
#
# displays a translated message in plymouth screen images
#
# Usage:
#  $ ./plymouth-l10n
#
# Requires: ImageMagick + various fonts
#
# Author: JM. Philippe <jean-michel.philippe@doudoulinux.org>
#################################
# This file is part of DoudouLinux.
#
# DoudouLinux is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# DoudouLinux is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DoudouLinux.  If not, see <http://www.gnu.org/licenses/>.

##################
# constants
# NB: messages to be translated are below, look at get_pomsg()
TEXTSCRIPT=plymouth-text
PLYMOUTHTEMPL=template.script
PLYMOUTHSCRIPT=plymouth.script
TEXTDOMAIN=doudoulinux-boot
PODIR=../po
PREBUILDDIR=../prebuilt
LANGTABLE=../../../../lang/trunk/langcodes.table

##################
# initialisations
SCRIPTDIR=$(dirname $0)
PODIR=$SCRIPTDIR/$PODIR
if [ -d $PODIR ]; then
	LOCALEDIR=$SCRIPTDIR/.locale
	PREBUILDDIR=$SCRIPTDIR/$PREBUILDDIR
else
	LOCALEDIR=
	PREBUILDDIR=/usr/lib/plymouth-themes-doudoulinux/prebuilt
fi
PLYMOUTHDIR=$SCRIPTDIR/../data		# try the local directory
if ! [ -d "$PLYMOUTHDIR" ]; then	# or fallback to system wide directory
	PLYMOUTHDIR=./usr/share/plymouth/themes
fi

##################
# help/error messages
helpmsg() {
	cat <<EOF
Translates plymouth boot and shutdown images. Note that Live tools cannot
use gettext since locales are not configured in the chroot environment.
For this reason we need to compile prebuilt scripts with translated messages.

Usage:
  plymouth-l10n --prepare
  plymouth-l10n [--no-gettext]

  --prepare       Prebuild shell scripts from PO files
  --no-gettext    Use prebuilt shell scripts instead of PO files
EOF
	exit
}
errormsg() {
	cat <<EOF
ERROR!

helpmsg
EOF
}

##################
# get input args
PREPARE=0
NOGETTEXT=0
while [ $# -ge 1 ]; do
	case "$1" in
		"--prepare")
			if ( [ $NOGETTEXT -ne 0 ] || [ $ADDCDMSG -ne 0 ] ); then
				errormsg
			fi
			PREPARE=1
			;;
		"--no-gettext")
			if [ $PREPARE -ne 0 ]; then
				errormsg
			fi
			NOGETTEXT=1
			;;
		"-h"|"--help")
			helpmsg
			;;
		*)
			errormsg
			;;
	esac
	shift 1
done

##################
# recompile gettext messages
if [ -d $PODIR ]; then
	# change default MO files directory
	export TEXTDOMAINDIR=$LOCALEDIR
	
	# i18n: generate MO files if necessary
	echo "INFO: regenerate POT file with:"
	echo "  $ xgettext --from-code utf-8 -ki18n -L Shell -o $SCRIPTDIR/../$TEXTDOMAIN.pot $0"
	OUTPUTFILE=$TEXTDOMAIN.mo
	for FILENAME in $PODIR/*.po; do
		LANGCODE=$(basename ${FILENAME%%.po})
		OUTPUTDIR=$LOCALEDIR/$LANGCODE/LC_MESSAGES
		if ! [ -d $OUTPUTDIR ]; then
			mkdir -p $OUTPUTDIR
		fi
		msgfmt -o $OUTPUTDIR/$OUTPUTFILE $FILENAME
		echo "  compiled '$OUTPUTDIR/$OUTPUTFILE'"
	done
fi

##################
# gettext translations
i18n() {
	gettext $TEXTDOMAIN "$1"
}

get_pomsg() {
	STARTMSG=$(i18n "Hello :-)")
	STOPMSG=$(i18n "Bye bye…")
	STARTCOMMENT=$(i18n "I'm almost ready!")
	STOPCOMMENT=$(i18n "See you soon!")
}

get_lang() {
	# returns the LANG variable for a 2 letters language code
	#
	# get_lang LANGCODE
	LANGCODE=$1
	
	if echo $LANGCODE | grep '_' >/dev/null; then
	# case of pt_BR, zh_CN, etc.
		echo $LANGCODE.UTF-8
	elif echo $LANGCODE | grep '@' >/dev/null; then
	# case of sr@latin
		case $1 in
		"sr@latin")
			echo 'sr_RS.UTF-8@latin'
			;;
		esac
	else
	# case of 2 letters
		if ! grep "^$LANGCODE" $LANGTABLE; then
			echo $LANGCODE\_$(echo $LANGCODE | tr "[:lower:]" "[:upper:]").UTF-8
		fi
	fi
}

##################
# creation of prebuilt translations
write_prebuilt() {
	# write_prebuilt FILENAME
	get_pomsg
	cat >$1 <<EOF
# this file is autogenerated, do not change
# use “plymouth-l10n --prepare“ instead

STARTMSG="$STARTMSG"
STOPMSG="$STOPMSG"
STARTCOMMENT="$STARTCOMMENT"
STOPCOMMENT="$STOPCOMMENT"
EOF
	echo "  updated '$1' for $LANG"
}

make_prebuilt() {
	# make_prebuilt
	if ! [ -d $PREBUILDDIR ]; then
		mkdir -p $PREBUILDDIR
	fi
	for FILENAME in $PODIR/*.po; do
		LANGCODE=$(basename ${FILENAME%%.po})
		LANG=$(get_lang $LANGCODE)
		write_prebuilt $PREBUILDDIR/$LANGCODE.sh
	done
	LANG=en_US.UTF-8
	write_prebuilt $PREBUILDDIR/en.sh
}

##################
# get prebuilt translations
get_prebuilt() {
	# prebuiltfile
	LANGCODE=$(echo $LANG | grep -o '^[a-z]*')
	
	FILENAME=$PREBUILDDIR/$LANGCODE.sh
	if ! [ -f $FILENAME ]; then
		FILENAME=$(ls --format single-column $PREBUILDDIR/$LANGCODE*.sh 2>/dev/null | head -n 1)
	fi
	if ! [ -f "$FILENAME" ]; then
		echo $PREBUILDDIR/en.sh
	else
		echo $FILENAME
	fi
}

##################
# recompile shell scripts and exit
if [ $PREPARE -eq 1 ]; then
	make_prebuilt
	exit
fi

##################
# get messages
if [ $NOGETTEXT -eq 0 ]; then
	get_pomsg
else
	. $(get_prebuilt)
fi

##################
# insert splash image texts
$SCRIPTDIR/$TEXTSCRIPT start "$STARTMSG"
$SCRIPTDIR/$TEXTSCRIPT stop "$STOPMSG"

##################
# translate plymouth script texts
for IMGDIR in $(ls -d $PLYMOUTHDIR/*); do
	sed "s/STARTCOMMENT/$STARTCOMMENT/" $IMGDIR/$PLYMOUTHTEMPL > $IMGDIR/$PLYMOUTHSCRIPT
	sed -i "s/STOPCOMMENT/$STOPCOMMENT/" $IMGDIR/$PLYMOUTHSCRIPT
done
