-
Notifications
You must be signed in to change notification settings - Fork 17
/
jp-hb-devices-addon
executable file
·350 lines (287 loc) · 12.7 KB
/
jp-hb-devices-addon
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#!/bin/sh
# WebUI icon used from http://icons8.com
ADDON_NAME=`basename "$0"`
ADDON_DIR=/usr/local/addons/${ADDON_NAME}
PATCH_DIR=${ADDON_DIR}/patch
NEW_ADDON_DIR=${ADDON_DIR}-new
WWW_DIR=/usr/local/etc/config/addons/www/$ADDON_NAME
LOG_DIR=${ADDON_DIR}/log
TMP_LOGDIR=/tmp
TMP_LOGFILE=${TMP_LOGDIR}/${ADDON_NAME}-inst.log
TMP_ERRFILE=${TMP_LOGDIR}/${ADDON_NAME}-inst.err
FIRMWARE_DIR=/firmware/rftypes
CUSTOMIZED_FIRMWARE_DIR=${ADDON_DIR}/customized_firmware
RC_DIR=/usr/local/etc/config/rc.d
CK_FILE=/www/api/methods/ccu/downloadFirmware.tcl
RL_FILE=/lib/udev/rules.d/99-hmip-rfusb.rules
CK_FIRMWARE_FILE=${FIRMWARE_DIR}/hb-uni-sen-cap-moist.xml
PATCHSUBDIR_COMMON=common
check_ccu_fw_version()
{
model=`grep VERSION /boot/VERSION | awk -F'[=.]' {'print $2'}`
version=`grep VERSION /boot/VERSION | awk -F'[=.]' {'print $3'}`
build=`grep VERSION /boot/VERSION | awk -F'[=.]' {'print $4'}`
if [ $model -ge 2 ] && [ $version -le 43 ]; then
echo "Unsupported firmware version $model.$version.$build found. (2.45 / 3.45 or higher needed!)" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
exit 14
fi
if [ $model -ge 2 ] && [ $version -ge 65 ]; then
PATCHSUBDIR_VERSION=ge_365
else
PATCHSUBDIR_VERSION=le_363
fi
echo "Found firmware version $model.$version.$build - using patch subdirectory version ${PATCHSUBDIR_VERSION}" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
}
convert_lf()
{
filename=`sed -n '2p' $1 | sed 's/+++ .\/patchsource//g' | awk {'print $1'}`
# we have to convert the original file to UNIX format; patch cannot patch files in DOS format :(
dos2unix $filename
cmpresult=`unix2dos < $filename | cmp - $filename`
if [ "$cmpresult" ]; then
cmpresult=`dos2unix < $1 | cmp - $1`
if [ "$cmpresult" ]; then
echo "dos2unix conversion needed for $1" | tee -a $TMP_LOGFILE
dos2unix $1 | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
fi
fi
}
MONIT=0
check_monit()
{
MONIT=`ps -ef|grep \/usr\/bin\/monit|grep -v grep|wc -l`
echo "Found $MONIT monit process(es)" | tee -a $TMP_LOGFILE
}
REGA=0
check_rega()
{
REGA=`ps -ef|grep \/bin\/ReGaHss|grep -v grep|wc -l`
echo "Found $REGA ReGaHss process(es)" | tee -a $TMP_LOGFILE
}
RFD=0
check_rfd()
{
RFD=`ps -ef|grep \/bin\/rfd|grep -v grep|wc -l`
echo "Found $RFD rfd process(es)" | tee -a $TMP_LOGFILE
}
cleanup_orig_files()
{
[[ -f /www/webui/webui.js.orig ]] && rm /www/webui/webui.js.orig
[[ -f /www/rega/esp/datapointconfigurator.fn.orig ]] && rm /www/rega/esp/datapointconfigurator.fn.orig
[[ -f /www/rega/esp/functions.fn.orig ]] && rm /www/rega/esp/functions.fn.orig
[[ -f /www/rega/esp/side.inc.orig ]] && rm /www/rega/esp/side.inc.orig
[[ -f /www/rega/pages/tabs/admin/views/programs.htm.orig ]] && rm /www/rega/pages/tabs/admin/views/programs.htm.orig
[[ -f /www/config/ic_common.tcl.orig ]] && rm /www/config/ic_common.tcl.orig
}
do_patch()
{
echo "### $2 patch file $1" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
convert_lf $1
patch $3 -p3 -i $1 >> $TMP_LOGFILE 2>>$TMP_ERRFILE
echo "- done" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
}
patcher()
{
if [[ "$1" == "APPLY" ]]; then
PATCH_METHOD_HINT="APPLY"
PATCH_METHOD_PARAM=""
elif [[ "$1" == "REVOKE" ]]; then
PATCH_METHOD_HINT="REVOKE"
PATCH_METHOD_PARAM="-R"
else
echo "Invalid patcher() param $1" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
return
fi
cd /www
for patchfile in ${PATCH_DIR}/${PATCHSUBDIR_COMMON}/* ; do
do_patch $patchfile $PATCH_METHOD_HINT $PATCH_METHOD_PARAM
done
for patchfile in ${PATCH_DIR}/${PATCHSUBDIR_VERSION}/* ; do
do_patch $patchfile $PATCH_METHOD_HINT $PATCH_METHOD_PARAM
done
cleanup_orig_files
}
case "$1" in
""|init|start)
if [ -e "$CK_FILE" ] && grep -iq 'to\|bu' "$CK_FILE" ; then exit 13; fi
if [ -e "$RL_FILE" ] && grep -iq 'hmul' "$RL_FILE" ; then exit 13; fi
check_ccu_fw_version
[[ -f $TMP_ERRFILE ]] && rm $TMP_ERRFILE
[[ -f $TMP_LOGFILE ]] && rm $TMP_LOGFILE
### Check if the ccu firmware was updated or there was a new addon installation uploaded
if [ ! -f ${CK_FIRMWARE_FILE} ] || [ -d ${NEW_ADDON_DIR} ]; then
echo "Starting complete installation within '$1' $(date +"%d.%m.%Y %H:%M:%S")" | tee $TMP_LOGFILE
#### Check if monit process exists
check_monit
if [ $MONIT -ge 1 ]; then
echo "Stopping monitoring service for ReGaHss and RFD" | tee -a $TMP_LOGFILE
/usr/bin/monit unmonitor ReGaHss
/usr/bin/monit unmonitor rfd
fi
check_rega
if [ $REGA -ge 1 ]; then
/etc/init.d/S70ReGaHss stop | tee -a $TMP_LOGFILE
fi
check_rfd
if [ $RFD -ge 1 ]; then
/etc/init.d/S61rfd stop | tee -a $TMP_LOGFILE
fi
mount -o remount,rw /
### revoke patches from currently installed addon version
if [ -f ${CK_FIRMWARE_FILE} ] && [ -d ${PATCH_DIR} ]; then
currentversion_major=`cat /usr/local/addons/jp-hb-devices-addon/VERSION | awk -F'[=.]' {'print $1'}`
currentversion_minor=`cat /usr/local/addons/jp-hb-devices-addon/VERSION | awk -F'[=.]' {'print $2'}`
if [ $currentversion_major -eq 5 ]; then
PATCHSUBDIR_VERSION=ge_345
fi
echo "Revoking current addon V $currentversion_major.$currentversion_minor patch files." | tee $TMP_LOGFILE | tee $TMP_ERRFILE
patcher "REVOKE"
check_ccu_fw_version
fi
### delete the old (current) addon directory and rename the newly uploaded
if [ -d ${NEW_ADDON_DIR} ]; then
if [ -d ${ADDON_DIR} ]; then
### backup customized_firmware files in new addon directory, before deleting the old (current) one!
if [ "$(find ${CUSTOMIZED_FIRMWARE_DIR} -type f -iname '*.xml')" ]; then
echo "Copying customized firmware files to new addon directory" | tee -a $TMP_LOGFILE
cp ${CUSTOMIZED_FIRMWARE_DIR}/*.xml ${NEW_ADDON_DIR}/customized_firmware/
fi
echo "Deleting current addon directory " | tee $TMP_LOGFILE
rm -rf ${ADDON_DIR}
fi
echo "Renaming ${NEW_ADDON_DIR} to ${ADDON_DIR}" | tee $TMP_LOGFILE
mv ${NEW_ADDON_DIR} ${ADDON_DIR}
fi
cd ${ADDON_DIR}
cp -ar www/* /www/
chown root:root /www/config/img/devices/250/hb-*
chmod 755 /www/config/img/devices/250/hb-*
chown root:root /www/config/img/devices/50/hb-*
chmod 755 /www/config/img/devices/50/hb-*
chown root:root /www/ise/img/icons_hm_dis_ep_wm55/24/*
chmod 755 /www/ise/img/icons_hm_dis_ep_wm55/24/*
# Symbolic Links
rm -rf ${WWW_DIR}
ln -sf ${ADDON_DIR} ${WWW_DIR}
patcher "APPLY"
### Running inst_* scripts for each device installation
cd ${ADDON_DIR}
echo "Running scripts..." | tee -a $TMP_LOGFILE
for f in ${ADDON_DIR}/inst_* ; do echo " - $(basename $f)"; ./$(basename $f) install >> $TMP_LOGFILE 2>>$TMP_ERRFILE; done
if [ "$(find ${CUSTOMIZED_FIRMWARE_DIR} -type f -iname '*.xml')" ]; then
echo "Copying customized firmware files..." | tee -a $TMP_LOGFILE
cp ${CUSTOMIZED_FIRMWARE_DIR}/*.xml ${ADDON_DIR}${FIRMWARE_DIR}/
else
echo "No customized firmware files found in ${CUSTOMIZED_FIRMWARE_DIR}." | tee -a $TMP_LOGFILE
fi
echo "(Re-)Creating symlinks for firmware files..." | tee -a $TMP_LOGFILE
for f in ${ADDON_DIR}${FIRMWARE_DIR}/* ; do rm -f ${FIRMWARE_DIR}/$(basename $f); ln -s $f ${FIRMWARE_DIR}/$(basename $f); echo " - $(basename $f)"; done
sync
mount -o remount,ro /
if [ $RFD -ge 1 ]; then
/etc/init.d/S61rfd start | tee -a $TMP_LOGFILE
fi
if [ $REGA -ge 1 ]; then
/etc/init.d/S70ReGaHss start | tee -a $TMP_LOGFILE
fi
if [ $MONIT -ge 1 ]; then
echo "Starting monitoring service for ReGaHss and RFD" | tee -a $TMP_LOGFILE
/usr/bin/monit monitor ReGaHss
/usr/bin/monit monitor rfd
fi
echo "Installation done." | tee -a $TMP_LOGFILE
else
echo "Checking for accidently restored unpatched programs.htm"
if [ "$(grep HBOUCFM /www/rega/pages/tabs/admin/views/programs.htm)" ]; then
echo "programs.htm is ok"
else
mount -o remount,rw /
patchfile=${PATCH_DIR}/${PATCHSUBDIR_VERSION}/programs.htm.patch
echo "Applying ${PATCHSUBDIR_VERSION} patch file $(basename $patchfile)" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
convert_lf $patchfile
patch -N -p3 -i $patchfile >> $TMP_LOGFILE 2>>$TMP_ERRFILE
echo "- done" | tee -a $TMP_LOGFILE | tee -a $TMP_ERRFILE
mount -o remount,ro /
fi
echo "Checking for subsequent customized firmware files..."
changed=0
for f in ${ADDON_DIR}${FIRMWARE_DIR}/* ; do
$(cmp -s ${CUSTOMIZED_FIRMWARE_DIR}/$(basename $f) $f)
rc=$?
if [ $rc -eq 1 ]; then
echo "Difference detected for $(basename $f). Copying..."
cp ${CUSTOMIZED_FIRMWARE_DIR}/$(basename $f) $f
let changed++
fi
done
if [ $changed -gt 0 ]; then
echo "$changed Firmware file(s) changed" | tee -a $TMP_LOGFILE
check_rfd
if [ $RFD -ge 1 ]; then
check_monit
if [ $MONIT -ge 1 ]; then
echo " - restarting RFD [monit]" | tee -a $TMP_LOGFILE
/usr/bin/monit restart rfd
else
echo " - restarting RFD [init.d]" | tee -a $TMP_LOGFILE
/etc/init.d/S61rfd restart
fi
fi
else
echo "Nothing changed - nothing to do"
fi
fi
if [ -f ${TMP_ERRFILE} ]; then
errors=`grep -i -E 'fail|err' ${TMP_ERRFILE}`
if [ -z $errors ]; then
echo "No errors found during installation"
[[ -f ${ADDON_DIR}/install_error ]] && rm ${ADDON_DIR}/install_error
else
echo "Errors found during installation:"
cat ${TMP_ERRFILE}
touch ${ADDON_DIR}/install_error
fi
fi
cp ${TMP_LOGDIR}/${ADDON_NAME}-* ${LOG_DIR}/
if [ ! -f /www/rega/${ADDON_NAME}-error.log ]; then mount -o remount,rw /; ln -s ${LOG_DIR}/${ADDON_NAME}-inst.err /www/rega/${ADDON_NAME}-error.log; mount -o remount,ro /; fi
;;
stop)
echo "Nothing to stop..."
;;
uninstall)
check_ccu_fw_version
mount -o remount,rw /
cd ${ADDON_DIR}
echo "Running scripts..."
for f in ${ADDON_DIR}/inst_* ; do echo " - $(basename $f)"; ./$(basename $f) uninstall > $TMP_LOGFILE 2>$TMP_ERRFILE; done
patcher "REVOKE"
[[ -f /www/rega/${ADDON_NAME}-error.log ]] && rm /www/rega/${ADDON_NAME}-error.log
#delete copied files
cd ${ADDON_DIR}/www
find . -type f | sed 's/^./\/www/' | xargs -I {} sh -c '[[ -f {} ]] && rm {}'
cd /
rm -f ${WWW_DIR}
rm -rf ${ADDON_DIR}
sync
mount -o remount,ro /
# the last thing: we delete ourselves
rm -f ${RC_DIR}/${ADDON_NAME}
;;
restart|reload)
# Operations to stop and start (restart) the addon
;;
info)
echo "Info: <center><b>JP HB Devices Support Addon</b></center><br />"
echo "Info: <center><img src='../addons/${ADDON_NAME}/${ADDON_NAME}.png'></img></center><br/><a target='_blank' href='https://github.com/jp112sdl/${ADDON_NAME}'>https://github.com/jp112sdl/${ADDON_NAME}</a>"
echo "Version: $(cat ${ADDON_DIR}/VERSION)"
echo "Name: JP HB Devices"
echo "Operations: uninstall"
#echo "Config-Url: ${CONFIG_URL}"
echo "Update: /addons/${ADDON_NAME}/update-check.cgi"
;;
*)
echo "Usage: $ADDON_NAME {start|info|uninstall}" >&2
exit 1
;;
esac
exit $?