#!/bin/bash

cmd=$1

function i18list() {
	grep -lri --exclude-dir=node_modules --exclude-dir=.git 'i18next.t(' . \
		| grep -P '\.js' \
		| xargs cat \
		| grep 'i18next.t(' \
		| perl -p -e 's/i18next\.t/\ni18next.t/g' \
		| grep 'i18next.t(' \
		| perl -p -e "s/^i18next.t\('(.*?)(?<!\\\\)'.*$/\1/g" \
		| perl -p -e 's/^i18next.t\("(.*?)(?<!\\)".*$/\1/g' \
		| perl -p -e "s/\\\\'/'/g" \
		| perl -p -e 's/\\"/"/g'

	grep -lri --exclude-dir=node_modules --exclude-dir=.git '{{t ' . \
		| grep -P '\.hbs' \
		| xargs cat \
		| grep '{{t ' \
		| perl -p -e 's/\{\{t\s+/\n{{t /g' \
		| grep '{{t ' \
		| perl -p -e "s/^\{\{t '(.*?)(?<!\\\\)'.*$/\1/g" \
		| perl -p -e 's/^\{\{t "(.*?)(?<!\\)".*$/\1/g' \
		| perl -p -e "s/\\\\'/'/g" \
		| perl -p -e 's/\\"/"/g'
}

function template() {
	content="${1}"
	lang="${2}"
	locale="${3}"
	if [ "${lang}" == "" ]; then
		lang="xx"
	fi
	if [ "${locale}" == "" ]; then
		locale="xx"
	fi
	echo "// Set your language/locale codes here."
	echo "//"
	echo "// Locale can be blank, in which case it will be used as the default for the"
	echo "// language."
	echo "//"
	echo "// if you don't know your language/local codes, fine them here:"
	echo "//   https://www.science.co.il/language/Locale-codes.php"
	echo "var language = '${lang}';"
	echo "var locale = '${locale}';"
	echo ""
	echo "// Do your translation here!"
	echo "var translation = {"
	echo "${1}"
	echo "};"
	echo "";
}

function update() {
	lang=$1
	app_keys="$(i18list | perl -p -e 's/"/\\"/g')"
	lang_code="$( cat locales/${lang}.js | grep 'var language =' | awk '{print $4}' | perl -p -e 's|[^a-z]||gi' )"
	locale_code="$( cat locales/${lang}.js | grep 'var locale =' | awk '{print $4}' | perl -p -e 's|[^a-z]||gi' )"
	old="$(cat locales/${lang}.js | grep -P '^\s')"
	IFS=$'\n'
	output="$(
		for x in ${app_keys}; do
			keep="$( echo "${old}" | grep -F '"'"${x}"'":' )"
			if [ "${keep}" != "" ]; then
				echo "${keep}" | perl -p -e 's/,?$/,/'
			else
				echo '	"'"${x}"'": "",'
			fi
		done
	)"
	template "$(echo "${output}" | sort | uniq)" "${lang_code}" "${locale_code}" > locales/${lang}.js
}

function full_regen() {
	for locale in `ls locales/*.js | grep '_' | sed 's|locales/||' | sed 's|\.js||'`; do
		echo "Updating locale ${locale}..."
		update ${locale};
	done
	echo "Updating locales/locale.js.template..."
	$0 template > locales/locale.js.template
}

function usage() {
	echo
	echo "--------------------------------------------------------------------------------"
	echo "$0 <cmd>"
	echo "  <cmd> can be one of:"
	echo
	echo "    template"
	echo "      Read all JS/templates and output a new locale template based off the"
	echo "      needed translations."
	echo "    update <lang>_<locale>"
	echo "      Read the file at locales/<lang>_<locale>.js, remove any unused translations"
	echo "      and add in any new (blank) translations needed, and output the entire"
	echo "      updated file. Use this to update a locale after the interface has changed."
	echo "      (NOTE: this overwrites the locale file)"
	echo "      (NOTE: running this can take a minute)"
	echo "    full"
	echo "      Regenerate all locale files (including the template) with the most up to"
	echo "      date locale keys. Essentially this is the same as calling the update"
	echo "      command on all locales. This will take a while."
	echo "    keys"
	echo "      List all current locale keys. Mainly used for debugging."
	echo
}

case $cmd in
	"template")
		prepped_keys="$(i18list | sort | uniq \
			| perl -p -e 's/"/\\"/g' \
			| perl -p -e 's/^/	"/g' \
			| perl -p -e 's/\n/": "",\n/g')"
		template "${prepped_keys}"
		;;
	"update")
		update $2
		;;
	"full")
		full_regen
		;;
	"keys")
		i18list
		;;
	*)
		usage
		;;
esac


