-
Notifications
You must be signed in to change notification settings - Fork 7
/
ds4drv-install.sh
executable file
·279 lines (236 loc) · 7.17 KB
/
ds4drv-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
#!/bin/bash
#
# Description: ds4drv installer
#
# Company: Robotnik Automation S.L.L.
# Creation Year: 2021
# Author: Guillem Gari <[email protected]>
#
#
# Copyright (c) 2021, Robotnik Automation S.L.L.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the Robotnik Automation S.L.L. nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL Robotnik Automation S.L.L. BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#Colour
red_colour='\033[0;31m'
green_colour='\033[0;32m'
light_purple_colour='\033[1;35m'
err_colour="${red_colour}"
nfo_colour="${light_purple_colour}"
suc_colour="${green_colour}"
no_colour='\033[0m'
#root permision
err_str_root_permission="You need root privileges try:\nsudo ${0}"
#udev
udev_rule_file="55-ds4drv.rules"
udev_rule_destiny="/etc/udev/rules.d/${udev_rule_file}"
udev_rule_copy_command="cp ${udev_rule_file} ${udev_rule_destiny}"
udev_reload_rules_command="udevadm control --reload-rules && udevadm trigger"
suc_str_udev_install='udev rules active'
err_str_udev_copy="error copying udev rules ${udev_rule_file}"
err_str_udev_reload="error reloading udev rules"
nfo_str_udev_install='Installing udev rules'
# config
config_file="ds4drv.conf"
config_file_destiny="/etc/${config_file}"
config_copy_command="cp ${config_file} ${config_file_destiny}"
err_str_config_copy="error copying config file ${config_file}"
nfo_str_config_install="Configuring drivers"
#systemd
systemd_service_file="ds4drv.service"
systemd_service_destiny="/etc/systemd/system/${systemd_service_file}"
systemd_service_copy_command="cp ${systemd_service_file} ${systemd_service_destiny}"
systemd_daemon_reload_command="systemctl daemon-reload"
systemd_enable_service_command="systemctl enable ${systemd_service_file}"
systemd_start_service_command="systemctl start ${systemd_service_file}"
suc_str_systemd_install='systemd service active'
err_str_systemd_copy="error copying systemd service ${systemd_service_file}"
err_str_systemd_reload="error systemd reloading daemon"
err_str_systemd_enable="error enabling ${systemd_service_file} systemd service"
err_str_systemd_start="error starting ${systemd_service_file} systemd service"
nfo_str_systemd_install='installing service active'
suc_str_ds4drv_install='ds4drv installed'
ds4drv_search_python_command='which python'
ds4drv_search_python3_command='which python3'
ds4drv_search_pip_command='${python_bin} -m pip --version &>/dev/null'
ds4drv_pip_install_ds4dr_command='${python_bin} -m pip install ds4drv'
err_str_ds4drv_search_python='python not found'
err_str_ds4drv_search_pip='pip in not installed'
err_str_ds4drv_pip_install_ds4dr='could install ds4drv'
nfo_str_ds4drv_install='installing ds4drv'
function print_error() {
local message="${1}"
eval "echo -e "'"'"${err_colour}[ERROR]${no_colour}: ${message}"'"'" 2>&1"
}
function print_info() {
local message="${1}"
eval "echo -e "'"'"${nfo_colour}[INFO]${no_colour}: ${message}"'"'""
}
function print_success() {
local message="${1}"
eval "echo -e "'"'"${suc_colour}[SUCCESS]${no_colour}: ${message}"'"'""
}
function check_root_permission() {
if [[ "${EUID}" = 0 ]]; then
return 0
else
print_error "${err_str_root_permission}"
return 1
fi
}
function copy_udev_rules() {
if ! eval "${udev_rule_copy_command}"; then
print_error "${err_str_udev_copy}"
return 1
fi
return 0
}
function reload_udev_rules() {
if ! eval "${udev_reload_rules_command}"; then
print_error "${err_str_udev_reload}"
return 1
fi
return 0
}
function copy_config_file() {
if ! eval "${config_copy_command}"; then
print_error "${err_str_config_copy}"
fi
return 0
}
function copy_systemd_service() {
if ! eval "${systemd_service_copy_command}"; then
print_error "${err_str_systemd_copy}"
fi
return 0
}
function reload_systemd_daemons() {
if ! eval "${systemd_daemon_reload_command}"; then
print_error "${err_str_systemd_reload}"
fi
return 0
}
function enable_systemd_service() {
if ! eval "${systemd_enable_service_command}"; then
print_error "${err_str_systemd_enable}"
fi
return 0
}
function start_systemd_service() {
if ! eval "${systemd_start_service_command}"; then
print_error "${err_str_systemd_start}"
fi
return 0
}
function search_python3() {
if eval "${ds4drv_search_python3_command}"; then
python_bin="$(eval "${ds4drv_search_python3_command}")"
return 0
fi
if ! eval "${ds4drv_search_python_command}"; then
print_error "${err_str_ds4drv_search_python}"
return 1
fi
python_bin="$(eval "${ds4drv_search_python_command}")"
return 0
}
function search_pip() {
if ! eval "${ds4drv_search_pip_command}"; then
print_error "${err_str_ds4drv_search_pip}"
return 1
fi
}
function pip_install_ds4dr() {
if ! eval "${ds4drv_pip_install_ds4dr_command}"; then
print_error "${err_str_ds4drv_pip_install_ds4dr}"
return 1
fi
}
function install_ds4drv() {
print_info "${nfo_str_ds4drv_install}"
if ! search_python3; then
return 1
fi
if ! search_pip; then
return 1
fi
if ! pip_install_ds4dr; then
return 1
fi
print_success "${suc_str_ds4drv_install}"
return 0
}
function install_udev_rules() {
print_info "${nfo_str_udev_install}"
if ! copy_udev_rules; then
return 1
fi
if ! reload_udev_rules; then
return 1
fi
print_success "${suc_str_udev_install}"
return 0
}
function install_config() {
print_info "${nfo_str_config_install}"
if ! copy_config_file; then
return 1
fi
return 0
}
function install_systemd_service() {
print_info "${nfo_str_systemd_install}"
if ! copy_systemd_service; then
return 1
fi
if ! reload_systemd_daemons; then
return 1
fi
if ! enable_systemd_service; then
return 1
fi
if ! start_systemd_service; then
return 1
fi
print_success "${suc_str_systemd_install}"
return 0
}
function main() {
if ! check_root_permission; then
return 1
fi
if ! install_ds4drv; then
return 1
fi
if ! install_udev_rules; then
return 1
fi
if ! install_config; then
return 1
fi
if ! install_systemd_service; then
return 1
fi
}
main "${@}"
exit $?