summaryrefslogtreecommitdiff
path: root/scripts/inc/functions.sh
blob: 0fdc8a18b57085b676a90660ea795b2f6a58eedf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/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
}