Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
rolandoislas
GitHub Repository: rolandoislas/drc-sim
Path: blob/master/install.sh
128 views
1
#!/usr/bin/env bash
2
# drc-sim(-backend): Wii U gamepad emulator.
3
#
4
# drc-sim-backend install script
5
# https://github.com/rolandoislas/drc-sim
6
#
7
# Changelog
8
#
9
# June 1, 2017 - 1.1
10
# Add output on error for make, cmake, and setup.py
11
# Add setup.py outputs to install.txt during install and it is read from for an uninstall
12
# Move init script, desktop launcher, and icon to setup.py
13
# Add version number
14
# Add pkg-info - wpa_supplicant compile fails without it
15
# Remove virtualenv
16
# June 1, 2017 - 1.1.1
17
# Fix Make, cmake, and setup.py not returning on errors
18
# Fix current directory not being restored on a git update failure
19
# June 2, 2017 - 1.1.2
20
# Detect and install to virtualenv
21
# June 3, 2017 - 1.1.3
22
# Use python3 from virtualenv if found
23
24
VERSION="1.1.3"
25
REPO_DRC_SIM="https://github.com/rolandoislas/drc-sim.git"
26
REPO_WPA_SUPPLICANT_DRC="https://github.com/rolandoislas/drc-hostap.git"
27
REPO_DRC_SIM_C="https://github.com/rolandoislas/drc-sim-c.git"
28
INSTALL_DIR="/opt/drc_sim/"
29
dependencies=()
30
branch_drc_sim=""
31
32
# Checks to see if OS has apt-get and sets dependencies
33
# Exits otherwise
34
check_os() {
35
if command -v apt-get &> /dev/null; then
36
echo "Command apt-get found."
37
# Backend dependencies
38
dependencies=("python3" "python3-pip"
39
"net-tools" "wireless-tools" "sysvinit-utils" "psmisc" "rfkill"
40
"isc-dhcp-client" "ifmetric" "python3-tk" "gksu")
41
# Wpa supplicant compile dependencies
42
dependencies+=("git" "libssl-dev" "libnl-genl-3-dev" "gcc" "make" "pkg-config")
43
# DRC Sim Server C++
44
dependencies+=("libavcodec-dev" "libswscale-dev" "libjpeg-dev" "cmake")
45
else
46
echo "The command apt-get was not found. This OS is not supported."
47
exit 1
48
fi
49
}
50
51
# Check to see if the script is running as root
52
# Exits if not root
53
check_root() {
54
if [[ ${EUID} -ne 0 ]]; then
55
echo "Install script must be executed with root privileges."
56
exit 1
57
fi
58
}
59
60
# Checks and installs pre-defined decencies array
61
# Exits on failed dependency
62
install_dependencies() {
63
echo "Installing dependencies."
64
for dependency in "${dependencies[@]}"
65
do
66
installed="$(dpkg -s ${dependency} 2>&1)"
67
if [[ ${installed} =~ "Status: install ok installed" ]]; then
68
echo "${dependency} [INSTALLED]"
69
else
70
echo "${dependency} [INSTALLING]"
71
if command apt-get -y install ${dependency} &> /dev/null; then
72
echo "${dependency} [INSTALLED]"
73
else
74
echo "${dependency} [FAILED]"
75
exit 1
76
fi
77
fi
78
done
79
}
80
81
# Update git directory while stashing changed return 1
82
# Returns 1 on failure
83
update_git() {
84
cur_dir="${PWD}"
85
cd "${1}" &> /dev/null || return 1
86
if [[ -d "${1}" ]]; then
87
echo "Found existing git directory ${1}"
88
if command git stash --include-untracked &> /dev/null; then
89
echo "Stashed git changes"
90
echo "Updating git repo"
91
if command git pull &> /dev/null; then
92
echo "Updated git repo"
93
else
94
cd "${cur_dir}" &> /dev/null || return 1
95
return 1
96
fi
97
else
98
cd "${cur_dir}" &> /dev/null || return 1
99
return 1
100
fi
101
fi
102
cd "${cur_dir}" &> /dev/null || return 1
103
return 0
104
}
105
106
# Clones a git repo to the install path
107
# If the directory exists it is removed
108
# Param $1: git repo url
109
get_git() {
110
git_dir="${INSTALL_DIR}${2}"
111
if update_git ${git_dir}; then
112
return 0
113
else
114
# Remove directory for a clean clone
115
if [[ -d "${git_dir}" ]]; then
116
rm -rf "${git_dir}"
117
fi
118
fi
119
# Clone
120
echo "Cloning ${1} into ${git_dir}"
121
if command git clone ${1} ${git_dir} &> /dev/null; then
122
echo "Cloned ${1}"
123
else
124
echo "Failed to clone ${1}"
125
exit 1
126
fi
127
}
128
129
# Compiles wpa_supplicant after fetching it from git
130
compile_wpa() {
131
get_git ${REPO_WPA_SUPPLICANT_DRC} "wpa"
132
echo "Compiling wpa_supplicant_drc"
133
compile_dir="${INSTALL_DIR}wpa/wpa_supplicant/"
134
cur_dir="${PWD}"
135
cd "${compile_dir}" &> /dev/null || return 1
136
cp ../conf/wpa_supplicant.config ./.config &> /dev/null || return 1
137
compile_log="${compile_dir}make.log"
138
echo "Compile log at ${compile_log}"
139
if ! make &> ${compile_log}; then cat "${compile_log}"; return 1; fi
140
echo "Installing wpa_supplicant_drc and wpa_cli_drc to /usr/local/bin"
141
cp wpa_supplicant /usr/local/bin/wpa_supplicant_drc &> /dev/null || return 1
142
cp wpa_cli /usr/local/bin/wpa_cli_drc &> /dev/null || return 1
143
cd "${cur_dir}" &> /dev/null || return 1
144
return 0
145
}
146
147
# Compiles drc_sim_c after fetching it from git
148
compile_drc_sim_c() {
149
get_git ${REPO_DRC_SIM_C} "drc_sim_c"
150
echo "Compiling drc_sim_c"
151
compile_dir="${INSTALL_DIR}drc_sim_c/"
152
cur_dir="${PWD}"
153
cd "${compile_dir}" &> /dev/null || return 1
154
compile_log="${compile_dir}make.log"
155
cmake_log="${compile_dir}cmake.log"
156
echo "Compile log at ${compile_log}"
157
if ! cmake "${compile_dir}" &> "${cmake_log}"; then cat "${cmake_log}"; return 1; fi
158
if ! make &> "${compile_log}"; then cat "${compile_log}"; return 1; fi
159
echo "Installing drc_sim_c to /usr/local/bin"
160
make install &> /dev/null || return 1
161
cd "${cur_dir}" &> /dev/null || return 1
162
return 0
163
}
164
165
# Installs drc-sim in a virtualenv
166
install_drc_sim() {
167
echo "Installing DRC Sim Server GUI/CLI Utility"
168
# Paths
169
drc_dir="${INSTALL_DIR}drc/"
170
cur_dir="${PWD}"
171
# Fix virtualenv paths
172
prefix=""
173
python="python3"
174
if [ ! -z "${VIRTUAL_ENV}" ]; then
175
echo "Installing into virtualenv: ${VIRTUAL_ENV}"
176
prefix="--prefix ${VIRTUAL_ENV}"
177
python="${VIRTUAL_ENV}/bin/${python}"
178
fi
179
# Get source
180
if [[ "${branch_drc_sim}" != "local" ]]; then
181
# Get repo
182
get_git ${REPO_DRC_SIM} "drc"
183
else
184
# Copy local
185
if [[ ! -f "${cur_dir}/setup.py" ]]; then
186
echo "Cannot perform local install. Missing source files at ${cur_dir}."
187
return 1
188
fi
189
if [[ ! -d "${INSTALL_DIR}" ]]; then
190
mkdir "${INSTALL_DIR}" &> /dev/null || return 1
191
fi
192
rm -rf ${drc_dir} &> /dev/null || return 1
193
mkdir ${drc_dir} &> /dev/null || return 1
194
cp -R "${cur_dir}/." "${drc_dir%/*}" &> /dev/null || return 1
195
fi
196
# Install python dependencies
197
echo "Installing setuptools"
198
${python} -m pip install setuptools &> /dev/null || return 1
199
# Remove an existing install of drc-sim
200
echo "Attempting to remove previous installations"
201
${python} -m pip uninstall -y drcsim &> /dev/null || \
202
echo "Failed to remove the previous installation. Attempting to install anyway."
203
# Set the directory
204
cd "${drc_dir}" &> /dev/null || return 1
205
# Branch to checkout
206
if [[ "${branch_drc_sim}" != "local" ]]; then
207
echo "Using branch \"${branch_drc_sim}\" for drc-sim install"
208
git checkout ${branch_drc_sim} &> /dev/null || return 1
209
else
210
echo "Using current directory as install source"
211
fi
212
# Install
213
echo "Installing drc-sim"
214
echo "Downloading Python packages. This may take a while."
215
if ! ${python} "${drc_dir}setup.py" install ${prefix} --record "${drc_dir}/install.txt" &> \
216
"/tmp/drc-sim-py-install.log"; then
217
cat "/tmp/drc-sim-py-install.log"
218
return 1
219
fi
220
cd "${cur_dir}" &> /dev/null || return 1
221
# Update icon cache
222
update-icon-caches /usr/share/icons/* &> /dev/null || echo "Failed to update icon cache."
223
}
224
225
# Echos the general info
226
print_info() {
227
echo "Drc-sim installer (script version ${VERSION})"
228
printf "\thttps://github.com/rolandoislas/drc-sim\n"
229
}
230
231
# Uninstalls DRC Sim then exists
232
uninstall() {
233
drc_install_log="${INSTALL_DIR}drc/install.txt"
234
echo "Uninstalling DRC Sim Server"
235
# Remove setup.py files
236
if [[ -f "${drc_install_log}" ]]; then
237
echo "Files to remove:"
238
cat ${drc_install_log}
239
read -p "Remove these files? [Y/N]" reponse
240
if [[ ${reponse} =~ [Yy](es)* ]]; then
241
tr '\n' '\0' < ${drc_install_log} | xargs -0 sudo rm -f --
242
echo "Removed Python installed files"
243
else
244
echo "Not removing Python installed files"
245
echo "Install canceled"
246
exit 2
247
fi
248
else
249
echo "Could not clean Python installed files. Missing ${drc_install_log}"
250
fi
251
# Launcher (.desktop)
252
to_remove=("/usr/share/applications/drc-sim-backend.desktop" "/usr/share/applications/drcsimbackend.desktop"
253
"/usr/share/icons/hicolor/512x512/apps/drcsimbackend.png")
254
for item in "${to_remove[@]}"; do
255
if [[ -f "${item}" ]]; then
256
rm -f ${item} &> /dev/null
257
fi
258
done
259
# Install dir
260
echo "Removing install directory"
261
rm -rf ${INSTALL_DIR} &> /dev/null || echo "Failed to remove install directory."
262
# TODO uninstall packages
263
printf "\nNOT removing package dependencies\n"
264
printf "${dependencies[*]}\n\n"
265
# Done
266
echo "Uninstalled DRC Sim Server"
267
exit 0
268
}
269
270
# Parses args
271
check_args() {
272
branch_drc_sim=${1:-master}
273
# Help
274
if [[ "${1}" == "help" ]] || [[ "${1}" == "-h" ]]; then
275
echo "Usage: <install.sh> [argument]"
276
echo " Defaults to install."
277
echo " Arguments:"
278
echo " -h, help : help menu"
279
echo " branch : branch to use for drc-sim (master, develop, local) master is used by default"
280
echo " uninstall : uninstall DRC Sim"
281
exit 1
282
# Uninstall
283
elif [[ "${1}" == "uninstall" ]]; then
284
uninstall
285
# Install branch
286
elif [[ "${branch_drc_sim}" != "develop" ]] && [[ "${branch_drc_sim}" != "master" ]] &&
287
[[ "${branch_drc_sim}" != "local" ]]; then
288
echo "Invalid branch \"${1}\""
289
check_args "help"
290
fi
291
}
292
293
# Check if command return value is non-zero and exit with message.
294
# If the command exited with a zero exit value the success message will be echoed
295
pass_fail() {
296
if $1; then
297
echo $2
298
else
299
echo $3
300
exit 1
301
fi
302
}
303
304
# Echo post install message and exit
305
post_install() {
306
echo "Install finished"
307
echo "\"DRC SIM Server\" will now appear in GUI application menus."
308
echo "It can also be launched via \"drc-sim-backend\"."
309
exit 0
310
}
311
312
# Install drc_sim
313
install() {
314
install_dependencies
315
pass_fail compile_wpa "Compiled wpa_supplicant" "Failed to compile wpa_supplicant"
316
pass_fail compile_drc_sim_c "Compiled drc_sim_c" "Failed to compile drc_sim_c"
317
pass_fail install_drc_sim "Installed drc-sim" "Failed to install drc-sim"
318
post_install
319
}
320
321
main() {
322
print_info
323
check_root
324
check_os
325
check_args "$@"
326
install
327
}
328
329
330
main "$@"
331
332