Skip to content

Commit 0090095

Browse files
JLFNKoenkk
authored andcommitted
Zigbee2Socat installer (Koenkk#1097)
* Zigbee2Socat Zigbee2Socat installer -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 * All in one installer * Update installer.sh * Delete installer.sh * Rename to install * Rename zigbee2socat_install.sh to scripts/zigbee2socat_installer.sh
1 parent 11a6e36 commit 0090095

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed

scripts/zigbee2socat_installer.sh

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
#!/bin/bash
2+
3+
######################################
4+
## INITIALIZE ##
5+
######################################
6+
7+
set -o errexit -o pipefail -o noclobber -o nounset
8+
9+
! getopt --test > /dev/null
10+
if [[ ${PIPESTATUS[0]} -ne 4 ]]; then
11+
echo "Unfortunately getopt failed in this environment."
12+
exit 2
13+
fi
14+
15+
16+
PROGRAM_NAME="zigbee2socat_installer"
17+
VERSION="1.0"
18+
19+
20+
######################################
21+
## PARSE ARGUMENTS ##
22+
######################################
23+
24+
OPTS="a:p:uvmhV"
25+
LONG="addr:,port:,uninstall,verbose,man,help,version"
26+
27+
# Concern: possible to escape getopt and execute commands as root?
28+
! PARSED=$(getopt -n $PROGRAM_NAME \
29+
-o $OPTS \
30+
-l $LONG \
31+
-- "$@")
32+
if [[ ${PIPESTATUS[0]} -ne 0 ]]; then
33+
printf "Error parsing arguments. Try %s --help\n" "$PROGRAM_NAME"
34+
exit 3
35+
fi
36+
eval set -- "$PARSED" # Use remaining arguments that weren't parsed
37+
38+
39+
function handle_opts {
40+
if [[ $# = 1 ]]; then
41+
usage
42+
fi
43+
while true; do
44+
case $1 in
45+
-a|--addr)
46+
ip=$2; shift 2; continue ;;
47+
48+
-p|--port)
49+
port=$2; shift 2; continue ;;
50+
51+
-u|--uninstall)
52+
uninstall ;;
53+
54+
-v|--verbose)
55+
verbose=1; shift; continue ;;
56+
57+
-m|--man)
58+
makemanual ;;
59+
60+
-h|--help)
61+
usage ;;
62+
63+
-V|--version)
64+
version ;;
65+
66+
--) # No more arguments to parse
67+
shift; break ;;
68+
69+
*)
70+
printf "Programming error! Option: %s\n" "$1"
71+
exit 4 ;;
72+
esac;done
73+
}
74+
75+
76+
######################################
77+
## START OF MAIN ##
78+
######################################
79+
80+
verbose=0
81+
ip=""
82+
port=""
83+
84+
function main {
85+
handle_opts "$@"
86+
87+
# TODO: Check IP-address valid?
88+
if [[ "$ip" = "" ]]; then
89+
echo "IP-address not specified"
90+
exit 5
91+
fi
92+
93+
# Is the port valid?
94+
if [[ $port -lt 1 || $port -gt 65535 ]]; then
95+
printf "Port %s, is outside range: [1-65535]\n" "$port"
96+
exit 6
97+
fi
98+
99+
zigbee-socatvusb-install-package
100+
}
101+
102+
103+
######################################
104+
## FUNCTIONS BELOW ##
105+
######################################
106+
107+
function zigbee-socatvusb-install-package {
108+
echo "Installing socat:"
109+
sudo apt-get install socat
110+
echo
111+
112+
echo "Make dir for zigbee vusb"
113+
sudo mkdir -p /opt/zigbee2mqtt/vusb/ || die "Couldn't mkdir /opt/zigbee2mqtt/vusb/"
114+
sudo chown -R pi:pi /opt/zigbee2mqtt/vusb/ || die "Couldn't chown /opt/zigbee2mqtt/vusb/"
115+
116+
echo "Creating service file zigbee-socatvusb.service"
117+
service_path="/etc/systemd/system/zigbee-socatvusb.service"
118+
119+
[[ -f $service_path ]] && sudo rm $service_path
120+
echo "[Unit]
121+
Description=socat-vusb
122+
After=network-online.target
123+
124+
[Service]
125+
User=pi
126+
ExecStart=/usr/bin/socat -d -d pty,raw,echo=0,link=/opt/zigbee2mqtt/vusb/zigbee_cc2530 tcp:$ip:$port,reuseaddr
127+
Restart=always
128+
RestartSec=10
129+
130+
[Install]
131+
WantedBy=multi-user.target" > $service_path || die "Couldn't create service /etc/systemd/system/zigbee-socatvusb.service"
132+
133+
sudo systemctl --system daemon-reload
134+
135+
echo "Installation is now complete"
136+
echo
137+
echo "Service can be started after configuration by running: sudo systemctl start zigbee-socatvusb"
138+
}
139+
140+
function uninstall {
141+
service_path="systemctl status socat-vusb.service"
142+
[[ -f $service_path ]] && sudo rm $service_path
143+
sudo systemctl --system daemon-reload
144+
echo "Uninstalled successfully"
145+
exit 0
146+
}
147+
148+
function makemanual {
149+
[[ -f "$PROGRAM_NAME.man" ]] && sudo rm $PROGRAM_NAME.man
150+
help2man -N ./$PROGRAM_NAME.sh > $PROGRAM_NAME.man ; man ./$PROGRAM_NAME.man
151+
exit 0
152+
}
153+
154+
function usage {
155+
echo -e "\
156+
\rSetup for development version of Zigbee2Socat
157+
158+
-a, --addr IP Listen on this IP-address
159+
-p, --port PORT Listen on this port
160+
-u, --uninstall Uninstall zigbee-socatvusb.service
161+
-v, --verbose Print more information
162+
-m, --man Make and display manual
163+
-h, --help Display this help message
164+
165+
\rOriginal concept by JFLN\
166+
"
167+
exit 0
168+
}
169+
170+
function version {
171+
echo "$PROGRAM_NAME $VERSION"
172+
exit 0
173+
}
174+
175+
function die {
176+
printf "%s\n" "$1"
177+
exit 1
178+
}
179+
180+
181+
main "$@" # Call main-function last

0 commit comments

Comments
 (0)