-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtemplate.lib.sh
127 lines (107 loc) · 4.16 KB
/
template.lib.sh
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
#
# Copyright (c) 2010-2011 Linagora
# Patrick Guiran <[email protected]>
# http://github.com/Tauop/ScriptHelper
#
# ScriptHelper 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 2 of
# the License, or (at your option) any later version.
#
# ScriptHelper 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 Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# README ---------------------------------------------------------------------
# This library helps to deal with template
#
# Global variables ===========================================================
# IMPORTANT: Please to write to those variables
# __LIB_TEMPLATE__ : 'Loaded' when the lib is 'source'd
# __TEMPLATE_FILE__ : path the log file in which messages will be stored
# ----------------------------------------------------------------------------
# don't load several times this file
if [ "${__LIB_TEMPLATE__:-}" != 'Loaded' ]; then
__LIB_TEMPLATE__='Loaded'
__TEMPLATE_FILE__=''
# load dependencies
load() {
local var= value= file=
var="$1"; file="$2"
value=$( eval "printf '%s' \"\${${var}:-}\"" )
[ -n "${value}" ] && return 1;
if [ -f "${file}" ]; then
. "${file}"
else
printf "ERROR: Unable to load ${file}\n"
exit 2
fi
return 0;
}
# Load configuration file
load SCRIPT_HELPER_DIRECTORY /etc/ScriptHelper.conf
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY:-}"
SCRIPT_HELPER_DIRECTORY="${SCRIPT_HELPER_DIRECTORY%%/}"
load __LIB_MESSAGE__ "${SCRIPT_HELPER_DIRECTORY}/message.lib.sh"
load __LIB_RANDOM__ "${SCRIPT_HELPER_DIRECTORY}/random.lib.sh"
# ----------------------------------------------------------------------------
# usage: TEMPLATE_PREPARE <template_file> [ <temp_file> ]
# desc: create a temporary file, a copy of the template file, for replacing variable
# arguments:
# - <template_file> is a file containing variable in ${variable} format
# - <temp_file> is the temporary file to create
# note: if <temp_file> isn't specify, create a random file into /tmp
TEMPLATE_PREPARE () {
local file= tmp_file=
[ $# -ne 1 -a $# -ne 2 ] && FATAL "Bad arguments"
file="$1"
[ ! -r "${file}" ] && FATAL "Can't read file ${file}."
[ $# -eq 2 ] && tmp_file="$2" || tmp_file="/tmp/file.$(RANDOM)"
cp "${file}" "${tmp_file}" 2>/dev/null
[ $? -ne 0 ] && FATAL "Can't write into file ${tmp_file}."
TEMPLATE_CLEAN
__TEMPLATE_FILE__="${tmp_file}"
}
# usage: private_SED_SEPARATOR <string>
# desc: determine a good sed separator
private_SED_SEPARATOR () {
for s in '/' '@' ',' '|'; do
printf '%s' "$1" | grep "$s" >/dev/null
if [ $? -ne 0 ]; then
printf '%s' "$s"; return 0;
fi
done
return 1;
}
# usage: TEMPLATE_REPL_VAR <variable> [<value>]
# desc: Replace a variable in template file
# arguments:
# - <variable> to replace
# - <value> to set
# note: if <value> is not set, use <variable> value
# note: variable must be in ${variable} format
TEMPLATE_REPL_VAR () {
local var= value= tmp_file= sep=
[ $# -eq 1 -a $# -eq 2 ] && FATAL "Bad arguments"
var="$1"
[ $# -eq 1 ] && eval "value=\"\${${1}}\"" || value="$2"
sep=$( private_SED_SEPARATOR "${var}${value}" )
tmp_file="${__TEMPLATE_FILE__}.$(RANDOM)"
sed -e "s${sep}[$][{]${var}[}]${sep}${value}${sep}g" < "${__TEMPLATE_FILE__}" > "${tmp_file}"
mv "${tmp_file}" "${__TEMPLATE_FILE__}"
}
# usage: TEMPLATE_GET_FILE
# desc: echo-return the filepath of the prepared file
TEMPLATE_GET_FILE () {
printf "%s" "${__TEMPLATE_FILE__}"
}
# usage: TEMPLATE_CLEAN
# desc: remove the temporary file create for template manipulation
TEMPLATE_CLEAN () {
[ -w "${__TEMPLATE_FILE__}" ] && rm -f "${__TEMPLATE_FILE__}"
}
fi # end of: if [ "${__LIB_TEMPLATE__:-}" != 'Loaded' ]; then