#!/bin/bash
#
#Copyright 2012 - 2013 CurlyMo & mk01 <development@xbian.org>
#
#This file is part of XBian - XBMC on the Raspberry Pi.
#
#XBian 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.
#
#XBian 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 XBian. If not, see <http://www.gnu.org/licenses/>

#|------------------------------------|
#|          Global variables          |
#|------------------------------------|

# The arguments this module accepts
ARGUMENTS=(select update list);

#|------------------------------------|
#|          Include files             |
#|------------------------------------|

source $BASEPATH/modules/locales/functions
if [ $GUIMODE -eq 1 ]; then
	source $BASEPATH/modules/locales/dialogs
fi

#|------------------------------------|
#|           Main program             |
#|------------------------------------|

# Executes the GUI version of this module
function showGUIFn() {
	showAlphabetDlg;
	if [ $? -eq 0 ]; then
		LETTER=$(printf "\\$(printf "%03o" $(($RETURN+64)))");
		showLocalesDlg $LETTER;
		if [ $? -eq 0 ]; then
			getLanguageByIdFn $LETTER $RETURN;
			setLocaleFn ${LOCALE[0]};
			showSettingLocaleSuccessFn;
		fi
		showGUIFn;
	fi
}

# Executes the command line version of this module
#  $1 Argument [String]
#  $2 Letter | ISO code
# Returns
#  *Select [String]
#  - Current ISO locale
#  *List [String] - Tab seperated list
#  1) ISO code
#  2) English language name
#  3) XBian supported language
#  5) XBMC language name
#  *Update [Integer]
#  - 1: Success
#  - 0: Failed
function showCMDFn() {
	case $1 in
		select)
			if [ $# == 1 ]; then
				sed -ne 's/LANG=\(.*\)/\1/p' /etc/default/locale
			else
				echo -2;
			fi
		;;
		list)
			if [ $# == 2 ]; then
				getLanguagesByLetterFn $2;
				for VALUE in ${LANGUAGES[@]}; do
					IFS=$'\t';
					VALUES=($VALUE);
					TEMP=${VALUES[0]};
					ABBR=${TEMP%_*};
					if [ -f "/usr/share/locale/$ABBR/LC_MESSAGES/xbian.mo" ]; then
						XBLANG=1;
					else
						XBLANG=0;
					fi
					echo "${VALUES[0]},${VALUES[1]},$XBLANG,${VALUES[4]}";
				done;
			else
				echo -2;
			fi
		;;
		update)
			if [ $# == 2 ]; then
				if [ $(ISOLocaleExistsFn $2; echo $?) -eq 1 ]; then
					setLocaleFn $2;
					echo 1;
				else
					echo -3;
				fi
			else
				echo -2;
			fi
		;;
	esac
}
