#!/bin/sh
# Version: 3

# TODO: Call a CMakeLists.txt ...
# OR just find out what env variables it looks at to determine the "/usr/share" dir.
# TODO: check if run as root, and if so, install to /usr/share instead of ~/.local/share

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
plasmoidName=`kreadconfig5 --file="$DIR/../metadata.desktop" --group="Desktop Entry" --key="X-KDE-PluginInfo-Name"`
website=`kreadconfig5 --file="$DIR/../metadata.desktop" --group="Desktop Entry" --key="X-KDE-PluginInfo-Website"`
bugAddress="$website"
packageRoot=".." # Root of translatable sources
projectName="plasma_applet_${plasmoidName}" # project name

#---
if [ -z "$plasmoidName" ]; then
	echo "[install] Error: Couldn't read plasmoidName."
	exit
fi

if [ -z "$(which msgfmt)" ]; then
	echo "[install] Error: msgfmt command not found. Need to install gettext"
	echo "[install] Running 'sudo apt install gettext'"
	sudo apt install gettext
	echo "[install] gettext installation should be finished. Going back to installing translations."
fi

#---
echo "[install] Installing messages"

catalogs=`find . -name '*.po'`
for cat in $catalogs; do
	echo "$cat"
	catLocale=`basename ${cat%.*}`
	msgfmt -o "${catLocale}.mo" "$cat"

	# installPath="/usr/share/locale/${catLocale}/LC_MESSAGES/${projectName}.mo"
	installPath="${HOME}/.local/share/locale/${catLocale}/LC_MESSAGES/${projectName}.mo"

	mkdir -p "$(dirname "$installPath")"

	echo "[install] Install to ${installPath}"
	
	# sudo mv "${catLocale}.mo" "${installPath}"
	# sudo chown root "${installPath}"
	mv "${catLocale}.mo" "${installPath}"
done

echo "[install] Done installing messages (will take effect after restarting plasmashell)"

echo "[install] Restarting plasmashell"
killall plasmashell
kstart5 plasmashell
echo "[install] Done restarting plasmashell"
