-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimghelper.subr
242 lines (189 loc) · 6.38 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
# Compare imghelper_version with forms version
check_version()
{
local forms_version=
forms_version=$( cbsdsql ${formfile} SELECT version FROM system 2>/dev/null )
[ -z "${forms_version}" ] && err 1 "${MAGENTA}Unsupported image schema: version is empty${NORMAL}"
[ -z "${imghelper_version}" ] && err 1 "${MAGENTA}Unsupported image schema: imghelper is empty${NORMAL}"
[ ${forms_version} -gt ${imghelper_version} ] && err 1 "${MAGENTA}Unsupported image schema. Image/Forms version is: ${GREEN}${imghelper_version}/${forms_version}${MAGENTA}. Please update CBSD first${NORMAL}"
return 0
}
init_title()
{
forms_title=
forms_title=$( cbsdsql ${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
eval _T=\${${i}}
eval mandatory=\$${i}_mandatory
[ -z "${_T}" -a "${mandatory}" = "1" ] && return 1
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 -p ${jailsysdir}/${jname}/helpers
_imgformfile="${jailsysdir}/${jname}/helpers/${helpername}.sqlite"
[ "${cfg_existing}" != "1" ] && /bin/cp -aH ${formfile} ${_imgformfile}
for i in ${param}; do
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="$( cbsdsql ${formfile} SELECT id FROM ${_link} WHERE text=\"${_T}\" )"
# txt="$( cbsdsql ${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
[ ! -f ${formfile} ] && err 1 "${MAGENTA}formfile not found: ${GREEN}${formfile}${NORMAL}"
[ -z "${table}" ] && table="forms"
idx=0
param="$( cbsdsql ${formfile} SELECT param FROM ${table}|/usr/bin/xargs )"
# eval $( cbsdsql ${formfile} SELECT param,mytable FROM ${table} WHERE type != \"subtable\" ORDER BY order_id |while read _x; do
# idx=$(( idx + 1 ))
# unset _param _mytable
# sqllist "${_x}" _param _mytable
# echo "param${idx}=\"${_param}\""
# echo "table${idx}=\"${_mytable}\""
# done )
helpername=$( cbsdsql ${formfile} SELECT helpername FROM system )
packages=$( cbsdsql ${formfile} SELECT packages FROM system )
have_restart=$( cbsdsql ${formfile} SELECT have_restart FROM system )
[ "${have_restart}" = "0" ] && unset have_restart
[ "${packages}" = "0" ] && packages=""
_id=0 # First SQL sequence is 1
eval $( for i in ${param}; do
unset _env desc _res cur attr xattr mandatory groupname group_id
_id="$( cbsdsql ${formfile} SELECT idx FROM ${table} WHERE param=\"${i}\" LIMIT 1 )"
desc="$( cbsdsql ${formfile} SELECT desc FROM ${table} WHERE idx=\"${_id}\" |/usr/bin/tr -d "'\"")"
is_delimer=0
is_hiddendelim=0
is_subtable=0
is_value=0
_res=
unset type
type="$( cbsdsql ${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
i="forms_delimer_${_id}"
;;
group_add|group_del)
is_value=0
;;
*)
is_value=1
;;
esac
# check for H_environment
[ ${is_value} -eq 1 ] && eval _res="\$H_${i}"
# echo " >>> ${i} is value: ${is_value}, IDX = ${_id}, TYPE: ${type}, DESC: [${desc}], RES: ${_res}" >> /tmp/forms.txt
if [ -n "${_res}" ]; then
# environment is preferred
echo "${i}=\"${_res}\""
# echo "${i}=\"${_res}\"" >> /tmp/forms.txt
elif [ ${is_value} -eq 1 ]; then
# preferred for cur value
cur="$( cbsdsql ${formfile} SELECT cur FROM ${table} WHERE idx=\"${_id}\" )"
if [ -n "${cur}" ]; then
def="${cur}"
else
def="$( cbsdsql ${formfile} SELECT def FROM ${table} WHERE idx=\"${_id}\" )"
fi
unset link
link="$( cbsdsql ${formfile} SELECT link FROM ${table} WHERE idx=\"${_id}\" )"
if [ -n "${link}" ]; then
def2="$( cbsdsql ${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 "${i}=\"${def}\""
# echo "${i}=\"${def}\"" >> /tmp/forms.txt
elif [ ${is_delimer} -eq 1 ]; then
echo "${i}=\"${i}\""
# echo "${i}=\"${i}\"" >> /tmp/forms.txt
fi
# todo: rewrite to sqllist
attr="$( cbsdsql ${formfile} SELECT attr FROM ${table} WHERE idx=\"${_id}\" )"
xattr="$( cbsdsql ${formfile} SELECT xattr FROM ${table} WHERE idx=\"${_id}\" )"
mandatory="$( cbsdsql ${formfile} SELECT mandatory FROM ${table} WHERE idx=\"${_id}\" )"
groupname="$( cbsdsql ${formfile} SELECT groupname FROM ${table} WHERE idx=\"${_id}\" )"
group_id="$( cbsdsql ${formfile} SELECT group_id FROM ${table} WHERE idx=\"${_id}\" )"
/bin/cat <<EOF
${i}_desc="${desc}"
${i}_mandatory="${mandatory}"
${i}_link="${link}"
${i}_type="${type}"
${i}_attr="${attr}"
${i}_groupname="${groupname}"
${i}_group_id="${group_id}"
EOF
#/bin/cat >> /tmp/forms.txt <<EOF
#${i}_desc="${desc}"
#${i}_mandatory="${mandatory}"
#${i}_link="${link}"
#${i}_type="${type}"
#${i}_attr="${attr}"
#${i}_groupname="${groupname}"
#${i}_group_id="${group_id}"
#EOF
done )
}
# $formfile and $table must be present
load_new()
{
[ ! -f ${formfile} ] && err 1 "${MAGENTA}formfile not found: ${GREEN}${formfile}${NORMAL}"
[ -z "${table}" ] && table="forms"
local param
param=$( cbsdsql ${formfile} SELECT param FROM ${table} ORDER BY CAST\(order_id AS int\) | /usr/bin/xargs )
local _env
eval $( for i in ${param}; do
[ "${i}" = "-" ] && continue
unset _env new
new="$( cbsdsql ${formfile} SELECT new FROM ${table} WHERE param=\"${i}\" )"
[ -z "${new}" ] && continue
echo "${i}=\"${new}\""
done )
}