forked from doctorfree/RoonCommandLine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linInstall
executable file
·494 lines (457 loc) · 16.4 KB
/
linInstall
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/bash
#
# shellcheck disable=SC1090,SC2220,SC2086,SC2207,SC2001,SC2068
PKG="RoonCommandLine"
TOP="/usr"
DESTDIR="${TOP}/local"
ROON="${DESTDIR}/Roon"
PKG_NAME="RoonCommandLine"
ROONETC=${ROON}/etc
ROON_INI=${ROONETC}/roon_api.ini
ROONCONF=${ROONETC}/pyroonconf
TMP_ROON_API="/tmp/_roon_api_ini_.save"
export SUDO=sudo
BOLD=$(tput bold 2>/dev/null)
NORMAL=$(tput sgr0 2>/dev/null)
export PATH=$PATH:/usr/local/bin
if [ -x /home/linuxbrew/.linuxbrew/bin/brew ]; then
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
else
[ -x /usr/local/bin/brew ] && eval "$(/usr/local/bin/brew shellenv)"
fi
PYTHON=
python3_inst=$(type -p python3)
if [ "${python3_inst}" ]; then
PYTHON="python3"
else
python_inst=$(type -p python)
if [ "${python_inst}" ]; then
[[ "$(python --version)" =~ "Python 3" ]] && PYTHON="python"
fi
fi
[ "${PYTHON}" ] || {
echo "RoonCommandLine requires Python 3 and Pip"
echo "Install Python 3 and restart this installation"
echo "See https://docs.python-guide.org/starting/install3/linux/"
echo "for step by step instructions to install Python 3 on Linux"
exit 1
}
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)
[ -f "${SCRIPT_DIR}/VERSION" ] || {
echo "$SCRIPT_DIR/VERSION does not exist. Exiting."
exit 1
}
. "${SCRIPT_DIR}/VERSION"
version=${VERSION}
release=${RELEASE}
while true; do
read -r -p "Install ${PKG_NAME} version ${version}r${release} ? ('Y'/'N'): " yn
case $yn in
[Yy]*)
break
;;
[Nn]*)
exit 0
;;
*)
echo "Please answer yes or no."
;;
esac
done
[ -d /usr/local ] || ${SUDO} mkdir /usr/local
[ -d /usr/local/bin ] || ${SUDO} mkdir /usr/local/bin
[ -d ${ROON} ] || ${SUDO} mkdir ${ROON}
[ -d ${DESTDIR}/share ] || ${SUDO} mkdir ${DESTDIR}/share
[ -d ${DESTDIR}/share/applications ] || ${SUDO} mkdir ${DESTDIR}/share/applications
[ -d ${DESTDIR}/share/doc ] || ${SUDO} mkdir ${DESTDIR}/share/doc
[ -d ${DESTDIR}/share/doc/${PKG} ] || ${SUDO} mkdir ${DESTDIR}/share/doc/${PKG}
# Install the Python Roon API
# if pip list | grep roonapi >/dev/null; then
# ${SUDO} ${PYTHON} -m pip install --upgrade roonapi >/dev/null 2>&1
# else
# ${SUDO} ${PYTHON} -m pip install roonapi >/dev/null 2>&1
# fi
# ${SUDO} ${PYTHON} -m pip install rich-cli >/dev/null 2>&1
${SUDO} ${PYTHON} -m venv ${ROON}/venv
[ -x ${ROON}/venv/bin/python ] && export PYTHON=${ROON}/venv/bin/python
${SUDO} ${PYTHON} -m pip install roonapi
${SUDO} ${PYTHON} -m pip install rich-cli
[ -f /usr/local/Roon/etc/roon_api.ini ] && {
${SUDO} cp /usr/local/Roon/etc/roon_api.ini ${TMP_ROON_API}
}
for dir in bin etc api patches; do
[ -d ${ROON}/${dir} ] && ${SUDO} rm -rf ${ROON}/${dir}
done
${SUDO} cp -a api ${ROON}/api
${SUDO} cp -a bin ${ROON}/bin
${SUDO} cp -a etc ${ROON}/etc
${SUDO} cp -a patches ${ROON}/patches
${SUDO} cp ./*.desktop "${DESTDIR}/share/applications"
${SUDO} cp AUTHORS ${DESTDIR}/share/doc/${PKG}/AUTHORS
${SUDO} cp LICENSE ${DESTDIR}/share/doc/${PKG}/copyright
${SUDO} cp CHANGELOG.md ${DESTDIR}/share/doc/${PKG}/changelog
${SUDO} cp README.md ${DESTDIR}/share/doc/${PKG}/README
${SUDO} cp VERSION ${DESTDIR}/share/doc/${PKG}/VERSION
${SUDO} gzip -9 ${DESTDIR}/share/doc/${PKG}/changelog
${SUDO} chmod 644 man/*/*
${SUDO} chmod 755 man man/man*
for mandir in man/man*; do
[ "${mandir}" == "man/man*" ] && continue
[ -d ${DESTDIR}/share/"${mandir}" ] || {
${SUDO} mkdir -p ${DESTDIR}/share/"${mandir}"
}
${SUDO} cp -p "${mandir}"/* ${DESTDIR}/share/"${mandir}"
done
[ -f .gitignore ] && {
while read -r ignore; do
${SUDO} rm -f ${ROON}/"${ignore}"
done <.gitignore
}
${SUDO} chmod 644 ${ROON}/etc/* \
${ROON}/api/* \
${ROON}/patches/*.md ${ROON}/patches/*.patch \
${ROON}/patches/roonapi/* \
${ROON}/patches/roon-web-controller/*
${SUDO} chmod 755 ${ROON}/bin/* \
${ROON}/bin \
${ROON}/etc/roon_faded \
${ROON}/etc \
${ROON}/api \
${ROON}/patches \
${ROON}/patches/mk* \
${ROON}/patches/roonapi \
${ROON}/patches/roon-web-controller
${SUDO} chown -R root:root ${ROON}
# ${SUDO} chown -R root:root ${DESTDIR}
# Try to configure the roon script with the IP and username
IP=$(hostname -I | awk ' { print $1 } ')
USER=$(id -u -n)
cd /usr/local/bin || exit 1
for command in "${ROON}"/bin/*; do
b=$(basename "${command}")
if [ -f "${b}" ]; then
diff "${command}" "${b}" >/dev/null && {
${SUDO} rm -f "${b}"
${SUDO} ln -s "${command}" .
}
else
${SUDO} ln -s "${command}" .
fi
done
# Copy in distributed roon_api.ini template if no previous one exists
[ -f ${ROON_INI} ] || {
# Restore previously saved roon_api.ini if one exists
if [ -f ${TMP_ROON_API} ]; then
if grep defaultalbum "${TMP_ROON_API}" >/dev/null; then
${SUDO} cp ${ROONETC}/roon_api.ini-dist ${ROON_INI}
else
${SUDO} cp ${TMP_ROON_API} ${ROON_INI}
fi
${SUDO} rm -f ${TMP_ROON_API}
else
${SUDO} cp ${ROONETC}/roon_api.ini-dist ${ROON_INI}
fi
}
EX_CORE_IP=$(cat ${ROON_INI} | grep RoonCoreIP | awk -F '=' ' { print $2 } ')
EX_CORE_IP="$(echo -e "${EX_CORE_IP}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
EX_CORE_PORT=$(cat ${ROON_INI} | grep RoonCorePort | awk -F '=' ' { print $2 } ')
EX_CORE_PORT="$(echo -e "${EX_CORE_PORT}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
updatever=$(grep RoonCommandLineVersion ${ROON_INI})
if [ "${updatever}" ]; then
cat ${ROON_INI} | sed -e "s/RoonCommandLineVersion.*/RoonCommandLineVersion = ${version}/" >/tmp/v$$
else
cat ${ROON_INI} | sed -e "/RoonCorePort.*/a RoonCommandLineVersion = __VERSION__" >/tmp/v$$
fi
${SUDO} cp /tmp/v$$ ${ROON_INI}
${SUDO} rm -f /tmp/v$$
updaterel=$(grep RoonCommandLineRelease ${ROON_INI})
if [ "${updaterel}" ]; then
cat ${ROON_INI} | sed -e "s/RoonCommandLineRelease.*/RoonCommandLineRelease = ${release}/" >/tmp/r$$
else
cat ${ROON_INI} | sed -e "/RoonCommandLineVersion.*/a RoonCommandLineRelease = __RELEASE__" >/tmp/r$$
fi
${SUDO} cp /tmp/r$$ ${ROON_INI}
${SUDO} rm -f /tmp/r$$
users=()
numusers=0
for homedir in /home/*; do
[ "${homedir}" == "/home/*" ] && continue
[ -d "${homedir}" ] && {
username=$(basename "${homedir}")
exists=$(id -g -n "${username}")
[ "${exists}" ] && {
users+=("${username}")
numusers=$((numusers + 1))
}
}
done
[ ${numusers} -gt 1 ] && {
while true; do
read -r -p "Using ${USER} as the Roon user. 'Y' for OK, 'N' to select a different user: " yn
case $yn in
[Nn]*)
PS3="${BOLD}Please enter Roon user (numeric or text): ${NORMAL}"
users+=("None")
select opt in "${users[@]}"; do
case "$opt,$REPLY" in
"None",* | *,"None")
break
;;
*)
[ -d /home/"${opt}" ] && {
USER="${opt}"
break
}
printf "\nInvalid entry. Please try again"
printf "\nEnter either a number or text of one of the menu entries\n"
;;
esac
done
break
;;
[Yy]*)
break
;;
*)
echo "Please answer yes or no."
;;
esac
done
}
echo "Setting the Python Roon API server IP address to $IP"
echo "Enabling local access in ${ROONCONF}"
if [ -f ${ROONCONF} ]; then
grep -v ^LOCAL= ${ROONCONF} >/tmp/roon$$
echo "LOCAL=true" >>/tmp/roon$$
else
echo "LOCAL=true" >/tmp/roon$$
fi
${SUDO} cp /tmp/roon$$ ${ROONCONF}
${SUDO} rm -f /tmp/roon$$
cat ${ROON}/bin/roon | sed -e "s/XX.X.X.XXX/$IP/" -e "s/SSH_USERNAME/$USER/" >/tmp/roon$$
${SUDO} cp ${ROON}/bin/roon ${ROON}/bin/roon.orig
${SUDO} cp /tmp/roon$$ ${ROON}/bin/roon
${SUDO} rm -f /tmp/roon$$
PYTHONUSERBASE=
# Check the global site directories
SITES=($(${PYTHON} -c 'import site; print(site.getsitepackages())' | tr -d '[],'))
for site in ${SITES[@]}; do
site=$(echo $site | sed -e "s/'//g")
site_sep=
echo ${site} | grep site-packages >/dev/null && site_sep="site-packages"
[ "${site_sep}" ] || site_sep="dist-packages"
[ -d "${site}/roonapi" ] && {
PYTHONUSERBASE=$(echo ${site} | awk -F "/lib/" ' { print $1 } ')
PYTHON_SITEDIR=$(echo ${site} | awk -F "/${site_sep}" ' { print $1 } ')
break
}
done
if [ "${PYTHONUSERBASE}" ]; then
if [ -f ${ROONCONF} ]; then
grep PYTHONUSERBASE ${ROONCONF} >/dev/null || {
echo "export PYTHONUSERBASE=${PYTHONUSERBASE}" | ${SUDO} tee -a ${ROONCONF} >/dev/null
}
else
echo "export PYTHONUSERBASE=${PYTHONUSERBASE}" | ${SUDO} tee ${ROONCONF} >/dev/null
fi
. ${ROONCONF}
RVER=$(${PYTHON} -m pip show roonapi | grep Version | awk -F ':' ' { print $2 } ')
# Remove leading and trailing spaces in RVER
RVER="$(echo -e "${RVER}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Only patch Roonapi version 0.0.38 and earlier
LESSER=$(echo "${RVER} 0.0.38" | tr " " "\n" | sort -V | head -n 1)
if [ "${LESSER}" != "${RVER}" ]; then
# Mark it as having been patched
grep -v ROONAPIPATCHED ${ROONCONF} >/tmp/roonconf$$
echo "ROONAPIPATCHED=true" >>/tmp/roonconf$$
${SUDO} cp /tmp/roonconf$$ ${ROONCONF}
${SUDO} rm -f /tmp/roonconf$$
else
# Apply the Python Roon API patch if it has not already been applied
if [ "${ROONAPIPATCHED}" = true ]; then
echo "Python Roon API already patched. Skipping patch."
else
# Locate the patch file
if [ "${site_sep}" == "site-packages" ]; then
ROONAPI_PATCH=roonapi-listmedia-site.patch
else
ROONAPI_PATCH=roonapi-listmedia.patch
fi
patchfile=
if [ -f ${ROON}/patches/${ROONAPI_PATCH} ]; then
patchfile=${ROON}/patches/${ROONAPI_PATCH}
else
echo "Cannot locate patch file ${ROONAPI_PATCH}"
echo "Python Roon API patch not applied."
echo "List commands will not function properly."
fi
[ "${patchfile}" ] && {
patch_inst=$(type -p patch)
if [ "$patch_inst" ]; then
cd ${PYTHON_SITEDIR} || exit 1
${SUDO} patch -b -p0 <${patchfile}
grep -v ROONAPIPATCHED ${ROONCONF} >/tmp/roonconf$$
echo "ROONAPIPATCHED=true" >>/tmp/roonconf$$
${SUDO} cp /tmp/roonconf$$ ${ROONCONF}
${SUDO} rm -f /tmp/roonconf$$
else
echo "Cannot locate the patch utility. Either patch is not installed"
echo "or it is not in your execution PATH."
echo ""
echo "Skipping the patch for the Python Roon API."
echo "Listing of Roon library media will not work without this patch."
fi
}
fi
fi
else
echo "Could not locate the roonapi Python module installation directory"
echo "Python Roon API patch not applied."
echo "List commands will not function properly."
fi
[ "${USER}" ] && {
GROUP=$(id -g -n "${USER}")
${SUDO} chown -R "${USER}":"${GROUP}" ${ROONETC}
}
echo ""
echo "In order to configure the Python Roon API we must set the IP address"
echo "of the Roon Core. Discovery will be used to determine the Roon Core IP."
echo "When prompted for authorization, go to a Roon Remote window and click"
echo " Settings -> Extensions -> Enable"
echo "to authorize discovery"
echo ""
# Get and set the Roon Core IP address
${ROON}/bin/get_core_ip 2>&1 | tee /tmp/discover$$
CORE_IP=$(cat /tmp/discover$$ | grep RoonCoreIP | awk -F '=' ' { print $2 } ')
CORE_IP="$(echo -e "${CORE_IP}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
CORE_PORT=$(cat /tmp/discover$$ | grep RoonCorePort | awk -F '=' ' { print $2 } ')
CORE_PORT="$(echo -e "${CORE_PORT}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Could use Python (or better, yq) to modify roon_api.ini something like:
# python -c 'import yaml;f=open("roon_api.ini");y=yaml.safe_load(f);y["DEFAULT"]["RoonCoreIP"] = ${CORE_IP}; print(yaml.dump(y, default_flow_style=False, sort_keys=False))'
# But for now we use sed
cat ${ROON_INI} | sed -e "s/${EX_CORE_IP}/${CORE_IP}/" -e "s/${EX_CORE_PORT}/${CORE_PORT}/" >/tmp/core$$
${SUDO} cp /tmp/core$$ ${ROON_INI}
${SUDO} rm -f /tmp/core$$ /tmp/discover$$
# Get and set the default zone and initial zone groupings
# Attempting to avoid grouping incompatible zones is somewhat convoluted
defaultZone=
groupOne=
groupTwo=
groupRee=
groupFor=
zones=$(${ROON}/bin/get_zones)
numZones=$(echo "${zones}" | awk -F ',' ' { print NF } ')
numWith=0
[ ${numZones} -gt 0 ] && {
defaultZone=$(echo "${zones}" | awk -F ',' ' { print $1 } ')
withDefaultZone=$(${ROON}/bin/get_zone_info -l -z "${defaultZone}")
numWith=$(echo "${withDefaultZone}" | awk -F ',' ' { print NF } ')
}
[ ${numZones} -gt 2 ] && {
[ ${numWith} -gt 0 ] && {
groupOne=$(echo "${defaultZone},${withDefaultZone}" | sed -e "s/,/, /g" -e "s/:/,/")
# Find a zone not in groupOne and use it for the second zone group
zoneTwo=
numWithTwo=0
zonearray=()
arrayone=()
unset saved_IFS
[ -n "${IFS+set}" ] && saved_IFS=$IFS
IFS=","
for i in ${zones}; do
thiszone="$(echo -e "$i" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
zonearray+=("${thiszone}")
done
for i in ${groupOne}; do
thisgroup="$(echo -e "$i" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
arrayone+=("${thisgroup}")
done
Array3=()
for zone in ${zonearray[@]}; do
for one in ${arrayone[@]}; do
[ "${zone}" == "${one}" ] && continue 2
done
Array3+=("${zone}")
done
zoneTwo=${Array3[0]}
[ "${zoneTwo}" ] || zoneTwo=${arrayone[1]}
withZoneTwo=$(${ROON}/bin/get_zone_info -l -z "${zoneTwo}")
numWithTwo=$(echo "${withZoneTwo}" | awk -F ',' ' { print NF } ')
[ ${numWithTwo} -gt 0 ] && {
groupTwo=$(echo "${zoneTwo},${withZoneTwo}" | sed -e "s/${defaultZone},//" -e "s/,/, /g" -e "s/:/,/")
}
unset IFS
[ -n "${saved_IFS+set}" ] && {
IFS=$saved_IFS
unset saved_IFS
}
}
}
[ ${numZones} -gt 3 ] && {
secondZone=$(echo "${zones}" | awk -F ',' ' { print $2 } ' | sed -e "s/^ //")
withSecondZone=$(${ROON}/bin/get_zone_info -l -z "${secondZone}")
withSecondZone=$(echo ${withSecondZone} | sed -e "s/${defaultZone},//")
numWith=$(echo "${withSecondZone}" | awk -F ',' ' { print NF } ')
if [ ${numWith} -gt 1 ]; then
withDefOne=$(echo "${withSecondZone}" | awk -F ',' ' { print $1 ", " $2 } ')
groupRee="${secondZone}, ${withDefOne}"
else
[ ${numWith} -gt 0 ] && {
withDefOne=$(echo "${withSecondZone}" | awk -F ',' ' { print $1 } ')
groupRee="${secondZone}, ${withDefOne}"
}
fi
}
[ ${numZones} -gt 4 ] && {
lastZone=$(echo "${zones}" | awk -F ',' ' { print $(NF) } ' | sed -e "s/^ //")
withLastZone=$(${ROON}/bin/get_zone_info -l -z "${lastZone}")
withLastZone=$(echo ${withLastZone} | sed -e "s/${defaultZone},//")
withLastZone=$(echo ${withLastZone} | sed -e "s/${secondZone},//")
numWith=$(echo "${withLastZone}" | awk -F ',' ' { print NF } ')
if [ ${numWith} -gt 2 ]; then
withDefOne=$(echo "${withLastZone}" | awk -F ',' ' { print $(NF-2) ", " $(NF-1) ", " $(NF) } ')
groupFor="${lastZone}, ${withDefOne}"
else
if [ ${numWith} -gt 1 ]; then
withDefOne=$(echo "${withLastZone}" | awk -F ',' ' { print $(NF-1) ", " $(NF) } ')
groupFor="${lastZone}, ${withDefOne}"
else
[ ${numWith} -gt 0 ] && {
withDefOne=$(echo "${withLastZone}" | awk -F ',' ' { print $(NF) } ')
groupFor="${lastZone}, ${withDefOne}"
}
fi
fi
}
cat ${ROON_INI} \
| sed -e "s/__ALL_ZONES__/${zones}/" \
-e "s/__GROUP_ONE__/${groupOne}/" \
-e "s/__GROUP_TWO__/${groupTwo}/" \
-e "s/__GROUP_REE__/${groupRee}/" \
-e "s/__GROUP_FOR__/${groupFor}/" \
-e "s/__VERSION__/${version}/" \
-e "s/__RELEASE__/${release}/" \
-e "s/__DEF_ZONE__/${defaultZone}/" >/tmp/zones$$
${SUDO} cp /tmp/zones$$ ${ROON_INI}
rm -f /tmp/zones$$
DEFZONE=$(grep ^DefaultZone ${ROON_INI} | awk -F '=' ' { print $2 } ')
# Remove leading and trailing spaces in DEFZONE
DEFZONE="$(echo -e "${DEFZONE}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')"
# Set ROON_ZONE in pyroonconf if not already set
if [ -f ${ROONCONF} ]; then
grep ROON_ZONE ${ROONCONF} >/dev/null || {
echo "ROON_ZONE=\"$DEFZONE\"" | ${SUDO} tee -a ${ROONCONF} >/dev/null
}
else
echo "ROON_ZONE=\"$DEFZONE\"" | ${SUDO} tee ${ROONCONF} >/dev/null
fi
echo ""
echo "Verify the 'server' and 'user' settings in the roon script are correct"
echo "and copy the 'roon' frontend shell script to a location in your execution"
echo "PATH on all systems from which you wish to control Roon via SSH"
echo ""
echo "Edit the Roon Command Line configuration settings at:"
echo "${ROON_INI}"
echo "and verify the settings in the configuration file ${ROONCONF}"
echo ""
echo "RoonCommandLine can be uninstalled with the './Uninstall' command"