#!/bin/sh
#
# This file is part of OpenMediaVault.
#
# @license   http://www.gnu.org/licenses/gpl.html GPL Version 3
# @author    Volker Theile <volker.theile@openmediavault.org>
# @copyright Copyright (c) 2009-2012 Volker Theile
#
# OpenMediaVault 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
# any later version.
#
# OpenMediaVault 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 OpenMediaVault. If not, see <http://www.gnu.org/licenses/>.

set -e

. /etc/default/openmediavault

OMV_I18N_JS_DICT_FILE=${OMV_I18N_JS_DICT_FILE:-"${OMV_DOCUMENTROOT_DIR}/js/omv/i18nDict.js"}

cat <<EOF > ${OMV_I18N_JS_DICT_FILE}
/** This file is auto-generated by OpenMediaVault. */
OMV.i18nDict = {};
EOF

for language in $(find ${OMV_I18N_LOCALE_DIR} -maxdepth 1 -type d ! -iname ".*" -printf "%P\n"); do
	# Skip empty directories.
	[ $(find ${OMV_I18N_LOCALE_DIR}/${language} -iname \*.po | wc -l) = 0 ] && continue
	# Convert .po files into JSON.
	json=$(find ${OMV_I18N_LOCALE_DIR}/${language} -iname \*.po -type f -print0 | xargs -0 omv-po2json)
	cat <<EOF >> ${OMV_I18N_JS_DICT_FILE}
OMV.i18nDict["${language}"] = ${json}
EOF
done

cat <<EOF >> ${OMV_I18N_JS_DICT_FILE}
OMV.i18n.loadDictionary(OMV.i18nDict);
EOF

# Minify javascript code.
tmpdict=$(tempfile)
omv-jsminify --in=${OMV_I18N_JS_DICT_FILE} --out=${tmpdict}
cp ${tmpdict} ${OMV_I18N_JS_DICT_FILE} # Copy, to keep file permissions
rm -f ${tmpdict}

exit 0
