forked from xiaoyao9184/DomoticzXiaomiPlugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstall.sh
361 lines (314 loc) · 10.7 KB
/
install.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
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
351
352
353
354
355
356
357
358
359
360
361
#!/usr/bin/env bash
# Domoticz Xiaomi Plugins
# (c) 2018 by xiaoyao9184
# https://github.com/xiaoyao9184/DomoticzXiaomiPlugins
# Installs Domoticz Xiaomi Plugins
#
# Domoticz Xiaomi Plugins 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 3 of the License, or
# (at your option) any later version.
# Donations are welcome via the website or application
#
# Install with this command (from your Pi):
#
# curl -L https://github.com/SylvainPer/DomoticzXiaomiPlugins/raw/master/install.sh | bash
set -e
######## VARIABLES #########
setupVars=/etc/domoticz/setupVars.conf
useUpdateVars=false
Dest_folder=""
Temp_folder="/tmp/DomoticzXiaomiPlugins"
lowercase(){
echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
}
OS=`lowercase \`uname -s\``
MACH=`uname -m`
if [ ${MACH} = "armv6l" ]
then
MACH="armv7l"
fi
# Find the rows and columns will default to 80x24 is it can not be detected
screen_size=$(stty size 2>/dev/null || echo 24 80)
rows=$(echo $screen_size | awk '{print $1}')
columns=$(echo $screen_size | awk '{print $2}')
# Divide by two so the dialogs take up half of the screen, which looks nice.
r=$(( rows / 2 ))
c=$(( columns / 2 ))
# Unless the screen is tiny
r=$(( r < 20 ? 20 : r ))
c=$(( c < 70 ? 70 : c ))
######## FIRST CHECK ########
# Must be root to install
echo ":::"
if [[ ${EUID} -eq 0 ]]; then
echo "::: You are root."
else
echo "::: Script called with non-root privileges. The Domoticz Xiaomi Plugin installs server packages and configures"
echo "::: system networking, it requires elevated rights. Please check the contents of the script for"
echo "::: any concerns with this requirement. Please be sure to download this script from a trusted source."
echo ":::"
echo "::: Detecting the presence of the sudo utility for continuation of this install..."
if [ -x "$(command -v sudo)" ]; then
echo "::: Utility sudo located."
exec curl -sSL https://github.com/SylvainPer/DomoticzXiaomiPlugins/raw/master/install.sh | sudo bash "$@"
exit $?
else
echo "::: sudo is needed for the Web interface to run domoticz commands. Please run this script as root and it will be automatically installed."
exit 1
fi
fi
# Compatibility
if [ -x "$(command -v apt-get)" ]; then
#Debian Family
#############################################
PKG_MANAGER="apt-get"
PKG_CACHE="/var/lib/apt/lists/"
UPDATE_PKG_CACHE="${PKG_MANAGER} update"
PKG_UPDATE="${PKG_MANAGER} upgrade"
PKG_INSTALL="${PKG_MANAGER} --yes --fix-missing install"
# grep -c will return 1 retVal on 0 matches, block this throwing the set -e with an OR TRUE
PKG_COUNT="${PKG_MANAGER} -s -o Debug::NoLocking=true upgrade | grep -c ^Inst || true"
INSTALLER_DEPS=( libffi-dev libssl-dev git python3 python3-pip)
PYTHON_DEPS=( python-miio ptvsd rpdb )
package_check_install() {
dpkg-query -W -f='${Status}' "${1}" 2>/dev/null | grep -c "ok installed" || ${PKG_INSTALL} "${1}"
}
PIP_INSTALL="pip3 install"
pip_install_list=$(pip3 list --format=columns)
pip_check_install() {
echo $pip_install_list | grep -c "${1}" || ${PIP_INSTALL} "$@"
}
else
echo "OS distribution not supported"
exit
fi
####### FUNCTIONS ##########
spinner() {
local pid=$1
local delay=0.50
local spinstr='/-\|'
while [ "$(ps a | awk '{print $1}' | grep "${pid}")" ]; do
local temp=${spinstr#?}
printf " [%c] " "${spinstr}"
local spinstr=${temp}${spinstr%"$temp"}
sleep ${delay}
printf "\b\b\b\b\b\b"
done
printf " \b\b\b\b"
}
find_current_user() {
# Find current user
Current_user=${SUDO_USER:-$USER}
echo "::: Current User: ${Current_user}"
}
welcomeDialogs() {
# Display the welcome dialog
whiptail --msgbox --backtitle "Welcome" --title "Domoticz Xiaomi Plugin automated installer" "\n\nThis installer will install xiaomi plugin to your Domoticz!\n\n
Domoticz Xiaomi Plugin is free\n\n
Domoticz Xiaomi Plugin needs a miio project for communicate with the device, must make sure device token is ready.
" ${r} ${c}
}
displayFinalMessage() {
# Final completion message to user
whiptail --msgbox --backtitle "Ready..." --title "Installation Complete!" "Go to Domoticz and add plugin.
Github: https://github.com/SylvainPer/DomoticzXiaomiPlugins" ${r} ${c}
}
chooseServices() {
cd "$Temp_folder/DomoticzXiaomiPlugins"
index=0
indexs=()
options=()
for d in */; do
indexs[index]=$d
i=$((index * 3))
options[i]=${d}
options[$((i + 1))]="Enable plugin ${d}"
options[$((i + 2))]=on
index=$((index + 1))
done
echo "::: Find ${#indexs[*]} plugins in $Temp_folder/DomoticzXiaomiPlugins"
cmd=(whiptail --separate-output --checklist "Select Plugin (press space to select)" ${r} ${c} ${#indexs[*]})
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
if [[ $? = 0 ]];then
count=0
for choice in ${choices}
do
echo "::: Choice install plugin ${choice}"
count=$((count + 1))
done
if [ ${count} -eq 0 ]; then
echo "::: Cannot continue, nothing plugins selected"
echo "::: Exiting"
exit 1
fi
else
echo "::: Cancel selected. Exiting..."
exit 1
fi
}
chooseDestinationFolder() {
Dest_folder=$(whiptail --inputbox "Domoticz Folder:" ${r} ${c} ${Dest_folder} --title "Destination" 3>&1 1>&2 2>&3)
exitstatus=$?
if [ $exitstatus = 0 ]; then
echo ":::"
else
echo "::: Cancel selected. Exiting..."
exit 1
fi
}
stop_service() {
# Stop service passed in as argument.
echo ":::"
echo -n "::: Stopping ${1} service..."
if [ -x "$(command -v service)" ]; then
service "${1}" stop &> /dev/null & spinner $! || true
fi
echo " done."
}
start_service() {
# Start/Restart service passed in as argument
# This should not fail, it's an error if it does
echo ":::"
echo -n "::: Starting ${1} service..."
if [ -x "$(command -v service)" ]; then
service "${1}" restart &> /dev/null & spinner $!
fi
echo " done."
}
update_package_cache() {
#Running apt-get update/upgrade with minimal output can cause some issues with
#requiring user input (e.g password for phpmyadmin see #218)
#Check to see if apt-get update has already been run today
#it needs to have been run at least once on new installs!
timestamp=$(stat -c %Y ${PKG_CACHE})
timestampAsDate=$(date -d @"${timestamp}" "+%b %e")
today=$(date "+%b %e")
if [ ! "${today}" == "${timestampAsDate}" ]; then
#update package lists
echo ":::"
echo -n "::: ${PKG_MANAGER} update has not been run today. Running now..."
${UPDATE_PKG_CACHE} &> /dev/null & spinner $!
echo " done!"
fi
}
notify_package_updates_available() {
# Let user know if they have outdated packages on their system and
# advise them to run a package update at soonest possible.
echo ":::"
echo -n "::: Checking ${PKG_MANAGER} for upgraded packages...."
updatesToInstall=$(eval "${PKG_COUNT}")
echo " done!"
echo ":::"
if [[ ${updatesToInstall} -eq "0" ]]; then
echo "::: Your system is up to date! Continuing with Domoticz installation..."
else
echo "::: There are ${updatesToInstall} updates available for your system!"
echo "::: We recommend you run '${PKG_UPDATE}' after installing Domoticz! "
echo ":::"
fi
}
install_dependent_packages() {
# Install packages passed in via argument array
# No spinner - conflicts with set -e
declare -a argArray1=("${!1}")
for i in "${argArray1[@]}"; do
echo -n "::: Checking for $i..."
package_check_install "${i}" &> /dev/null
echo " installed!"
done
}
install_python_packages() {
ua=( pip setuptools )
for i in "${ua[@]}"; do
echo -n "::: Checking for python $i..."
pip_check_install "${i}" "-U" 1> /dev/null
echo " installed!"
done
# Install packages passed in via argument array
# No spinner - conflicts with set -e
declare -a argArray1=("${!1}")
for i in "${argArray1[@]}"; do
echo -n "::: Checking for python $i..."
pip_check_install "${i}" 1> /dev/null
echo " installed!"
done
}
install_packages() {
# Update package cache
update_package_cache
# Notify user of package availability
notify_package_updates_available
# Install packages used by this installation script
install_dependent_packages INSTALLER_DEPS[@]
# Install packages used by the Domoticz
install_python_packages PYTHON_DEPS[@]
}
downloadDomoticzPlugin() {
if [[ -e $Temp_folder ]]; then
rm -f -r $Temp_folder
fi
echo "::: Creating ${Temp_folder}"
mkdir $Temp_folder
cd $Temp_folder
# Get plugin
echo "::: Clone github"
git clone https://github.com/SylvainPer/DomoticzXiaomiPlugins &> /dev/null
cd "./DomoticzXiaomiPlugins"
}
installDomoticzPlugin() {
cd "$Temp_folder/DomoticzXiaomiPlugins"
if [[ ! -e "${Dest_folder}/plugins/" ]]; then
mkdir "${Dest_folder}/plugins/"
chown "${Current_user}":"${Current_user}" "${Dest_folder}/plugins/"
fi
# Move plugin
echo "::: Destination folder=${Dest_folder}/plugins/"
for d in */; do
if echo "${choices[@]}" | grep -w "$d" &>/dev/null; then
echo -n "::: Move plugin ${d}..."
cp -a $d "${Dest_folder}/plugins/"
chown -R "${Current_user}":"${Current_user}" "${Dest_folder}/plugins/${d}"
chmod +x "${Dest_folder}/plugins/${d}plugin.py"
echo " done!"
fi
done
# Remove temp files
rm -f -r $Temp_folder
}
main() {
install_packages
downloadDomoticzPlugin
find_current_user
Dest_folder="/home/${Current_user}/domoticz"
if [[ -f ${setupVars} ]]; then
useUpdateVars=false
else
echo "Domoticz not installed!"
exit
fi
if [[ ${useUpdateVars} == false ]]; then
# Display welcome dialogs
welcomeDialogs
# Install and log everything to a file
chooseServices
chooseDestinationFolder
installDomoticzPlugin
fi
if [[ "${useUpdateVars}" == false ]]; then
displayFinalMessage
fi
echo "::: Restarting services..."
# ReStart services
cd ${Dest_folder}
stop_service domoticz.sh
start_service domoticz.sh
echo "::: done."
echo ":::"
if [[ "${useUpdateVars}" == false ]]; then
echo "::: Installation Complete!"
else
echo "::: Update complete!"
fi
}
main "$@"