-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimghelper.subr
291 lines (235 loc) · 7.23 KB
/
imghelper.subr
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
if [ ! "$_CBSD_IMGHELPER_SUBR" ]; then
_CBSD_IMGHELPER_SUBR=1
###
# Compare imghelper_version with forms version
check_version()
{
local forms_version=
forms_version=$( cbsdsqlro ${formfile} SELECT version FROM system 2>/dev/null )
[ -z "${forms_version}" ] && err 1 "${N1_COLOR}Unsupported image schema: version is empty${N0_COLOR}"
[ -z "${imghelper_version}" ] && err 1 "${N1_COLOR}Unsupported image schema: imghelper is empty${N0_COLOR}"
[ ${forms_version} -gt ${imghelper_version} ] && err 1 "${N1_COLOR}Unsupported image schema. Image/Forms version is: ${N2_COLOR}${imghelper_version}/${forms_version}${N1_COLOR}. Please update CBSD first${N0_COLOR}"
return 0
}
init_title()
{
forms_title=
forms_title=$( cbsdsqlro ${formfile} SELECT title FROM system 2>/dev/null )
return 0
}
# if all_filled; then
# echo "ALL FILLED"
# fi
all_filled()
{
local i _T
for i in ${param}; do
case "${i}" in
-*)
continue
;;
Expand)
continue
;;
esac
eval _T=\${${i}}
eval mandatory=\$${i}_mandatory
if [ -z "${_T}" -a "${mandatory}" = "1" ]; then
printf "${i}"
return 1
fi
done
return 0
}
# $1 - specify field to update, default is 'new' column
update_params()
{
local i _T _imgformfile
local _target="new" _link id
[ -n "${1}" ] && _target="${1}"
[ ! -d "${jailsysdir}/${jname}/helpers" ] && ${MKDIR_CMD} -p ${jailsysdir}/${jname}/helpers
_imgformfile="${jailsysdir}/${jname}/helpers/${helpername}.sqlite"
[ "${cfg_existing}" != "1" ] && ${CP_CMD} -aH ${formfile} ${_imgformfile}
for i in ${param}; do
case "${i}" in
-*)
continue
;;
# Expand)
# continue
# ;;
esac
eval _T=\${${i}}
if [ "${_T}" = "*****" ]; then
eval _T=\$${i}_password
fi
[ -z "${_T}" ] && continue
unset _link
eval _link=\$${i}_link
# deplace text value by id if linked field
if [ -n "${_link}" ]; then
unset txt id
id="$( cbsdsqlro ${formfile} SELECT id FROM ${_link} WHERE text=\"${_T}\" )"
# txt="$( cbsdsqlro ${formfile} SELECT txt FROM ${_link} WHERE text=\"${_T}\" )"
# [ -n "${txt}" ] && _T="${txt}"
# echo "I $id, T: $txt"
# read p
# it is bad construction for input box, eg:
# disk_select in installed: 'ada0' - found id
# 'ada0 ada' - not found
# [ -n "${id}" ] && _T="${id}"
fi
${miscdir}/sqlcli ${_imgformfile} UPDATE forms SET ${_target}=\"${_T}\" WHERE param=\"${i}\"
# cleanup new
[ "${_target}" != "new" ] && ${miscdir}/sqlcli ${_imgformfile} UPDATE forms SET new=\"\" WHERE param=\"${i}\"
done
echo "${helpername} formfile for ${jname}: updated"
return 0
}
# $formfile and $table must be present
load_forms()
{
local param _id def def2 link type _env idx lnk _vars
local _tmpfile
[ ! -f ${formfile} ] && err 1 "${N1_COLOR}formfile not found: ${N2_COLOR}${formfile}${N0_COLOR}"
[ -z "${table}" ] && table="forms"
idx=0
param="$( cbsdsqlro ${formfile} "SELECT param FROM ${table}" ORDER BY CAST\(order_id AS int\) | ${XARGS_CMD} )"
helpername=$( cbsdsqlro ${formfile} "SELECT helpername FROM system" )
packages=$( cbsdsqlro ${formfile} "SELECT packages FROM system" )
have_restart=$( cbsdsqlro ${formfile} "SELECT have_restart FROM system" )
[ "${have_restart}" = "0" ] && unset have_restart
[ "${packages}" = "0" ] && packages=
# First SQL sequence is 1
_id=0
_seq_ind=0
_tmpfile="/dev/stdout" # default
_tmpfile=$( ${MKTEMP_CMD} ) # for debuf
# export params_<ind> variables by sequence, not SQL index
for i in ${param}; do
_seq_ind=$(( _seq_ind + 1 ))
unset _env desc _res cur attr xattr mandatory groupname group_id
_id="$( cbsdsqlro ${formfile} "SELECT idx FROM ${table} WHERE param=\"${i}\" LIMIT 1" )"
desc="$( cbsdsqlro ${formfile} "SELECT desc FROM ${table} WHERE idx=\"${_id}\"" | ${TR_CMD} -d "'\"")"
is_delimer=0
is_hiddendelim=0
is_subtable=0
is_value=0
_res=
unset type
type="$( cbsdsqlro ${formfile} "SELECT type FROM ${table} WHERE idx=\"${_id}\"" )"
# check for special type or its value
case "${type}" in
hiddendelim)
is_hiddendelim=1
;;
subtable)
is_subtable=1
;;
delimer)
is_delimer=1
is_value=0
_vars="forms_delimer_${_seq_ind}"
#_vars="forms_delimer_${_seq_ind}"
;;
group_add|group_del)
is_value=1
is_delimier=1
#_vars="forms_group_add_${_seq_ind}"
#_vars="forms_delimer_${_seq_ind}"
#unset _vars
_vars="forms_groupadd_${_seq_ind}"
_vars="${i}"
;;
*)
is_value=1
;;
esac
# check for H_environment
if [ ${is_value} -eq 1 ]; then
eval _res="\$H_${_vars}"
_vars="${i}"
else
#
fi
[ -z "${_vars}" ] && continue
# echo " >>> ${_vars} is value: ${is_value}, IDX = ${_id}, TYPE: ${type}, DESC: [${desc}], RES: ${_res}" >> /tmp/forms.txt
if [ -n "${_res}" ]; then
# environment is preferred
echo "${_vars}=\"${_res}\"" >> ${_tmpfile}
# echo "${_vars}=\"${_res}\"" >> /tmp/forms.txt
elif [ ${is_value} -eq 1 ]; then
# preferred for cur value
cur="$( cbsdsqlro ${formfile} "SELECT cur FROM ${table} WHERE idx=\"${_id}\"" )"
if [ -n "${cur}" ]; then
def="${cur}"
else
def="$( cbsdsqlro ${formfile} "SELECT def FROM ${table} WHERE idx=\"${_id}\"" )"
fi
unset link
link="$( cbsdsqlro ${formfile} "SELECT link FROM ${table} WHERE idx=\"${_id}\"" )"
if [ -n "${link}" ]; then
def2="$( cbsdsqlro ${formfile} "SELECT text FROM ${link} WHERE id=\"${def}\"" )"
if [ -n "${def2}" ]; then
descr="${desc}. Text value for ID ${def}: ${def2}"
def="${def2}"
fi
fi
echo "${_vars}=\"${def}\"" >> ${_tmpfile}
# echo "${_vars}=\"${def}\"" >> /tmp/forms.txt
elif [ ${is_delimer} -eq 1 ]; then
echo "${_vars}=\"${_vars}\"" >> ${_tmpfile}
# echo "${_vars}=\"${_vars}\"" >> /tmp/forms.txt
fi
# todo: rewrite to sqllist
attr="$( cbsdsqlro ${formfile} "SELECT attr FROM ${table} WHERE idx=\"${_id}\"" )"
xattr="$( cbsdsqlro ${formfile} "SELECT xattr FROM ${table} WHERE idx=\"${_id}\"" )"
mandatory="$( cbsdsqlro ${formfile} "SELECT mandatory FROM ${table} WHERE idx=\"${_id}\"" )"
groupname="$( cbsdsqlro ${formfile} "SELECT groupname FROM ${table} WHERE idx=\"${_id}\"" )"
group_id="$( cbsdsqlro ${formfile} "SELECT group_id FROM ${table} WHERE idx=\"${_id}\"" )"
${CAT_CMD} >> ${_tmpfile} <<EOF
${_vars}_desc="${desc}"
${_vars}_mandatory="${mandatory}"
${_vars}_link="${link}"
${_vars}_type="${type}"
${_vars}_attr="${attr}"
${_vars}_groupname="${groupname}"
${_vars}_group_id="${group_id}"
EOF
#${CAT_CMD} >> /tmp/forms.txt <<EOF
#${_vars}_desc="${desc}"
#${_vars}_mandatory="${mandatory}"
#${_vars}_link="${link}"
#${_vars}_type="${type}"
#${_vars}_attr="${attr}"
#${_vars}_groupname="${groupname}"
#${_vars}_group_id="${group_id}"
done
. ${_tmpfile}
${RM_CMD} -f ${_tmpfile}
}
# $formfile and $table must be present
load_new()
{
[ ! -f ${formfile} ] && err 1 "${N1_COLOR}formfile not found: ${N2_COLOR}${formfile}${N0_COLOR}"
[ -z "${table}" ] && table="forms"
local param
param=$( cbsdsqlro ${formfile} "SELECT param FROM ${table} ORDER BY CAST(order_id AS int)" | ${XARGS_CMD} )
local _env
eval $( for i in ${param}; do
case "${i}" in
-*)
continue
;;
# Expand)
# continue
# ;;
esac
unset _env new
new="$( cbsdsqlro ${formfile} SELECT new FROM ${table} WHERE param=\"${i}\" )"
[ -z "${new}" ] && continue
echo "${i}=\"${new}\""
done )
}
###
fi