forked from geekslant/smartgarage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-smartgarage.sh
executable file
·440 lines (378 loc) · 10.9 KB
/
install-smartgarage.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/usr/bin/env bash
# https://github.com/geekslant/smartgarage/
version=v0.1
# The MIT License (MIT)
#
# Copyright (c) 2018 Geek Slant
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
if [[ $EUID -ne 0 ]]; then
echo "install-smartgarage must be run as root."
echo "Try: sudo ./install-smartgarage"
echo ""
exit 1
fi
set -e
SECONDS=0
previous_part_elapsed_time=0
total_elapsed_time=0
dir=`dirname $0`
skip_camera=0
camera_password=""
camera_ip_address=""
interactive_mode=0
total_parts=10
function ask() {
# https://djm.me/ask
local prompt default reply
while true; do
if [ "${2:-}" = "Y" ]; then
prompt="Y/n"
default=Y
elif [ "${2:-}" = "N" ]; then
prompt="y/N"
default=N
else
prompt="y/n"
default=
fi
# Ask the question (not using "read -p" as it uses stderr not stdout)
echo -n "$1 [$prompt] "
# Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
read reply </dev/tty
# Default?
if [ -z "$reply" ]; then
reply=$default
fi
# Check if the reply is valid
case "$reply" in
Y*|y*) return 0 ;;
N*|n*) return 1 ;;
esac
done
}
function bail() {
echo ""
hashline
if [ -z "$1" ]; then
echo "Exiting due to error"
else
echo "Exiting due to error: $*"
fi
stop_timer
show_elapsed_time
exit 1
}
function start_timer() {
SECONDS=0
}
function stop_timer() {
previous_part_elapsed_time=$SECONDS
let "total_elapsed_time=total_elapsed_time+previous_part_elapsed_time"
}
function show_elapsed_time() {
let "previous_part_elapsed_time_minutes=previous_part_elapsed_time/60"
let "previous_part_elapsed_time_seconds=previous_part_elapsed_time%60"
let "total_elapsed_time_minutes=total_elapsed_time/60"
let "total_elapsed_time_seconds=total_elapsed_time%60"
echo ""
if [ -z "$1" ]; then
echo -n "Part Elapsed Time: "
else
echo -n "Part $1 Elapsed Time: "
fi
printf "%02d:%02d\nTotal Elapsed Time: %02d:%02d\n" $previous_part_elapsed_time_minutes $previous_part_elapsed_time_seconds $total_elapsed_time_minutes $total_elapsed_time_seconds
}
function hashline() {
echo "###############################################################"
}
function infopart() {
stop_timer
let "previous_part=$1-1"
show_elapsed_time $previous_part
echo ""
hashline
echo "Part ${1}/${total_parts}:"
echo "${2}"
hashline
echo ""
}
function configure_opener_and_camera() {
echo ""
if ask "Do you know the camera password and IP address?"; then
echo ""
echo "Configuring Pi Smart Garage Door Opener and HiKam S6 Camera..."
echo ""
while true; do
echo -n "Camera password: "
read camera_password </dev/tty
echo -n "Camera IP address: "
read camera_ip_address </dev/tty
echo ""
if ask "Are you sure these are correct?"; then
skip_camera=0
echo ""
break
else
echo ""
continue
fi
done
else
bail "The camera password and IP address are required to configure the camera."
fi
}
function configure_opener() {
skip_camera=1
echo ""
echo "Configuring Pi Smart Garage Door Opener only..."
echo ""
}
function choose_installation_mode() {
echo ""
if ask "Do all steps automatically?" Y; then
return
else
interactive_mode=1
fi
}
function choose_installation_configuration() {
local reply
start_timer
echo "Choose an installation configuration:"
echo ""
echo "0: Configure Pi Smart Garage Door Opener and HiKam S6 Camera"
echo "1: Configure Pi Smart Garage Door Opener Only"
echo "q: Quit"
echo ""
while true; do
echo -n "0|1|q? "
read reply </dev/tty
case "$reply" in
0)
configure_opener_and_camera
break
;;
1)
configure_opener
break
;;
q) exit 1 ;;
esac
done
choose_installation_mode
}
function execute_step() {
if [ ${interactive_mode} == "1" ] && !( ask "Do this step?" Y ) ; then
echo ""
echo "Skipping..."
start_timer
return 1
fi
echo ""
start_timer
}
function update_package_database() {
infopart 1 "Updating package database..."
if execute_step ; then
echo "$ apt-get update"
echo ""
apt-get update
fi
}
function upgrade_installed_packages() {
infopart 2 "Upgrading installed packages..."
if execute_step ; then
echo "$ apt-get -y upgrade"
echo ""
apt-get -y upgrade
fi
}
function install_node_repo() {
infopart 3 "Installing NodeSource Node.js v8.x repository..."
if execute_step ; then
echo "$ curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -"
echo ""
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -
fi
}
function install_nodejs_and_npm() {
infopart 4 "Installing Node.js and npm..."
if execute_step ; then
echo "$ apt-get install -y nodejs"
echo ""
apt-get install -y nodejs
fi
}
function install_avahi() {
infopart 5 "Installing Avahi..."
if execute_step ; then
echo "$ apt-get install -y libavahi-compat-libdnssd-dev"
echo ""
apt-get install -y libavahi-compat-libdnssd-dev
fi
}
function install_ffmpeg() {
if [ ${skip_camera} == "1" ]; then
infopart 6 "No camera: Skipping ffmpeg installation..."
return
fi
infopart 6 "Installing ffmpeg..."
if execute_step ; then
echo "$ apt-get install -y ffmpeg"
echo ""
apt-get install -y ffmpeg
fi
}
function install_homebridge() {
infopart 7 "Installing Homebridge..."
if execute_step ; then
echo "$ npm install -g --unsafe-perm homebridge"
echo ""
npm install -g --unsafe-perm homebridge
fi
}
function install_homebridge_garage_door_opener_plugin() {
infopart 8 "Installing Homebridge garage door opener plugin..."
if execute_step ; then
echo "$ npm install -g homebridge-rasppi-gpio-garagedoor"
echo ""
npm install -g homebridge-rasppi-gpio-garagedoor
fi
}
function install_homebridge_camera_plugin() {
if [ ${skip_camera} == "1" ]; then
infopart 9 "No camera: Skipping Homebridge camera plugin installation..."
return
fi
infopart 9 "Installing Homebridge camera plugin..."
if execute_step ; then
echo "$ npm install -g homebridge-camera-ffmpeg"
echo ""
npm install -g homebridge-camera-ffmpeg
fi
}
function configure_homebridge() {
infopart 10 "Configuring Homebridge..."
if execute_step ; then
echo ""
if !([[ -d /var/lib/homebridge ]]) ; then
echo "Creating /var/lib/homebridge directory..."
echo "$ mkdir /var/lib/homebridge"
echo ""
mkdir /var/lib/homebridge
else
echo "/var/lib/homebridge directory already exists. Skipping mkdir."
fi
echo ""
echo "Copying homebridge service defaults..."
echo "$ cp ${dir}/homebridge /etc/default/"
echo ""
cp ${dir}/homebridge /etc/default/
echo ""
echo "Copying homebridge service configuration..."
echo "$ cp ${dir}/homebridge.service /etc/systemd/system/"
echo ""
cp ${dir}/homebridge.service /etc/systemd/system/
echo ""
echo "Copying script that enables Pi GPIO pins for the garage door opener..."
echo "$ cp ${dir}/garage-door-gpio /var/lib/homebridge/"
echo ""
cp ${dir}/garage-door-gpio /var/lib/homebridge/
echo ""
if [ ${skip_camera} == "1" ]; then
echo "Copying Homebridge configuration..."
echo "$ cp ${dir}/config-without-camera.json /var/lib/homebridge/config.json"
echo ""
cp ${dir}/config-without-camera.json /var/lib/homebridge/config.json
else
echo "Copying Homebridge configuration with camera settings..."
echo "$ cp ${dir}/config-with-camera.json /var/lib/homebridge/config.json"
echo ""
cp ${dir}/config-with-camera.json /var/lib/homebridge/config.json
echo ""
echo "Inserting camera password in Homebridge configuration..."
echo "$ sed -i \"s/PUT_CAMERA_PASSWORD_HERE/\"$camera_password\"/g\" \"/var/lib/homebridge/config.json\""
echo ""
sed -i "s/PUT_CAMERA_PASSWORD_HERE/"$camera_password"/g" "/var/lib/homebridge/config.json"
echo ""
echo "Inserting camera IP address in Homebridge configuration..."
echo "$ sed -i \"s/PUT_CAMERA_IP_ADDRESS_HERE/\"$camera_ip_address\"/g\" \"/var/lib/homebridge/config.json\""
echo ""
sed -i "s/PUT_CAMERA_IP_ADDRESS_HERE/"$camera_ip_address"/g" "/var/lib/homebridge/config.json"
fi
echo ""
if [ id -u homebridge &>/dev/null ]; then
echo "Adding homebridge system user..."
echo "$ useradd -M --system homebridge"
echo ""
useradd -M --system homebridge
else
echo "homebridge user already exists. Skipping useradd."
fi
echo ""
echo "Modifying permissions for the /var/lib/homebridge directory..."
echo "$ chmod -R 0777 /var/lib/homebridge"
echo ""
chmod -R 0777 /var/lib/homebridge
echo ""
echo "Reloading the systemd manager configuration..."
echo "$ systemctl daemon-reload"
echo ""
systemctl daemon-reload
echo ""
echo "Enabling the homebridge service..."
echo "$ systemctl enable homebridge"
echo ""
systemctl enable homebridge
echo ""
echo "Starting the homebridge service..."
echo "$ systemctl start homebridge"
echo ""
systemctl start homebridge
echo ""
echo "Checking the status of the homebridge service..."
echo "$ systemctl status homebridge"
echo ""
systemctl status homebridge
fi
stop_timer
show_elapsed_time 10
}
clear
hashline
echo " Geek Slant Smart Garage Installation Script "
echo " ${version}"
hashline
echo ""
choose_installation_configuration
update_package_database || bail "Failed to update package database."
upgrade_installed_packages || bail "Failed to upgrade installed packages."
install_node_repo || bail "Failed to install Node.js repository."
install_nodejs_and_npm || bail "Failed to install Node.js and npm."
install_avahi || bail "Failed to install Avahi."
install_ffmpeg || bail "Failed to install ffmpeg."
install_homebridge || bail "Failed to install Homebridge."
install_homebridge_garage_door_opener_plugin || bail "Failed to install Homebridge garage door opener plugin."
install_homebridge_camera_plugin || bail "Failed to install Homebridge camera plugin."
configure_homebridge || bail "Failed to configure Homebridge."
echo ""
hashline
echo "Installation complete"
hashline