forked from Koenkk/zigbee2mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzigbee2socat_installer.sh
181 lines (136 loc) · 4.24 KB
/
zigbee2socat_installer.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
#!/bin/bash
######################################
## INITIALIZE ##
######################################
set -o errexit -o pipefail -o noclobber -o nounset
! getopt --test > /dev/null
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
echo "Unfortunately getopt failed in this environment."
exit 2
fi
PROGRAM_NAME="zigbee2socat_installer"
VERSION="1.0"
######################################
## PARSE ARGUMENTS ##
######################################
OPTS="a:p:uvmhV"
LONG="addr:,port:,uninstall,verbose,man,help,version"
# Concern: possible to escape getopt and execute commands as root?
! PARSED=$(getopt -n $PROGRAM_NAME \
-o $OPTS \
-l $LONG \
-- "$@")
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
printf "Error parsing arguments. Try %s --help\n" "$PROGRAM_NAME"
exit 3
fi
eval set -- "$PARSED" # Use remaining arguments that weren't parsed
function handle_opts {
if [[ $# = 1 ]]; then
usage
fi
while true; do
case $1 in
-a|--addr)
ip=$2; shift 2; continue ;;
-p|--port)
port=$2; shift 2; continue ;;
-u|--uninstall)
uninstall ;;
-v|--verbose)
verbose=1; shift; continue ;;
-m|--man)
makemanual ;;
-h|--help)
usage ;;
-V|--version)
version ;;
--) # No more arguments to parse
shift; break ;;
*)
printf "Programming error! Option: %s\n" "$1"
exit 4 ;;
esac;done
}
######################################
## START OF MAIN ##
######################################
verbose=0
ip=""
port=""
function main {
handle_opts "$@"
# TODO: Check IP-address valid?
if [[ "$ip" = "" ]]; then
echo "IP-address not specified"
exit 5
fi
# Is the port valid?
if [[ $port -lt 1 || $port -gt 65535 ]]; then
printf "Port %s, is outside range: [1-65535]\n" "$port"
exit 6
fi
zigbee-socatvusb-install-package
}
######################################
## FUNCTIONS BELOW ##
######################################
function zigbee-socatvusb-install-package {
echo "Installing socat:"
sudo apt-get install socat
echo
echo "Make dir for zigbee vusb"
sudo mkdir -p /opt/zigbee2mqtt/vusb/ || die "Couldn't mkdir /opt/zigbee2mqtt/vusb/"
sudo chown -R pi:pi /opt/zigbee2mqtt/vusb/ || die "Couldn't chown /opt/zigbee2mqtt/vusb/"
echo "Creating service file zigbee-socatvusb.service"
service_path="/etc/systemd/system/zigbee-socatvusb.service"
[[ -f $service_path ]] && sudo rm $service_path
echo "[Unit]
Description=socat-vusb
After=network-online.target
[Service]
User=pi
ExecStart=/usr/bin/socat -d -d pty,raw,echo=0,link=/opt/zigbee2mqtt/vusb/zigbee_cc2530 tcp:$ip:$port,reuseaddr
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target" > $service_path || die "Couldn't create service /etc/systemd/system/zigbee-socatvusb.service"
sudo systemctl --system daemon-reload
echo "Installation is now complete"
echo
echo "Service can be started after configuration by running: sudo systemctl start zigbee-socatvusb"
}
function uninstall {
service_path="systemctl status socat-vusb.service"
[[ -f $service_path ]] && sudo rm $service_path
sudo systemctl --system daemon-reload
echo "Uninstalled successfully"
exit 0
}
function makemanual {
[[ -f "$PROGRAM_NAME.man" ]] && sudo rm $PROGRAM_NAME.man
help2man -N ./$PROGRAM_NAME.sh > $PROGRAM_NAME.man ; man ./$PROGRAM_NAME.man
exit 0
}
function usage {
echo -e "\
\rSetup for development version of Zigbee2Socat
-a, --addr IP Listen on this IP-address
-p, --port PORT Listen on this port
-u, --uninstall Uninstall zigbee-socatvusb.service
-v, --verbose Print more information
-m, --man Make and display manual
-h, --help Display this help message
\rOriginal concept by JFLN\
"
exit 0
}
function version {
echo "$PROGRAM_NAME $VERSION"
exit 0
}
function die {
printf "%s\n" "$1"
exit 1
}
main "$@" # Call main-function last