#!/bin/sh
#
# inserts messages in plymouth screen images
#
# 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
BASEIMG=template.png
BASEIMG2=template-shutdown.png
STARTIMG=background.png
STOPIMG=shutdown.png
TITLEPOSFILE=title.pos
CONVERTOPTS="-encoding utf-8 -gravity Center"

# fonts
LATINFONT=/usr/share/fonts/truetype/ttf-larabie-straight/kleptocr.ttf
LATINSIZE=54
CHINESEFONT=/usr/share/fonts/truetype/wqy/wqy-zenhei.ttc
CHINESESIZE=54
BURMESEFONT=/usr/share/fonts/mm/AyarBonBon.ttf
BURMESESIZE=54
THAIFONT=/usr/share/fonts/truetype/thai/Loma.ttf
THAISIZE=54
FALLBACKFONT=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans.ttf
FALLBACKSIZE=44

TITLEOPTS="-weight bold"

#############
# test dependencies
if ! [ -f "$(which convert)" ]; then
	echo "ERROR: cannot find command 'convert' from ImageMagick package"
	exit
fi

#############
# guess path of images
SCRIPTDIR=$(dirname $0)
THEMEDIR=$SCRIPTDIR/../data		# try the local directory
if ! [ -d "$THEMEDIR" ]; then				# or fallback to system wide directory
	THEMEDIR=./usr/share/plymouth/themes
fi

#############
show_help() {
	# shows help message
	#
	# show_help
	cat <<EOF
Inserts text messages in plymouth start and shutdown splash images

Usage:
	$ ./plymouth-l10n start|stop TEXT
EOF
}

#############
insert_main() {
	# inserts a title text in the template image
	#
	# insert_main TEXT POSITION IMGDIR INPUTIMG OUTPUTIMG
	TEXT=$1
	POSITION=$2
	IMGDIR=$3
	INPUTIMG=$4
	OUTPUTIMG=$5
	
	echo TEXT=$TEXT
	case $(echo $LANG | grep -o '^[a-z]*') in
		"el"|"he"|"hi"|"ru"|"sr"|"uk")
			FONT=$FALLBACKFONT
			SIZE=$FALLBACKSIZE
			;;
		"my")
			FONT=$BURMESEFONT
			SIZE=$BURMESESIZE
			;;
		"th")
			FONT=$THAIFONT
			SIZE=$THAISIZE
			;;
		"zh")
			FONT=$CHINESEFONT
			SIZE=$CHINESESIZE
			;;
		*)
			FONT=$LATINFONT
			SIZE=$LATINSIZE
			;;
	esac
	convert "$IMGDIR/$INPUTIMG" $CONVERTOPTS $TITLEOPTS -font $FONT -fill "$COLOR" \
		-pointsize $SIZE -annotate $POSITION "$TEXT" "$IMGDIR/$OUTPUTIMG"
	echo "  processed '$IMGDIR/$INPUTIMG'"
}


# do the stuff
for IMGDIR in $(ls -d $THEMEDIR/*); do
	# source title.pos to get title position and color in image
	if [ -f $IMGDIR/$TITLEPOSFILE ]; then
		. $IMGDIR/$TITLEPOSFILE
	else
		echo "ERROR: connot find file '$IMGDIR/$TITLEPOSFILE'"
		continue
	fi
	
	# process images
	case "$1" in
		"start")
			insert_main "$2" $TITLEPOS $IMGDIR $BASEIMG $STARTIMG
			;;
		"stop")
			if [ -f "$IMGDIR/$BASEIMG2" ]; then
				insert_main "$2" $TITLEPOS2 $IMGDIR $BASEIMG2 $STOPIMG
			else
				insert_main "$2" $TITLEPOS2 $IMGDIR $BASEIMG $STOPIMG
			fi
			;;
		"--help"|"-h")
			show_help
			;;
		*)
			if [ -n "$1" ]; then
				echo "ERROR: unkown command '$1'"
			fi
			show_help
			;;
	esac
done
