-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbt-radio-setup.bash
executable file
·2877 lines (1946 loc) · 94.6 KB
/
bt-radio-setup.bash
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
COPYRIGHT_YEARS="2022-2025"
# Version of this script
APP_VERSION="1.11.1" # 2024/DECEMBER/22ND
########################################################################################################################
########################################################################################################################
# Copyright 2022-2025 GPLv3, Bluetooth Internet Radio By Mike Kilday: [email protected] (leave this copyright / attribution intact in ALL forks / copies!)
# https://github.com/taoteh1221/Bluetooth_Internet_Radio
# Fully automated setup of bluetooth, internet radio player (PyRadio), local music files player (mplayer), on a headless RaspberryPi,
# connecting to a stereo system's bluetooth receiver (bash script, chmod +x it to run).
# To install automatically on Ubuntu / RaspberryPi OS / Armbian, copy => paste => run the command below in a
# terminal program (using the 'Terminal' app in the system menu, or over remote SSH), while logged in AS THE
# USER THAT WILL RUN THE APP (user must have sudo privileges):
# wget --no-cache -O bt-radio-setup.bash https://tinyurl.com/bt-radio-setup;chmod +x bt-radio-setup.bash;./bt-radio-setup.bash
# AFTER installation, ~/radio is installed as a shortcut command pointing to this script,
# and paired bluetooth reconnects (if disconnected) when you start a new terminal session.
# A command line parameter can be passed to auto-select menu choices. Multi sub-option selecting is available too,
# by seperating each sub-option with a space, AND ecapsulating everything in quotes like "option1 sub-option2 sub-sub-option3".
# Running normally (displays options to choose from):
# ~/radio
# Auto-selecting single / multi option examples (MULTI OPTIONS #MUST# BE IN QUOTES!):
# ~/radio "1 y"
# ~/radio "upgrade y"
# (checks for / confirms script upgrade)
# ~/radio "7 1 b3"
# ~/radio "internet 1 b3"
# (plays default INTERNET playlist in background, 3rd station)
# ~/radio "internet 1 b3vlc"
# (plays default INTERNET playlist in background, 3rd station, RESET default player to: vlc)
# ~/radio "9 bsr"
# ~/radio "local bsr"
# (rescans music files / plays LOCAL music folder ~/Music/MPlayer [RECURSIVELY] in background, shuffling)
# ~/radio 10
# ~/radio off
# (stops audio playback)
# ~/radio "12 XX:XX:XX:XX:XX:XX"
# ~/radio "connect XX:XX:XX:XX:XX:XX"
# (connect bluetooth device by mac address)
# ~/radio "13 XX:XX:XX:XX:XX:XX"
# ~/radio "remove XX:XX:XX:XX:XX:XX"
# (remove bluetooth device by mac address)
# ~/radio "14 3"
# ~/radio "devices paired"
# (shows paired bluetooth devices)
########################################################################################################################
########################################################################################################################
# If parameters are added via command line
# (CLEANEST WAY TO RUN PARAMETER INPUT #TO AUTO-SELECT MULTIPLE CONSECUTIVE OPTION MENUS#)
# (WE CAN PASS THEM #IN QUOTES# AS: command "option1 sub-option2 sub-sub-option3")
if [ "$1" != "" ] && [ "$APP_RECURSE" != "1" ]; then
# Flag recursion and export it
APP_RECURSE=1
export APP_RECURSE=$APP_RECURSE
# Convert any human-readable params to their numeric counterpart(s)
convert="$1"
# Multi-options MUST be converted FIRST
# (helps avoid mis-converting PRIMARY options [if we add a lot in the future])
# devices internal
convert=$(echo "$convert" | sed -r "s/devices internal/14 1/g")
# devices available
convert=$(echo "$convert" | sed -r "s/devices available/14 2/g")
# devices paired
convert=$(echo "$convert" | sed -r "s/devices paired/14 3/g")
# devices trusted
convert=$(echo "$convert" | sed -r "s/devices trusted/14 4/g")
# upgrade
convert=$(echo "$convert" | sed -r "s/upgrade/1/g")
# internet
convert=$(echo "$convert" | sed -r "s/internet/7/g")
# local
convert=$(echo "$convert" | sed -r "s/local/9/g")
# off
convert=$(echo "$convert" | sed -r "s/off/10/g")
# stop (backwards compatibility)
convert=$(echo "$convert" | sed -r "s/stop/10/g")
# connect
convert=$(echo "$convert" | sed -r "s/connect/12/g")
# remove
convert=$(echo "$convert" | sed -r "s/remove/13/g")
if [ ! -f ~/radio ]; then
echo " "
echo "Setting up a few things first, PLEASE RUN WITH YOUR CLI PARAMETERS *AGAIN AFTER* THIS SETUP COMPLETES..."
echo " "
else
# Pipe it through
printf "%s\n" $convert | ~/radio
exit
fi
fi
######################################
ISSUES_URL="https://github.com/taoteh1221/Bluetooth_Internet_Radio/issues"
echo " "
echo "PLEASE REPORT ANY ISSUES HERE: $ISSUES_URL"
echo " "
echo "Initializing, please wait..."
echo " "
######################################
# EXPLICITLY set any dietpi paths
# Export too, in case we are calling another bash instance in this script
if [ -f /boot/dietpi/.version ]; then
PATH=/boot/dietpi:$PATH
export PATH=$PATH
fi
# EXPLICITLY set any ~/.local/bin paths
# Export too, in case we are calling another bash instance in this script
if [ -d ~/.local/bin ]; then
PATH=~/.local/bin:$PATH
export PATH=$PATH
fi
# EXPLICITLY set any /usr/sbin path
# Export too, in case we are calling another bash instance in this script
if [ -d /usr/sbin ]; then
PATH=/usr/sbin:$PATH
export PATH=$PATH
fi
# In case we are recursing back into this script (for filtering params etc),
# flag export of a few more basic sys vars if present
# Authentication of X sessions
export XAUTHORITY=~/.Xauthority
# Working directory
export PWD=$PWD
######################################
# Get date / time
DATE=$(date '+%Y-%m-%d')
TIME=$(date '+%H:%M:%S')
# Current timestamp
CURRENT_TIMESTAMP=$(date +%s)
# Are we running on Ubuntu OS?
IS_UBUNTU=$(cat /etc/os-release | grep -i "ubuntu")
# If a symlink, get link target for script location
# WE ALWAYS WANT THE FULL PATH!
if [[ -L "$0" ]]; then
SCRIPT_LOCATION=$(readlink "$0")
else
SCRIPT_LOCATION="$( cd -- "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )/"$(basename "$0")""
fi
# Now set path / file vars, after setting SCRIPT_LOCATION
SCRIPT_PATH="$( cd -- "$(dirname "$SCRIPT_LOCATION")" >/dev/null 2>&1 ; pwd -P )"
SCRIPT_NAME=$(basename "$SCRIPT_LOCATION")
# Parent directory of the script location
PARENT_DIR="$(dirname "$SCRIPT_LOCATION")"
# Get the operating system and version
if [ -f /etc/os-release ]; then
# freedesktop.org and systemd
. /etc/os-release
OS=$NAME
VER=$VERSION_ID
elif type lsb_release >/dev/null 2>&1; then
# linuxbase.org
OS=$(lsb_release -si)
VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
# For some versions of Debian/Ubuntu without lsb_release command
. /etc/lsb-release
OS=$DISTRIB_ID
VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
# Older Debian/Ubuntu/etc.
OS=Debian
VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
# Older SuSE/etc.
...
elif [ -f /etc/redhat-release ]; then
# Older Red Hat, CentOS, etc.
...
else
# Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
OS=$(uname -s)
VER=$(uname -r)
fi
# For setting user agent header in curl, since some API servers !REQUIRE! a set user agent OR THEY BLOCK YOU
CUSTOM_CURL_USER_AGENT_HEADER="User-Agent: Curl (${OS}/$VER; compatible;)"
######################################
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
if hash tput > /dev/null 2>&1; then
red=`tput setaf 1`
green=`tput setaf 2`
yellow=`tput setaf 3`
blue=`tput setaf 4`
magenta=`tput setaf 5`
cyan=`tput setaf 6`
reset=`tput sgr0`
else
red=``
green=``
yellow=``
blue=``
magenta=``
cyan=``
reset=``
fi
######################################
# Get logged-in username (if sudo, this works best with logname)
TERMINAL_USERNAME=$(logname)
# If logname doesn't work, use the $SUDO_USER or $USER global var
if [ -z "$TERMINAL_USERNAME" ]; then
if [ -z "$SUDO_USER" ]; then
TERMINAL_USERNAME=$USER
else
TERMINAL_USERNAME=$SUDO_USER
fi
fi
# Quit if ACTUAL USERNAME is root
if [ "$TERMINAL_USERNAME" == "root" ]; then
echo " "
echo "${red}Please run as a NORMAL USER WITH 'sudo' PERMISSIONS (NOT LOGGED IN AS 'root').${reset}"
echo " "
echo "${cyan}Exiting...${reset}"
echo " "
exit
fi
######################################
if [ -f "/etc/debian_version" ]; then
echo "${cyan}Your system has been detected as Debian-based, which is compatible with this automated script."
# USE 'apt-get' IN SCRIPTING!
# https://askubuntu.com/questions/990823/apt-gives-unstable-cli-interface-warning
PACKAGE_INSTALL="sudo apt-get install"
PACKAGE_REMOVE="sudo apt-get --purge remove"
echo " "
echo "Continuing...${reset}"
echo " "
elif [ -f "/etc/redhat-release" ]; then
echo "${cyan}Your system has been detected as Redhat-based, which is ${red}CURRENTLY STILL IN DEVELOPMENT TO EVENTUALLY BE (BUT IS *NOT* YET) ${cyan}compatible with this automated script."
PACKAGE_INSTALL="sudo yum install"
PACKAGE_REMOVE="sudo yum remove"
echo " "
echo "Continuing...${reset}"
echo " "
else
echo "${red}Your system has been detected as NOT BEING Debian-based OR Redhat-based. Your system is NOT compatible with this automated script."
echo "${yellow} "
read -n1 -s -r -p $"PRESS ANY KEY to exit..." key
echo "${reset} "
if [ "$key" = 'y' ] || [ "$key" != 'y' ]; then
echo " "
echo "${green}Exiting...${reset}"
echo " "
exit
fi
fi
######################################
# Path to app (CROSS-DISTRO-COMPATIBLE)
get_app_path() {
app_path_result=$(whereis -b $1)
app_path_result="${app_path_result#*$1: }"
app_path_result=${app_path_result%%[[:space:]]*}
app_path_result="${app_path_result#*$1:}"
# If we have found the library already installed on this system
if [ ! -z "$app_path_result" ]; then
PATH_CHECK_REENTRY="" # Reset reentry flag
echo "$app_path_result"
# If we are re-entering from the else statement below, quit trying, with warning sent to terminal (NOT function output)
elif [ ! -z "$PATH_CHECK_REENTRY" ]; then
PATH_CHECK_REENTRY="" # Reset reentry flag
echo "${red} " > /dev/tty
echo "System path for '$1' NOT FOUND, even AFTER package installation attempts, giving up." > /dev/tty
echo " " > /dev/tty
echo "*PLEASE* REPORT THIS ISSUE HERE, *IF THIS SCRIPT FAILS TO RUN PROPERLY FROM THIS POINT ONWARD*:" > /dev/tty
echo " " > /dev/tty
echo "$ISSUES_URL" > /dev/tty
echo "${reset} " > /dev/tty
echo "${yellow} " > /dev/tty
read -n1 -s -r -p $"PRESS ANY KEY to continue..." key
echo "${reset} " > /dev/tty
if [ "$key" = 'y' ] || [ "$key" != 'y' ]; then
echo " " > /dev/tty
echo "${green}Continuing...${reset}" > /dev/tty
echo " " > /dev/tty
fi
echo " " > /dev/tty
# If library not found, attempt package installation
else
# Handle package name exceptions...
# bsdtar on Ubuntu 18.x and higher
if [ "$1" == "bsdtar" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="libarchive-tools"
# xdg-user-dir (debian package name differs slightly)
elif [ "$1" == "xdg-user-dir" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="xdg-user-dirs"
# rsyslogd (debian package name differs slightly)
elif [ "$1" == "rsyslogd" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="rsyslog"
# xorg (debian package name differs)
elif [ "$1" == "xorg" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="xserver-xorg"
# chromium-browser (debian package name differs)
elif [ "$1" == "chromium-browser" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="chromium"
# epiphany-browser (debian package name differs)
elif [ "$1" == "epiphany-browser" ] && [ -f "/etc/debian_version" ]; then
SYS_PACK="epiphany"
else
SYS_PACK="$1"
fi
# Terminal alert for good UX...
if [ "$1" != "$SYS_PACK" ]; then
echo " " > /dev/tty
echo "${yellow}'$1' is found WITHIN '$SYS_PACK', changing package request accordingly...${reset}" > /dev/tty
echo " " > /dev/tty
fi
echo " " > /dev/tty
echo "${cyan}Installing required component '$SYS_PACK', please wait...${reset}" > /dev/tty
echo " " > /dev/tty
sleep 3
$PACKAGE_INSTALL $SYS_PACK -y > /dev/tty
# If UBUNTU (*NOT* any other OS) snap was detected on the system, try a snap install too
# (as they moved some libs over to snap / snap-only? now)
if [ ! -z "$UBUNTU_SNAP_PATH" ]; then
UBUNTU_SNAP_INSTALL="sudo $UBUNTU_SNAP_PATH install"
echo " " > /dev/tty
echo "${yellow}CHECKING FOR UBUNTU SNAP PACKAGE '$SYS_PACK', please wait...${reset}" > /dev/tty
echo " " > /dev/tty
sleep 3
$UBUNTU_SNAP_INSTALL $SYS_PACK > /dev/tty
fi
sleep 2
PATH_CHECK_REENTRY=1 # Set reentry flag, right before reentry
echo $(get_app_path "$1")
fi
}
# Ubuntu uses snaps for very basic libraries these days, so we need to configure for possible snap installs
if [ "$IS_UBUNTU" != "" ]; then
UBUNTU_SNAP_PATH=$(get_app_path "snap")
fi
######################################
# Make sure automatic suspend / sleep is disabled
if [ ! -f "${HOME}/.sleep_disabled.dat" ]; then
echo "${red}We need to make sure your system will NOT AUTO SUSPEND / SLEEP, or your app server could stop running.${reset}"
echo "${yellow} "
read -n1 -s -r -p $"PRESS F to fix this (disables auto suspend / sleep), OR any other key to skip fixing..." key
echo "${reset} "
if [ "$key" = 'f' ] || [ "$key" = 'F' ]; then
echo " "
echo "${cyan}Disabling auto suspend / sleep...${reset}"
echo " "
echo -e "ran" > ${HOME}/.sleep_disabled.dat
if [ -f "/etc/debian_version" ]; then
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target > /dev/null 2>&1
elif [ -f "/etc/redhat-release" ]; then
sudo -u gdm dbus-run-session gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0 > /dev/null 2>&1
fi
else
echo " "
echo "${green}Skipping...${reset}"
echo " "
fi
fi
######################################
# ON DEBIAN-BASED SYSTEMS ONLY:
# Do we have less than 900MB PHYSICAL RAM (IN KILOBYTES),
# AND no swap / less swap virtual memory than 900MB (IN BYTES)?
if [ -f "/etc/debian_version" ] && [ "$(awk '/MemTotal/ {print $2}' /proc/meminfo)" -lt 900000 ] && (
[ "$(free | awk '/^Swap:/ { print $2 }')" = "0" ] || [ "$(free --bytes | awk '/^Swap:/ { print $2 }')" -lt 900000000 ]
); then
echo "${red}YOU HAVE LESS THAN 900MB *PHYSICAL* MEMORY, AND ALSO HAVE LESS THAN 900MB SWAP *VIRTUAL* MEMORY. This MAY cause your system to FREEZE, *IF* you have a desktop display attached!${reset}"
echo "${yellow} "
read -n1 -s -r -p $"PRESS F to fix this (sets swap virtual memory to 1GB), OR any other key to skip fixing..." key
echo "${reset} "
if [ "$key" = 'f' ] || [ "$key" = 'F' ]; then
echo " "
echo "${cyan}Changing Swap Virtual Memory size to 1GB, please wait (THIS MAY TAKE AWHILE ON SMALLER SYSTEMS)...${reset}"
echo " "
# Required components check...
# dphys-swapfile
DPHYS_PATH=$(get_app_path "dphys-swapfile")
# sed
SED_PATH=$(get_app_path "sed")
sudo $DPHYS_PATH swapoff
sleep 5
if [ -f /etc/dphys-swapfile ]; then
DETECT_SWAP_CONF=$(sudo sed -n '/CONF_SWAPSIZE=/p' /etc/dphys-swapfile)
if [ "$DETECT_SWAP_CONF" != "" ]; then
sudo sed -i "s/CONF_SWAPSIZE=.*/CONF_SWAPSIZE=1024/g" /etc/dphys-swapfile
elif [ "$DETECT_SWAP_CONF" == "" ]; then
sudo bash -c "echo 'CONF_SWAPSIZE=1024' >> /etc/dphys-swapfile"
fi
sudo $DPHYS_PATH setup
sleep 5
sudo $DPHYS_PATH swapon
sleep 5
echo " "
echo "${green}Swap Memory size has been updated to 1GB.${reset}"
echo " "
else
echo " "
echo "${red}Swap Memory config file could NOT be located, skipping update of Swap Memory size!${reset}"
echo " "
fi
else
echo " "
echo "${green}Skipping...${reset}"
echo " "
fi
fi
######################################
# clean_system_update function START
clean_system_update () {
if [ -z "$ALLOW_FULL_UPGRADE" ]; then
echo " "
echo "${yellow}Does the Operating System on this device update using the \"Rolling Release\" model (Kali, Manjaro, Ubuntu Rolling Rhino, Debian Unstable, etc), or the \"Long-Term Release\" model (Debian, Ubuntu, Raspberry Pi OS, Armbian Stable, Diet Pi, etc)?"
echo " "
echo "${red}(You can SEVERLY MESS UP a \"Rolling Release\" Operating System IF YOU DO NOT CHOOSE CORRECTLY HERE! In that case, you can SAFELY choose \"I don't know\".)${reset}"
echo " "
echo "Enter the NUMBER next to your chosen option.${reset}"
echo " "
OPTIONS="rolling long_term i_dont_know"
select opt in $OPTIONS; do
if [ "$opt" = "long_term" ]; then
ALLOW_FULL_UPGRADE="yes"
echo " "
echo "${green}Allowing system-wide updates before installs.${reset}"
break
else
ALLOW_FULL_UPGRADE="no"
echo " "
echo "${green}Disabling system-wide updates before installs.${reset}"
break
fi
done
echo " "
fi
if [ -z "$PACKAGE_CACHE_REFRESHED" ]; then
if [ -f "/etc/debian_version" ]; then
echo "${cyan}Making sure your APT sources list is updated before installations, please wait...${reset}"
echo " "
# In case package list was ever corrupted (since we are about to rebuild it anyway...avoids possible errors)
sudo rm -rf /var/lib/apt/lists/* -vf > /dev/null 2>&1
sleep 2
sudo apt-get update
echo " "
echo "${cyan}APT sources list update complete.${reset}"
echo " "
fi
if [ "$ALLOW_FULL_UPGRADE" == "yes" ]; then
echo "${cyan}Making sure your system is updated before installations, please wait...${reset}"
echo " "
if [ -f "/etc/debian_version" ]; then
#DO NOT RUN dist-upgrade, bad things can happen, lol
sudo apt-get upgrade -y
elif [ -f "/etc/redhat-release" ]; then
sudo yum upgrade -y
fi
sleep 2
echo " "
echo "${cyan}System updated.${reset}"
echo " "
fi
PACKAGE_CACHE_REFRESHED=1
fi
}
# clean_system_update function END
######################################
# Get PRIMARY dependency lib's paths (for bash scripting commands...auto-install is attempted, if not found on system)
# (our usual standard library prerequisites [ordered alphabetically], for 99% of advanced bash scripting needs)
# avahi-daemon
AVAHID_PATH=$(get_app_path "avahi-daemon")
# bc
BC_PATH=$(get_app_path "bc")
# bsdtar
BSDTAR_PATH=$(get_app_path "bsdtar")
# curl
CURL_PATH=$(get_app_path "curl")
# expect
EXPECT_PATH=$(get_app_path "expect")
# git
GIT_PATH=$(get_app_path "git")
# jq
JQ_PATH=$(get_app_path "jq")
# less
LESS_PATH=$(get_app_path "less")
# sed
SED_PATH=$(get_app_path "sed")
# wget
WGET_PATH=$(get_app_path "wget")
# PRIMARY dependency lib's paths END
######################################
# Get the *INTERNAL* NETWORK ip address
IP=$(ip -o route get to 8.8.8.8 | sed -n 's/.*src \([0-9.]\+\).*/\1/p')
######################################
# Dependencies SPECIFICALLY for this bluetooth internet radio script...
# pulseaudio's FULL PATH (to run checks later)
PULSEAUDIO_PATH=$(get_app_path "pulseaudio")
# python3's FULL PATH (we DONT want python [which is python2])
PYTHON_PATH=$(get_app_path "python3")
# Install xdg-user-dirs if needed
XDGUSER_PATH=$(get_app_path "xdg-user-dir")
# Install rsyslogd if needed
SYSLOG_PATH=$(get_app_path "rsyslogd")
######################################
###############################################################################################
# Primary init complete, now check bt_autoconnect_install and symbolic link status
###############################################################################################
# bluetooth-autoconnect's FULL PATH (to run checks OR install later)
BT_AUTOCONNECT_PATH="${SCRIPT_PATH}/bluetooth-autoconnect.py"
# bt_autoconnect_install function START
bt_autoconnect_install () {
# Install bluetooth-autoconnect.py if needed (AND we are #NOT# running as sudo)
if [ ! -f "$BT_AUTOCONNECT_PATH" ] && [ "$EUID" != 0 ]; then
# Clears / updates cache, then upgrades (if NOT a rolling release)
clean_system_update
echo " "
echo "${cyan}Installing required component bluetooth-autoconnect and dependencies, please wait...${reset}"
echo " "
# Install python3 prctl
$PACKAGE_INSTALL python3-prctl -y
# Install python3 dbus modules
$PACKAGE_INSTALL python3-dbus python3-slip-dbus python3-pydbus -y
# SPECIFILLY NAME IT WITH -O, TO OVERWRITE ANY PREVIOUS COPY...ALSO --no-cache TO ALWAYS GET LATEST COPY
wget --no-cache -O TEMP-BT-AUTO-CONN.py https://raw.githubusercontent.com/taoteh1221/Bluetooth_Internet_Radio/main/bluetooth-autoconnect/bluetooth-autoconnect.py
sleep 2
mv -v --force TEMP-BT-AUTO-CONN.py "$BT_AUTOCONNECT_PATH"
sleep 2
# bluetooth-autoconnect systemd service start at boot
if [ -d "/lib/systemd/system" ] && [ ! -f $HOME/.local/share/systemd/user/btautoconnect.service ]; then
echo " "
echo "${cyan}Installing bluetooth-autoconnect as a systemd service, please wait...${reset}"
echo " "
# Don't nest / indent, or it could malform the settings
read -r -d '' BT_AUTOCONNECT_STARTUP <<- EOF
\r
[Unit]
Description=Bluetooth autoconnect
After=pulseaudio.service
\r
[Service]
Type=simple
\r
ExecStart=python3 "$BT_AUTOCONNECT_PATH"
[Install]
WantedBy=pulseaudio.service
\r
EOF
# Setup service to run at login
# https://superuser.com/questions/1037466/how-to-start-a-systemd-service-after-user-login-and-stop-it-before-user-logout
mkdir -p $HOME/.local/share/systemd/user
sleep 3
echo -e "$BT_AUTOCONNECT_STARTUP" > $HOME/.local/share/systemd/user/btautoconnect.service
sleep 3
systemctl --user enable btautoconnect.service
echo " "
echo "${cyan}bluetooth-autoconnect systemd service is setup, and will run on terminal login.${reset}"
echo " "
fi
fi
# Run bluetooth-autoconnect.py (IF we are #NOT# running as sudo, AND no systemd startup service is installed)
if [ -f "$BT_AUTOCONNECT_PATH" ] && [ "$EUID" != 0 ] && [ ! -f $HOME/.local/share/systemd/user/btautoconnect.service ]; then
python3 "$BT_AUTOCONNECT_PATH"
fi
}
# bt_autoconnect_install function END
# Call bt_autoconnect_install function
bt_autoconnect_install
# bt_autoconnect_check function
bt_autoconnect_check () {
# Make sure we are connected to the bluetooth receiver (NOT just paired)
# (SOME DEVICES MAY DISCONNECT AGAIN IF WHEN YOU LOGIN, YOU DON'T #QUICKLY# START A SOUND / RADIO STREAM)
CONNECT_STATUS=$(python3 "$BT_AUTOCONNECT_PATH")
if [ -n "$CONNECT_STATUS" ]; then
echo " "
echo "$CONNECT_STATUS"
echo " "
fi
}
if [ ! -f ~/radio ]; then
ln -s "$SCRIPT_LOCATION" ~/radio
echo " "
echo "${red}IMPORTANT INFORMATION:"
echo " "
echo "~/radio command is now a shortcut for ./$SCRIPT_NAME"
echo " "
echo "IF YOU MOVE $SCRIPT_LOCATION TO A NEW LOCATION, #OR RENAME IT#,"
echo "you'll have to delete ~/radio and THIS SCRIPT WILL RE-CREATE THIS SHORTCUT.${reset}"
echo " "
else
echo " "
echo "${cyan}PRO TIPS:"
echo " "
echo "Shortcut to this script: ${green}~/radio${cyan}"
echo " "
echo "Paired bluetooth reconnects (if disconnected) when you start a terminal session"
echo " "
echo "Running normally (displays options to choose from):"
echo " "
echo "${green}~/radio${cyan}"
echo " "
echo "Auto-selecting single / multi option examples ${red}(MULTI OPTIONS #MUST# BE IN QUOTES!)${cyan}:"
echo " "
echo "${green}~/radio \"1 y\""
echo "${green}~/radio \"upgrade y\"${cyan}"
echo "(checks for / confirms script upgrade)"
echo " "
echo "${green}~/radio \"7 1 b3\""
echo "${green}~/radio \"internet 1 b3\"${cyan}"
echo "(plays default INTERNET playlist in background, 3rd station)"
echo "${green}~/radio \"internet 1 b3vlc\"${cyan}"
echo "(plays default INTERNET playlist in background, 3rd station, RESET default player to: vlc)"
echo " "
echo "${green}~/radio \"9 bsr\""
echo "${green}~/radio \"local bsr\"${cyan}"
echo "(rescans music files / plays LOCAL music folder ~/Music/MPlayer [RECURSIVELY] in background, shuffling)"
echo " "
echo "${green}~/radio 10"
echo "${green}~/radio off${cyan}"
echo "(stops audio playback)"
echo " "
echo "${green}~/radio \"12 XX:XX:XX:XX:XX:XX\""
echo "${green}~/radio \"connect XX:XX:XX:XX:XX:XX\"${cyan}"
echo "(connect bluetooth device by mac address)"
echo " "
echo "${green}~/radio \"13 XX:XX:XX:XX:XX:XX\""
echo "${green}~/radio \"remove XX:XX:XX:XX:XX:XX\"${cyan}"
echo "(remove bluetooth device by mac address)"
echo " "
echo "${green}~/radio \"14 3\""
echo "${green}~/radio \"devices paired\"${cyan}"
echo "(shows paired bluetooth devices)"
echo "${reset} "
fi
###############################################################################################
# Secondary init / checks complete, now run main app logic
###############################################################################################
if [ -f ~/.config/radio.alsamixer.state ]; then
echo " "
echo "${cyan}Loading customized alsamixer settings from: ~/.config/radio.alsamixer.state ${reset}"
echo " "
# RELIABLY persist volume / other alsamixer setting changes
# https://askubuntu.com/questions/50067/how-to-save-alsamixer-settings
alsactl --file ~/.config/radio.alsamixer.state restore
fi
echo " "
echo "${yellow}Enter the NUMBER next to your chosen option:${reset}"
echo " "
OPTIONS="upgrade_check pulseaudio_install pulseaudio_fix pulseaudio_status internet_player_install internet_player_fix internet_player_on local_player_install local_player_on any_player_off bluetooth_scan bluetooth_connect bluetooth_remove bluetooth_devices bluetooth_status sound_test volume_adjust troubleshoot syslog_logs journal_logs restart_computer exit_app other_apps about_this_app"
# start options
select opt in $OPTIONS; do
##################################################################################################################
##################################################################################################################
if [ "$opt" = "upgrade_check" ]; then
######################################
echo " "
if [ "$EUID" == 0 ]; then
echo "${red}Please run #WITHOUT# 'sudo' PERMISSIONS.${reset}"
echo " "
echo "${cyan}Exiting...${reset}"
echo " "
exit
fi
######################################
# Check for newer version
API_VERSION_DATA=$(curl -s 'https://api.github.com/repos/taoteh1221/Bluetooth_Internet_Radio/releases/latest')
LATEST_VERSION=$(echo "$API_VERSION_DATA" | jq -r '.tag_name')
APP_MAJOR_MINOR=$(echo "${APP_VERSION%.*}" | xargs) #X.XX trim whitespace
APP_BUG_FIXES=$(echo "${APP_VERSION##*.}" | xargs) #X trim whitespace
LATEST_MAJOR_MINOR=$(echo "${LATEST_VERSION%.*}" | xargs) #X.XX trim whitespace
LATEST_BUG_FIXES=$(echo "${LATEST_VERSION##*.}" | xargs) #X trim whitespace
if ( [ $(echo "$LATEST_MAJOR_MINOR > $APP_MAJOR_MINOR" |bc -l) -eq 1 ] ) || ( [ $(echo "$LATEST_MAJOR_MINOR == $APP_MAJOR_MINOR" |bc -l) -eq 1 ] && [ $(echo "$LATEST_BUG_FIXES > $APP_BUG_FIXES" |bc -l) -eq 1 ] ); then