#!/bin/bash
#
#Copyright 2012 CurlyMo <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/>

# Get current timezone
# Returns
#  @ Array
#  1 Timezone
#  2 Location
function getCurrentTZFn() {
	if [ -L /etc/localtime ]; then
		IFS="\/"
		CURRENTTZ=($(ls -Al /etc/localtime | awk '{print $11}'));
		CURRENTTZ=("${CURRENTTZ[@]:4:2}");
		IFS=$ORIGINALIFS;
	elif [ -f /etc/localtime ]; then
		TZDATA=$(find /usr/share/zoneinfo/ -type f | xargs md5sum);
		IFS="\/"
		CURRENTTZ=($(echo -e "$TZDATA" | grep $(md5sum /etc/localtime | awk '{print $1}') | awk '{print $2}'));
		CURRENTTZ=("${CURRENTTZ[@]:5:2}");
		IFS=$ORIGINALIFS;
	fi
}

# Check if argument is a valid timezone
#  $1 [String] Timezone
# Returns
#  * [Integer]
#  - 1: Success
#  - 0: Failed
function checkTZValid() {
	TZPATH=$(find /usr/share/zoneinfo/ -maxdepth 1 -iname $1);
	if [ -z $TZPATH ]; then
		return 0;
	else
		return 1;
	fi
}

# Check if argument is a valid location
#  $1 [String] Timezone
#  $2 [String] Location
# Returns
#  * [Integer]
#  - 1: Success
#  - 0: Failed
function checkTZLocationValid() {
	TZPATH=$(find /usr/share/zoneinfo/ -maxdepth 1 -iname $1);
	if [ -d $TZPATH ]; then
		TZLOCATIONPATH=$(find $TZPATH -maxdepth 1 -iname $2);
		if [ -z $TZLOCATIONPATH ]; then
			return 0;
		else
			return 1;
		fi
	else
		return 0;
	fi
}

# Retrieves the timezone locations list
# Array 
#  1 [String] Timezone
#  2 [String] Location
# Returns
#  @ Array Locations
function getTZLocationLstFn() {
	ARRAY=($1);
	IFS=" ";
	TZPATH=$(find /usr/share/zoneinfo/ -maxdepth 1 -iname ${ARRAY[0]});
	TZLOCATIONLST=($(ls $TZPATH | sed ':a;N;$!ba;s/\n/ /g'));
	IFS=$ORIGINALIFS;
}

# Changes the current timezone
# Array 
#  1 [String] Timezone
#  2 [String] Location
# Returns
#  - 1: Success
#  - 0: Failed
function changeCurrentTZFn() {
	if [ -f /etc/localtime ]; then
		rm /etc/localtime 2>/dev/null;
	fi
	TZPATH=$(find /usr/share/zoneinfo/ -maxdepth 1 -iname $1);
	if [ -d $TZPATH ]; then
		TZLOCATIONPATH=$(find $TZPATH -maxdepth 1 -iname $2);
		if [ -f $TZLOCATIONPATH ] || [ -L $TZLOCATIONPATH ]; then
			ln -s $TZLOCATIONPATH /etc/localtime 2>/dev/null;
			if [ $? -eq 0 ]; then
				city=$2;area=$1
				city="${city^}";area="${area^}"
				cou=$(cat $BASEPATH/config/countries | awk -v c=$city -F'\t' '$6 ~ c {print $5}')
				if [ -n "$cou" ]; then
				    mkdir -p /run/xbian-config
				    [ -d /home/xbian/.kodi ] && XBMC_KODI_DIR="/home/xbian/.kodi" || XBMC_KODI_DIR="/home/xbian/.xbmc"
				    echo "eval \$(echo \"sed -i 's/<timezone>.*<\/timezone>/<timezone>$area\/$city<\/timezone>/' $XBMC_KODI_DIR/userdata/guisettings.xml\")" >> /run/xbian-config/xbmc-tz
				    echo "eval \$(echo \"sed -i 's/<timezonecountry>.*<\/timezonecountry>/<timezonecountry>$cou<\/timezonecountry>/' $XBMC_KODI_DIR/userdata/guisettings.xml\")" >> /run/xbian-config/xbmc-tz
				fi
				if pgrep xbmc.bin >/dev/null || pgrep kodi.bin >/dev/null; then
				    cat /run/xbian-config/xbmc-tz >> /run/xbian-config/xbmc-changesettings.sh 2>/dev/null
				else
				    sh /run/xbian-config/xbmc-tz
				fi
				rm -fr /run/xbian-config/xbmc-tz
				return 1;
			else
				return 0;
			fi
		else
			return 0;
		fi
	else
		return 0;
	fi
}

# Returns the current date
function getCurrentDateTimeFn() {
	DATETIME=$(date)
}
