#!/bin/bash

function parse_args() {
	if [ $# -lt 1 ]
	then	return 1
	fi

	arg_source_machine="$1"
	return 0
}

function print_usage() {
	echo "Usage : $0 <source_machine>"
}

export LOG_NOTICE="notice"
export LOG_WARN="warning"
export LOG_ERR="error"
function log() {
	{
		echo -n "$(date --rfc-3339 seconds) $(hostname) $0: $2: <$(eval echo \$$1)> "
		shift 2
		echo "$*"
	} | tee -a ./bkp.log
}

function run_or_die() {
	local l_exit_value_on_failure="$1"
	local l_func_to_call="$2"
	shift 2

	$l_func_to_call "$@"
	res=$?
	if [ $res -ne 0 ]
	then	log "$log_type" "$l_func_to_call" "$log_message"
		exit $l_exit_value_on_failure
	fi
}

function check_env() {
	if [ $UID -ne 0 ]
	then	log LOG_WARN "This script is intended to be ran by root"
		return 1
	fi
	return 0
}

#INI function from http://ajdiaz.wordpress.com/2008/02/09/bash-ini-parser/
cfg_parser ()
{
    ini="$(<$1)"                # read the file
    ini="${ini//[/\[}"          # escape [
    ini="${ini//]/\]}"          # escape ]
    IFS=$'\n' && ini=( ${ini} ) # convert to line-array
    ini=( ${ini[*]//;*/} )      # remove comments with ;
    ini=( ${ini[*]/\	=/=} )  # remove tabs before =
    ini=( ${ini[*]/=\	/=} )   # remove tabs be =
    ini=( ${ini[*]/\ =\ /=} )   # remove anything with a space around =
ini=( ${ini[*]/#/cfg_} ) # lpo : Add cfg prefix for keys
    ini=( ${ini[*]/#cfg_\\[/\}$'\n'cfg.section.} ) # set section prefix
    ini=( ${ini[*]/%\\]/ \(} )    # convert text2function (1)
    ini=( ${ini[*]/=/=\( } )    # convert item to array
    ini=( ${ini[*]/%/ \)} )     # close array parenthesis
    ini=( ${ini[*]/%\\ \)/ \\} ) # the multiline trick
    ini=( ${ini[*]/%\( \)/\(\) \{} ) # convert text2function (2)
    ini=( ${ini[*]/%\} \)/\}} ) # remove extra parenthesis
    ini[0]="" # remove first element
    ini[${#ini[*]} + 1]='}'    # add the last brace
    eval "$(echo "${ini[*]}")" # eval the result
}

#function array_key_exists() {
#	local l_key="$1"
#	#Special behavior : pass as 2nd arg the keys of array with "${!array[@]}"
#	shift
#	
#	for k in "$@"
#	do
#		[ "x$l_key" == "x$k" ] && return 0
#	done
#
#	return 1
#}

function load_conf() {
	local l_conffile="$1"
	local l_source_machine="$2"
	
	cfg_parser "$l_conffile"
	cfg.section.common
	cfg.section.$l_source_machine

#	source "$l_conffile"

#	if array_key_exists "$l_source_machine" "${!CONF_SOURCE_POSSIBLE_ADDRESSES[@]}"
#	then	cfg_source_possible_addresses=${CONF_SOURCE_POSSIBLE_ADDRESSES["$l_source_machine"]}
#	else	log LOG_ERROR load_conf "No definition for CONF_SOURCE_POSSIBLE_ADDRESSES['$l_source_machine']"
#		return 1
#	fi
	
	#TODO : check loaded values
}

function find_source_address() {
	local l_oldifs="$IFS"
	local l_hostname="$(hostname)"
	
	if [ x"$l_hostname" == x"$arg_source_machine" ]
	then	runtime_source_address="" # Empty string means localhost
		return 0
	fi

	runtime_source_address=""
	IFS=", 	"
	for a in $*
	do
		ping -w 1 -c 1 -q "$a" &>/dev/null
		if [ $? -eq 0 ]
		then	runtime_source_address="$a"
			break
		fi
	done
	IFS="$l_oldifs"

	if [ -z "$runtime_source_address" ]
	then return 1
	fi

	return 0
}