-
Notifications
You must be signed in to change notification settings - Fork 2
/
upgrade
executable file
·782 lines (731 loc) · 36.1 KB
/
upgrade
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
#!/bin/bash
#color and other formatting configurations:
if [ -f /usr/local/lib/format.sh ]; then
source /home/pi/Projects/ser2bt-bridge/format.sh > /dev/null 2>&1
format_ena=flag=1 #True
else
printf "\nFormat library is not installed, continuing without!\n"
format_ena=flag=0 #False
fi
#File and directory Location settings:
home="/home/${SUDO_USER}/"
etc_folder="/etc/"
tmp_folder="/tmp/"
etc_default_folder="${etc_folder}/default/"
lib_folder="/lib/"
dev_folder="/dev/"
boot="/boot/"
bt_folder="bluetooth/"
project_folder="${home}Projects/ser2bt-bridge/"
project_img_folder="${project_folder}app_images/"
project_service_folder="${project_folder}services/"
support_folder="${home}Projects/support/"
logging_folder="${home}console_logs/"
bin_folder="/usr/local/bin/"
main_lib_folder="/usr/local/lib/"
ser2bt_lib_folder="${main_lib_folder}ser2bt-bridge/"
ser2bt_img_folder="${ser2bt_lib_folder}images/"
etc_service_folder="${etc_folder}systemd/system/"
lib_service_folder="${lib_folder}systemd/system/"
serial_fix_folder="${etc_service_folder}getty@ttyGS0.service.d/"
#List of required config and image files:
shut_file="shut" #This is the file that hold the shutdown/exit flag to save time by not loading the graphics on the e-paper. Flags are p for power, and e for exit.
e_ink_dev="spidev0.0"
python_req_file="requirements.txt"
bt_service="bluetooth.service"
bt_config="main.conf"
boot_config="config.txt"
serial_fix_file="override.conf"
rc_local="rc.local"
bcm_ver="1.69"
bcm2835_lib_file="libbcm2835.a"
bcm2835_install_file="bcm2835-${bcm_ver}.tar.gz"
bcm2835_link="http://www.airspayce.com/mikem/bcm2835/"
wiringpi="wiringpi-latest.deb"
wiringpi_link="https://project-downloads.drogon.net/"
wget_options=("-P ${tmp_folder}" "-q")
tar_post_options="-C ${tmp_folder}"
disable_svc=("keyboard-setup.service" "dphys-swapfile.service" "triggerhappy.service" \
"triggerhappy.socket" "apt-daily.service" "apt-daily.timer" \
"apt-daily-upgrade.service" "apt-daily-upgrade.timer" "rpi-eeprom-update.service" \
"ser2net.service")
base_package_install_list=("screen" "git" "minicom" "tio" "rfkill" "dnsutils" "xterm" "telnet" "telnetd" "ser2net" "spi-tools")
base_bin_files=(ser2bt_bridge screen_bat_mon screen_get_baud)
wavesh_bin_files=(ser2bt_status init_scr shutdown_scr ser2bt_lib.py)
full_bin_files=( "${base_bin_files[@]}")
pisugar2_bin_files=(button_detect bat_mon)
ups_lite_bin_files=(bat_mon)
base_lib_files=("format.sh")
ser2bt_img_files=("logo.bmp" "logo_inverted.bmp" "battery.png" "battery-10.png" "battery-90.png")
full_lib_files=( "${base_lib_files[@]}") #The script will determine what should get added to base_lib_files later on:
etc_files=(ser2bt_bridge.screenrc)
wavesh_etc_files=(ser2bt_config.ini)
base_service_files=(rfcomm.service)
enhanc_package_install_list=("wiringpi" "python3-pip" "python3-pil" "python3-numpy" "python3-smbus" "python3-gpiozero")
ups_package_install_list=("i2c-tools")
wavesh_package_install_list=("fonts-roboto-fontface" "fonts-freefont-ttf")
wavesh_service_files=(ser2bt-e-paper.service ser2bt-e-paper-init.service)
full_service_files=( "${base_service_files[@]}")
pisugar2_service_files=(ser2bt-button.service ser2bt-battery.service)
ups_lite_service_files=(ser2bt-battery.service)
wavesh_service_assoc=(ser2bt_status ser2bt-e-paper.service)
declare -a enhanc_python_req_list
enhanc_python_req_list=(`cat "${python_req_file}"`)
pisugar2_service_assoc=(button_detect ser2bt-button.service bat_mon ser2bt-battery.service)
ups_lite_service_assoc=(bat_mon ser2bt-battery.service)
removed_service_files=("ser2bt-e-paper-shut.service" "ser2bt_button.service" "ser2bt-bat.service")
begin_bashrc_addon_mark="#BEGIN ser2b-bridge .bashrc add-in"
end_bashrc_addon_mark="#END ser2b-bridge .bashrc add-in"
begin_rc_local_addon_mark="#BEGIN ser2b-bridge rc.local add-in"
end_rc_local_addon_mark="#END ser2b-bridge rc.local add-in"
rc_local_fix_content="${begin_rc_local_addon_mark}\n\n#Make sure bluetooth discoverable and pairable are turned on at boot:\nsudo bluetoothctl <<EOF\ndiscoverable on\npairable on\nEOF\n\n#Turn off HDMI:\n/usr/bin/tvservice -o\n\n${end_rc_local_addon_mark}"
serial_fix_file_content="[Service]\nTTYReset=no\nTTYVHangup=no\nTTYVTDisallocate=no"
bt_service_pre_search="ExecStart=/usr/lib/bluetooth/bluetoothd"
bt_service_content="ExecStart=/usr/lib/bluetooth/bluetoothd -C --noplugin=sap\nExecStartPost=/usr/bin/sdptool add SP\nExecStartPost=/bin/hciconfig hci0 piscan"
base_config_txt_addons=("hdmi_blanking=" "disable_splash=" "boot_delay=" "dtparam=audio=" "enable_uart=" "gpu_mem=" "initial_turbo=" "dtoverlay=")
enhanc_config_txt_addons=("dtparam=i2c_arm=")
wavesh_config_txt_addons=("dtparam=spi=")
pisugar2_config_txt_addons=("dtoverlay=i2c-rtc,ds1307,addr=")
bt_config_change=("Name = " "DiscoverableTimeout = " "PairableTimeout = ")
bt_config_search=(".?[a-zA-Z0-9]+" "[01]" "[01]")
bt_config_fix_value=(".HOSTNAME" 0 0)
bt_config_value=('$HOSTNAME' 0 0)
full_package_install_list=("${enhanc_package_install_list[@]}")
full_config_txt_addons=("${base_config_txt_addons[@]}" "${enhanc_config_txt_addons[@]}")
base_config_txt_values=(2 1 0 "off" 1 32 60 "dwc2")
base_boot_search=("[0-9]" "[0-9]" "[0-9]" "[0-9]" "on" "[0-9]" "[0-9]\*" "[0-9]\*" "dwc2")
enhanc_config_txt_values=("on")
wavesh_config_txt_values=("on")
enhanc_boot_search=("\(off\|on\)")
wavesh_boot_search=("\(off\|on\)")
full_boot_search=("${base_boot_search[@]}" "${enhanc_boot_search[@]}")
pisugar2_config_txt_values=("0x32")
pisugar2_boot_search=("0x[0-9]\*")
full_python_req_list=("${enhanc_python_req_list[@]}")
full_config_txt_values=("${base_config_txt_values[@]}" "${enhanc_config_txt_values[@]}")
ups_type="N/A"
locale_keys=("LANG=" "LANGUAGE=" "LC_ALL=")
#Messeges:
failed_to_disable="${drk_red}Failed to disable${nor}."
failed_to_enable="${drk_red}Failed to enable${nor}."
bad_failed="${drk_red}Failed${nor}."
completed="${drk_grn}Done${nor}."
stop="stop"
start="start"
good_dot="${drk_grn}.${nor}"
bad_dot="${drk_red}.${nor}"
continuing="${nor}, continuing."
rtc="${gre}rtc${nor}"
ups="${gre}ups${nor}"
display="${gre}display${nor}"
#Flags
bin_flag=0
serial_fix_flag=0
bt_fix_flag=0
svc_1_bin_flag=0
svc_3_bin_flag=0
svc_5_bin_flag=0
last_col=$(tput cols)
upgrade_path="${1}"
## End declarations
###########################################################################################
## Begin Function defenition:
function e-paper_service_toggle() {
local toggle=$1
local result=$(systemctl is-active ser2bt-e-paper.service)
if [ "${result}" = "active" ] && [ "${toggle}" = "stop" ]; then
printf "\n${toggle^}ping the e-paper service..."
if [ -f ${tmp_folder}${shut_file} ]; then
echo "u" > ${tmp_folder}${shut_file} && printf ${good_dot} || printf ${bad_dot}
fi
systemctl ${toggle} ser2bt-e-paper.service && printf "${good_dot}" || printf "${bad_dot}" &
spinner $!
printf "${completed}\n"
elif [ "${result}" = "inactive" ] && [ ${toggle} = "${stop}" ]; then
printf "\n${toggle^}ping the e-paper service..."
if [ -f ${tmp_folder}${shut_file} ]; then
echo "u" > ${tmp_folder}${shut_file} && printf ${good_dot} || printf ${bad_dot}
fi
shutdown_scr &
spinner $!
printf "${completed}\n"
elif [ "${result}" = "inactive" ] && [ ${toggle} = "${start}" ]; then
printf "\n${toggle^}ing the e-paper service..."
if [ -f ${tmp_folder}${shut_file} ]; then
echo "p" > ${tmp_folder}${shut_file} && printf "${good_dot}" || printf "${bad_dot}${bad_failed}"
else
printf "\n status change failed.\n"
fi
systemctl ${toggle} ser2bt-e-paper.service && printf "${good_dot}" || printf "${bad_dot}" &
spinner $!
printf "${completed}\n"
fi
}
#Upgrade wiringpi.
function upgrade_wiringpi() {
printf "\n${nor}Upgrading ${yel}wiringpi${nor}:"
wget ${wget_options[@]} ${wiringpi_link}${wiringpi}
if [ ${?} -eq 0 ]; then
printf "${good_dot}"
else
printf "${bad_dot}"
fi
dpkg -iE ${tmp_folder}${wiringpi} > /dev/null 2>&1
if [ ${?} -eq 0 ]; then
printf "${completed}\n"
else
printf "${bad_failed} ${continuing}\n"
fi
}
function install_upgrade_bcm_lib() {
printf "\n${nor}Checking for ${yel}${bcm2835_lib_file}${nor}:\n"
if [ -f "${main_lib_folder}${bcm2835_lib_file}" ]; then
printf " ${yel}${bcm2835_lib_file}${nor} exists, skipping...\n"
else
printf " ${yel}${bcm2835_lib_file}${nor} does not exist, installing..."
wget ${wget_options[@]} ${bcm2835_link}${bcm2835_install_file}
if [ ${?} -eq 0 ]; then
printf "${good_dot}"
tar zxvf ${tmp_folder}${bcm2835_install_file} ${tar_post_options} > /dev/null 2>&1 && printf "${good_dot}" || (printf "${bad_dot}Exiting, we cannot continue without installing first.\n"; exit 1)
cd "${tmp_folder}bcm2835-${bcm_ver}" && printf "${good_dot}" || exit 1
./configure > /dev/null 2>&1 && printf "${good_dot}" || (printf "${bad_dot}Exiting, we cannot continue without installing first.\n"; exit 1)
make > /dev/null 2>&1 && printf "${good_dot}" || (printf "${bad_dot}Exiting, we cannot continue without installing first.\n"; exit 1)
make check > /dev/null 2>&1 && printf "${good_dot}" || (printf "${bad_dot}Exiting, we cannot continue without installing first.\n"; exit 1)
make install > /dev/null 2>&1 && printf "${good_dot}" || (printf "${bad_dot}Exiting, we cannot continue without installing first.\n"; exit 1)
printf "${completed}\n"
else
printf "${bad_dot}"
fi
fi
}
#Upgrade system files
function system_update() {
printf "\n${nor}Update package manager:"
apt_update_result=$(apt update 2> /dev/null)
if [ ${?} -gt 0 ]; then
apt_update_flag=0 #False
up_to_date_flag=0 #False
printf " \n${nor}Update ${red}failed${nor}, exiting to shell\n"
exit 1 #apt update or apt full-upgrade failed"
else
echo ${apt_update_result} | grep -q -s -o -e "[0-9]* packages can be upgraded"
if [ ${?} -eq 0 ]; then
packadge_count=$(echo ${apt_update_result} | grep -o "[0-9]* packages can be upgraded" 2> /dev/null | grep -o "[0-9]*")
printf " \n ${grn}Update completed${nor}, ${yel}${packadge_count} ${nor} packadges will be upgraded.\n"
apt list --upgradable 2> /dev/null | grep -oe "^[-\.0-9a-z]*" 2> /dev/null
apt_update_flag=1 #True
up_to_date_flag=0 #False
elif [ ${?} -eq 1 ]; then
up_to_date=$(echo ${apt_update_result} | grep -ox "All packages are up to data")
printf " \n ${nor}We are already ${grn}up-to-date${continuing}\n"
apt_update_flag=1 #True
up_to_date_flag=1 #True
else
printf " \n ${drk_red}Update failed${nor}, exiting to shell\n"
exit 1 #system update failed"
apt_update_flag=0 #False
up_to_date_flag=0 #False
fi
fi
if [[ "${up_to_date_flag}" -eq 0 || "${apt_update_flag}" -eq 0 ]]; then
printf "\n${nor}Upgrade package manager:"
apt full-upgrade -y > /dev/null 2>&1
if [ ${?} -eq 1 ]; then
printf " \n${drk_red}Upgrade failed${nor}, exiting to shell\n"
exit 1 #apt update or apt full-upgrade failed"
else
printf " \n${nor}Upgrade ${drk_grn}Completed${nor}...\n"
fi
fi
}
## End function declaration
###########################################################################################
## Begin Main Program:
#Checks before begining:
##Check to make sure this is a rpi zero:
if grep -q "Zero" /proc/device-tree/model; then
#Starting script here:
printf "\n\n${nor}Beginning ${cyn}${upgrade_path}${nor} upgrade of ${yel}ser2bt-bridge${nor}:\n\n"
else
printf "\n${nor}This is ${drk_red}not${nor} a ${yel}Raspberry Zero${nor}, ${drk_red}Aborting ${nor}installation/upgrade.\n" | fmt --goal=91 --width=${last_col}
exit 1
fi
# Make sure the script is run with superuser privelages
if [[ "${UID}" -ne 0 ]]; then
printf "\n${nor}You do ${drk_red}not ${nor}have the permissions to successfully run this script.\n"
exit 1
fi
#check for minimum arguements:
if [[ "${#}" -lt 1 ]]; then
printf "${nor}Usage: sudo ${0} UPGRADE_OPTION [basic|battery|sceen|full].\n\n"
printf "Upgrade or install ${yel}ser2bt-bridge${nor}, with the following options:\n"
printf "\tbasic -\t\t${gre}Basic istallation of the ${yel}ser2bt-bridge ${gre}scripts.${nor}\n"
printf "\tbattery -\t${gre}Basic istallation of the ${yel}ser2bt-bridge ${gre}scripts, plus ${yel}battery ${gre}support.${nor}\n"
printf "\tscreen -\t${gre}Basic istallation of the ${yel}ser2bt-bridge ${gre}scripts, plus ${yel}e-ink ${gre}support.${nor}\n"
printf "\tfull -\t\t${gre}Full istallation of the ${yel}ser2bt-bridge ${gre}scripts, to include ${yel}e-ink ${gre}and ${yel}battery ${gre}support.${nor}\n\n"
exit 2
fi
#assign the upgrade path to the first argument
shift
if [ "${upgrade_path}" = "basic" ] ; then
bin_files=( "${base_bin_files[@]}" )
lib_files=( "${base_lib_files[@]}" )
service_files=( "${base_service_files[@]}" )
config_txt_addons=("${base_config_txt_addons[@]}")
config_txt_values=("${base_config_txt_values[@]}")
boot_search=(${base_boot_search[@]})
package_install_list=("${base_package_install_list[@]}")
ups_type="none"
fi
if [ "${upgrade_path}" = "enhanced" ] ; then
bin_files=(${base_bin_files[@]})
lib_files=(${base_lib_files[@]}) #The script will determine what should get added to base_lib_files later on:
service_files=(${enhanc_service_files[@]})
service_assoc=(${enhanc_service_assoc[@]})
config_txt_addons=(${enhanc_config_txt_addons[@]})
config_txt_values=("${enhanc_config_txt_values[@]}")
boot_search=(${enhanc_boot_search[@]})
package_install_list=("${enhanc_package_install_list[@]}")
python_req_list=("${enhanc_python_req_list[@]}")
fi
if [ "${upgrade_path}" = "full" ] ; then
bin_files=(${full_bin_files[@]})
lib_files=(${base_lib_files[@]}) #The script will determine what should get added to base_lib_files later on:
service_files=(${full_service_files[@]})
service_assoc=(${full_service_assoc[@]})
config_txt_addons=(${full_config_txt_addons[@]})
config_txt_values=("${full_config_txt_values[@]}")
boot_search=(${full_boot_search[@]})
package_install_list=("${full_package_install_list[@]}")
python_req_list=("${full_python_req_list[@]}")
fi
#Begin main program:
#Examine installed hardware to help determine what files to install and configuration changes that need to be made:
if [ ${upgrade_path} != "basic" ] ; then
printf "\n${nor}Determining installed hardware:${nor}\n"
#If this is not a basic install, check to see if e-ink display is installed:
result=$(spi-config -q -d ${dev_folder}${e_ink_dev} 2> /dev/null | grep "speed=4000000")
if [ ${?} -eq 0 ]; then
e_ink="True"
printf " ${nor}It appears that you have an ${yel}e-ink ${display}.\n"
else
result=$(spi-config -q -d ${dev_folder}${e_ink_dev} 2> /dev/null | grep "speed=125000000")
if [ ${?} -eq 0 ]; then
e_ink="True"
printf " ${nor}It appears that you have a ${yel}e-ink ${display}.\n"
else
printf " ${nor}It appears that there is no ${yel}e-ink ${display} installed.\n"
e_ink="False"
fi
fi
#If this is not a basic install, check to see which ups is installed.
result=$(i2cdetect -y 1 0x75 0x75 2> /dev/null | grep -Eo "UU|75")
if [ "${result}" == "75" ]; then
ups_type="pisugar2"
printf " ${nor}It looks as if you have a ${yel}${ups_type} ${ups}\n"
resultb=$(i2cdetect -y 1 0x32 0x32 2> /dev/null | grep -Eo "UU|32")
if [ "${resultb}" == "32" ] || [ "${resultb}" == "UU" ]; then
printf " ${nor}It looks as if the ${rtc} on your ${yel}${ups_type} ${ups} is available.\n"
fi
else
result=$(i2cdetect -y 1 0x36 0x36 2> /dev/null | grep -Eo "UU|36")
if [ "${result}" == "36" ]; then
ups_type="ups_lite"
printf " ${nor}It looks as if you have a ${yel}${ups_type} ${ups}.\n"
else
ups_type="none"
fi
fi
if [ "${e_ink}" == "False" ] && [ "${ups_type}" == "none" ]; then
printf " ${nor}No Hardware detected${continuing}\n"
fi
fi
#check for and shutdown the e-paper service if it is currently running:
if [ "${e_ink}" = "True" ]; then
e-paper_service_toggle ${stop}
fi
#Run a full apt update, and upgrade:
system_update &
spinner $!
#Based on what was discovered from any installed hardware, start putting the file lists and settings together for installation:
if [ "${e_ink}" = "True" ] && [ "${upgrade_path}" = "full" ] ; then
config_txt_addons=("${config_txt_addons[@]}" "${wavesh_config_txt_addons[@]}")
config_txt_values=("${config_txt_values[@]}" "${wavesh_config_txt_values[@]}")
boot_search=("${boot_search[@]}" "${wavesh_boot_search[@]}")
bin_files=("${bin_files[@]}" "${wavesh_bin_files[@]}")
etc_files=("${etc_files[@]}" "${wavesh_etc_files[@]}")
service_files=("${service_files[@]}" "${wavesh_service_files[@]}")
service_assoc=("${service_assoc[@]}" "${wavesh_service_assoc[@]}")
package_install_list=("${package_install_list[@]}" "${wavesh_package_install_list[@]}")
fi
if [ "${ups_type}" = "pisugar2" ] && [ "${upgrade_path}" = "full" ] ; then
config_txt_addons=("${config_txt_addons[@]}" "${pisugar2_config_txt_addons[@]}")
config_txt_values=("${config_txt_values[@]}" "${pisugar2_config_txt_values[@]}")
boot_search=("${boot_search[@]}" "${pishugar2_boot_search[@]}")
bin_files=("${bin_files[@]}" "${pisugar2_bin_files[@]}")
service_files=("${service_files[@]}" "${pisugar2_service_files[@]}")
service_assoc=("${service_assoc[@]}" "${pisugar2_service_assoc[@]}")
package_install_list=("${package_install_list[@]}" "${ups_package_install_list[@]}")
elif [ "${ups_type}" = "ups_lite" ] && [ "${upgrade_path}" = "full" ] ; then
bin_files=("${bin_files[@]}" "${ups_lite_bin_files[@]}")
service_files=("${service_files[@]}" "${ups_lite_service_files[@]}")
service_assoc=("${service_assoc[@]}" "${ups_lite_service_assoc[@]}")
package_install_list=("${package_install_list[@]}" "${ups_package_install_list[@]}")
fi
#Now that we have the lists together from above, we can prepare tp install any required packages:
#Verify what is already installed:
printf "\n${nor}Check for the status of required packages:\n"
for package in "${base_package_install_list[@]}"; do
dpkg -s ${package} > /dev/null 2>&1
if [ ${?} -eq 1 ]; then
printf " ${nor}Installing Package ${yel}${package}${nor}..."
apt install ${package} -y > /dev/null 2>&1 && printf "${completed}\n" || printf "${drk_grn}Failed${nor}.\n"
else
printf " ${nor}Package ${yel}${package}${nor} is${drk_grn} already installed${nor}\n"
fi
done
#Add required packadges:
if [ ${upgrade_path} != "basic" ] ; then
printf "\n${nor}Check for the status of additional required packages:\n"
for package in "${package_install_list[@]}"; do
dpkg -s ${package} > /dev/null 2>&1
if [ ${?} -eq 1 ]; then
printf " ${nor}Installing Package ${yel}${package}${nor}..."
apt install ${package} -y > /dev/null 2>&1 && printf "${completed}\n" || printf "${drk_grn}Failed${nor}.\n"
else
printf " ${nor}Package ${yel}${package}${nor} is${drk_grn} already installed${nor}\n"
fi
done
#Add Python dependencies from the requirements.txt This is for non-basic installs/upgrades:
printf "\n${nor}Check for the status of required Python 3 modules:\n"
printf " ${nor}Reading in the list of already-installed modules...${nor}"
pip3 list > pipo &
spinner $!
installed_modules=($(cat pipo))
rm pipo
printf "${completed}\n"
printf " ${nor}Checking required modules against list and installing anything missing:${nor}\n"
for module in "${python_req_list[@]}"; do
echo ${installed_modules[@]} | grep ${module} > /dev/null 2>&1
if [ ${?} -eq 1 ]; then
printf " ${nor}Installing Module ${yel}${module}${nor}..."
pip3 install ${module} > /dev/null 2>&1 && printf "${completed}\n" || printf "${drk_grn}Failed${nor}.\n"
else
printf " ${nor}Module ${yel}${module}${nor} is${drk_grn} already installed${nor}\n"
fi
done
#upgrade wiringpi:
upgrade_wiringpi &
spinner $!
#Install and/or upgrade bcm2835 as needed.
install_upgrade_bcm_lib
fi
#Check for and correct missing or incorrect locale settings that can happen while managing a headless pi:
printf "\n${nor}Checking and correcting ${yel}locale ${nor}issues..."
set_locale=$(grep LANG= "${etc_default_folder}locale" | cut -f 2 -d "=")
for key in "${locale_keys[@]}"; do
grep -q -s -x -E ^${key} "${etc_default_folder}locale"
if [ ${?} -eq 0 ]; then #replace the line.
printf "${drk_red}.${nor}"
update-locale ${key}${set_locale} > /dev/null 2>&1 && printf "${drk_grn}.${nor}" || printf "${drk_red}.${nor}"
else
printf "${drk_grn}.${nor}"
grep -q -s -x -E ^${key}${set_locale} "${etc_default_folder}locale"
if [ ${?} -eq 1 ]; then #Add the line.
printf " ${drk_red}.${nor}"
line=$(grep -s -x -E ^${key}${set_locale} "${etc_default_folder}locale")
update-locale ${key}${set_locale} > /dev/null 2>&1 && printf "${drk_grn}.${nor}" || printf "${drk_red}.${nor}"
else
printf "${drk_grn}.${nor}"
fi
fi
done > pipo &
spinner $!
rm pipo
printf "${completed}\n"
#checking and adding modules as needed into /etc/modules:
printf "\n${nor}Check to make sure the the relavent modules are loaded:\n"
grep -q -s -o -e "^g_serial" ${etc_folder}modules && (printf " ${nor}module ${drk_grn}already${nor} loaded\n") || (echo 'g_serial' >> "${etc_folder}modules"; printf " ${nor}module ${drk_grn}successfully${nor} loaded\n")
grep -q -s -o -e "^dwc2" ${etc_folder}/modules && (printf " ${nor}module ${drk_grn}already${nor} loaded\n") || (bash -c "echo 'dwc2' >> ${etc_folder}modules"; printf " ${nor}module ${drk_grn}successfully ${nor} loaded\n")
#checking and adding modules as needed into /boot/config.txt and /boot/cmdline.txt:
printf "\n${nor}Check to make sure the the relavent seetings are made in the ${yel}boot files:${nor}\n" | fmt --goal=91 --width=${last_col}
change_flag=0
num=0
for config in "${config_txt_addons[@]}"; do
grep -q -s -x -E ^${config}${config_txt_values[num]} ${boot}${boot_config}
if [ ${?} -eq 1 ]; then
rem_config=$(grep -s -x -E ^#?${config}\(dwc2\|0x[a-z0-9]*\|[0-9]*\|on\|off\) ${boot}${boot_config})
if [ ${?} -eq 0 ]; then
printf " The configuration ${yel}${rem_config}${nor} will be replaced by ${yel}${config}${config_txt_values[num]}${nor}..."
sed -i "s/^${rem_config}/${config}${config_txt_values[num]}/" ${boot}${boot_config} && printf ${drk_grn}"Good${nor}\n" || printf ${drk_red}"Bad${nor}\n"
let change_flag++
elif [ ${?} -eq 1 ]; then
sed -i "$ a ${config}${config_txt_values[num]}" ${boot}${boot_config}
printf " ${yel}${config}${config_txt_values[num]} ${nor}has been added to ${cyn}${boot}${boot_config}${nor}\n"
let change_flag++
fi
fi
let num++
done
if [ ${change_flag} -eq 0 ]; then
printf " ${drk_grn}No changes were needed${continuing}\n"
fi
#Check and edit bluetooth configuration file(s) as needed.
printf "\n${nor}Check to make sure the the relavent seetings are made in the ${blu}bluetooth${nor} configuration files:${nor}\n" | fmt --goal=91 --width=${last_col}
change_flag=0
num=0
for config in "${bt_config_change[@]}"; do
grep -q -s -x -E "^${config}${bt_config_fix_value[num]}" ${etc_folder}${bt_folder}${bt_config}
if [ ${?} -eq 1 ]; then
rem_config=$(grep -s -x -E "^#?${config}${bt_config_search[num]}" ${etc_folder}${bt_folder}${bt_config})
if [ ${?} -eq 0 ]; then
printf " The configuration ${yel}${rem_config}${nor} will be replaced by ${yel}${config}${bt_config_value[num]}${nor}..."
sed -i "s/^${rem_config}/${config}${bt_config_value[num]}/" ${etc_folder}${bt_folder}${bt_config} && printf ${drk_grn}"Good${nor}\n" || printf ${drk_red}"Bad${nor}\n"
let change_flag++
let bt_fix_flag++
elif [ ${?} -eq 1 ]; then
sed -i "$ a ${config}${bt_config_value[num]}" ${etc_folder}${bt_folder}${bt_config}
printf " ${yel}${config}${bt_config_value[num]} ${nor}has been added to ${cyn}${etc_folder}${bt_folder}${yel}${bt_config}${nor}\n"
let change_flag++
let bt_fix_flag++
fi
fi
let num++
done
if [ ${change_flag} -eq 0 ]; then
printf " ${drk_grn}No changes were needed${continuing}\n"
else
let bt_fix_flag++
fi
#make directorys:
#Make directory for serial port fix, then verify the config file is correct, if not, make corrections:
printf "\n${nor}Check if the ${cyn}${serial_fix_folder} ${nor}directory exists:\n"
official_s_ver=$(printf ${serial_fix_file_content})
if [ ! -d ${serial_fix_folder} ]; then
printf " ${nor}Directory does ${drk_red}not ${nor}exist, creating...\n"
mkdir -p ${serial_fix_folder}
else
printf " ${nor}Directory ${drk_grn}already ${nor}exists, checking for ${cyn}${serial_fix_folder}${yel}${serial_fix_file}${nor}:\n"
if [ ! -f ${serial_fix_folder}${serial_fix_file} ]; then
printf " ${yel}${serial_fix_file} ${nor}does ${drk_red}not ${nor}exist, creating..."
printf "${official_s_ver}" > "${serial_fix_folder}${serial_fix_file}" && printf " ${completed}.\n" || printf " ${drk_grn}Failed${nor}.\n"
else
printf " ${yel}${serial_fix_file} ${drk_grn}does${nor} exist, checking if updates are needed:\n"
existing_s_ver=$(cat ${serial_fix_folder}${serial_fix_file})
if [ "${existing_s_ver}" != "${official_s_ver}" ]; then
printf " ${nor}There is a difference between what is supposed to be in the ${yel}fix${nor}, will clean and re-apply ${yel}fix${nor} related configurations:\n"
printf "${official_s_ver}" > "${serial_fix_folder}${serial_fix_file}" && printf " ${completed}.\n" || printf " ${drk_grn}Failed${nor}.\n"
serial_fix_flag=1
else
printf " ${nor}Both versions are the same, skipping...\n" | fmt --goal=91 --width=${last_col}
serial_fix_flag=0
fi
fi
fi
#Modify/Ammend the bluetooth/bluez service file:
printf "\n${blu}Bluetooth${nor} service configurations:\n" | fmt --goal=91 --width=${last_col}
if [ -f "${lib_service_folder}${bt_service}" ]; then
existing_bt_chg=$(awk '/^ExecStart/,/piscan/ { print }' ${lib_service_folder}${bt_service})
printf " ${blu}Bluetooth${nor} Service ${drk_grn}was${nor} found, checking content:\n"
if grep -q -e "^${bt_service_pre_search}$" "${lib_service_folder}${bt_service}"; then
printf " ${blu}Bluetooth${nor} Service ${drk_yel}needs${nor} to be edited..."
sed -i "s+${bt_service_pre_search}+${bt_service_content}+g" "${lib_service_folder}${bt_service}" && printf "${completed}\n" || printf "\n"
let bt_fix_flag++
elif [[ $(printf "${existing_bt_chg}") == $(printf "${bt_service_content}") ]]; then
printf " ${blu}Bluetooth${nor} Service does ${drk_grn}not need${nor} to be edited.\n" | fmt --goal=91 --width=${last_col}
let bt_fix_flag--
else
printf " ${drk_red}Error${nor} fatal error, unable to overcome problem in ${yel}${lib_service_folder}${bt_service}${nor}. Please manually fix\n." | fmt --goal=91 --width=${last_col}
let bt_fix_flag--
exit 4
fi
else
printf " ${blu}Bluetooth${nor} Service could ${drk_red}not ${nor}be found, ${drk_red}Aborting${nor}.\n"
bt_fix_flag=0
exit 3
fi
#Disable Un-needed services:
printf "\nChecking for and disabling un-needed services:\n"
for file in "${disable_svc[@]}" ; do
status=$(systemctl is-enabled "${file}" 2>/dev/null)
if [ "${ups_type}" == "none" ] && [ "${file}" == "ser2net.service" ] && [ "${status}" != "enabled" ]; then
printf " Status of ${yel}${file}${nor} is currently ${drk_red}${status}${nor}..."
systemctl enable "${file}" 2>/dev/null && printf "${drk_grn}Enabled${nor}.\n" || printf "${failed_to_enable}\n"
else
if [ "${ups_type}" != "none" ] && [ "${status}" != "disabled" ] && [ "${status}" != "static" ]; then
printf " Status of ${yel}${file}${nor} is currently ${drk_red}${status}${nor}..."
systemctl disable "${file}" 2>/dev/null && printf "${drk_grn}Disabeled${nor}.\n" || printf "${failed_to_disable}\n"
else
printf " Status of ${yel}${file}${nor} is currently ${drk_grn}${status}${nor}.\n"
fi fi
done
#Build support folder(s) and copy files to them:
printf "\n${nor}Check to make sure the ${cyn}${support_folder} ${nor}directories exist:\n"
if [ ! -d ${support_folder} ]; then
printf " ${nor}Directory does ${drk_red}not ${nor}exist, creating...\n"
mkdir -p ${support_folder}
else
printf " ${nor}Directory ${drk_grn}already ${nor}exist, moving on.\n"
fi
printf "\n${nor}check to make sure the ${cyn}${logging_folder} ${nor}directories exist:\n"
if [ ! -d ${logging_folder} ]; then
printf " ${nor}Directory does ${drk_red}not ${nor}exist, creating...\n"
mkdir -p ${logging_folder}
else
printf " ${nor}Directory ${drk_grn}already ${nor}exist, moving on.\n"
fi
printf "\n${nor}check to make sure the ${cyn}${ser2bt_lib_folder} ${nor}directories exist:\n"
if [ ! -d ${ser2bt_lib_folder} ]; then
printf " ${nor}Directory does ${drk_red}not ${nor}exist, creating...\n"
mkdir -p ${ser2bt_lib_folder}
else
printf " ${nor}Directory ${drk_grn}already ${nor}exist, moving on.\n"
fi
printf "\n${nor}check to make sure the ${cyn}${ser2bt_img_folder} ${nor}directories exist:\n"
if [ ! -d ${ser2bt_img_folder} ]; then
printf " ${nor}Directory does ${drk_red}not ${nor}exist, creating...\n"
mkdir -p ${ser2bt_img_folder}
else
printf " ${nor}Directory ${drk_grn}already ${nor}exist, moving on.\n"
fi
#copy files to proper directories:
printf "\nCopying files to the ${cyn}${bin_folder} ${nor}directory:\n"
for file in "${bin_files[@]}" ; do
if cmp --silent "${bin_folder}${file}" "${project_folder}${file}" ; then
printf " ${nor}Both versions of ${yel}${file} ${nor}are the same, Skipping...\n"
else
if [ "${file}" == "${service_assoc[0]}" ] ; then
svc_1_bin_flag=1
bin_flag=1
fi
if [ "${file}" == "${service_assoc[2]}" ] ; then
svc_3_bin_flag=1
bin_flag=1
fi
if [ "${file}" == "${service_assoc[4]}" ] ; then
svc_5_bin_flag=1
bin_flag=1
fi
cp ${project_folder}${file} ${bin_folder} && printf " ${yel}${file} ${nor}copied.\n" || printf " ${yel}${file} ${nor}not copied.\n"
fi
done
printf "\nCopying files to the ${cyn}${main_lib_folder} ${nor}directory:\n"
for file in "${lib_files[@]}" ; do
if cmp --silent "${main_lib_folder}${file}" "${project_folder}${file}" ; then
printf " ${nor}Both versions of ${yel}${file} ${nor}are the same, Skipping...\n"
else
cp ${project_folder}${file} ${main_lib_folder} && printf " ${yel}${file} ${nor}copied.\n" || break
fi
done
if [ "${e_ink}" = "True" ] && [ "${upgrade_path}" = "full" ] ; then
printf "\nCopying files to the ${cyn}${ser2bt_img_folder} ${nor}directory:\n"
for file in "${ser2bt_img_files[@]}" ; do
if cmp --silent "${ser2bt_img_folder}${file}" "${project_folder}${file}" ; then
printf " ${nor}Both versions of ${yel}${file} ${nor}are the same, Skipping...\n"
else
cp ${project_img_folder}${file} ${ser2bt_img_folder} && printf " ${yel}${file} ${nor}copied.\n" || break
fi
done
fi
printf "\nCopying files to the ${cyn}${etc_folder} ${nor}directory:\n"
for file in "${etc_files[@]}" ; do
if [ -e "${etc_folder}${file}" ]; then
# if cmp --silent "${etc_folder}${file}" "${project_folder}${file}" ; then
printf " ${yel}${file}${nor} already exists, Skipping...\n"
else
cp ${project_folder}${file} ${etc_folder} && printf "${yel}${file} ${nor}copied.\n" || break
fi
done
printf "\nCoping service files to the ${cyn}${etc_service_folder} ${nor}directory, then disable and enable copied services:\n" | fmt --goal=91 --width=${last_col}
svc_count=0
for svc_file in ${service_files[@]} ; do
if cmp --silent "${etc_service_folder}${svc_file}" "${project_service_folder}${svc_file}" ; then
printf " ${nor}Both versions of ${yel}${svc_file} ${nor}are the same, Skipping...\n"
let svc_count++
else
cp ${project_service_folder}${svc_file} ${etc_service_folder} && printf " ${yel}${svc_file} ${nor}copied.\n" || break
systemctl disable ${svc_file} > /dev/null 2>&1 && printf " ${nor}${svc_file} ${drk_grn}successfully ${nor}disabeled.\n" || break
systemctl enable ${svc_file} > /dev/null 2>&1 && printf " ${nor}${svc_file} ${drk_grn}successfully ${nor}enabeled.\n" || break
fi
done
#Disable, then remove un-needed services"
printf "${nor}\nDisable, and remove any services that are no longer required${nor}\n"
num=0
for svc_file in ${removed_service_files[@]}; do
if [ -f "${etc_service_folder}${svc_file}" ]; then
systemctl disable ${svc_file} > /dev/null 2>&1 && printf " ${yel}${svc_file} ${drk_grn}successfully ${nor}disabeled.\n" || break
rm ${etc_service_folder}${svc_file} && printf " ${yel}${svc_file} ${drk_grn}successfully ${nor}removed.\n" || break
else
let num++
fi
done
if [[ ${#removed_service_files[@]} -eq ${num} ]]; then
printf " ${drk_grn}No changes were needed${continuing}\n"
fi
#After all of the changes have been made, reload the systemd daemon:
if [ "${svc_count}" != "${#service_files[@]}" ] || [[ "${bin_flag}" -gt 0 ]] ; then
printf "\n${nor}Restarting systemctl (as needed):${nor}\n"
systemctl daemon-reload && printf " ${yel}systemctl daemon-reload${nor} was ${drk_grn}Successfull${nor}.\n" || printf " ${nor}systemctl daemon-reload was ${drk_red}not ${nor}successfull.\n"
fi
#Action based on flags:
if [[ "${bt_fix_flag}" -gt 0 ]] ; then
printf "\n${nor}Disabeling, Enabeling and Starting ${bt_service}${nor}:\n"
systemctl disable ${bt_service} > /dev/null 2>&1 && printf " ${yel}${bt_service} ${drk_grn}successfully disabeled${nor}.\n" || printf " ${drk_red}Failed${nor}.\n"
systemctl enable ${bt_service} > /dev/null 2>&1 && printf " ${yel}${bt_service} ${drk_grn}successfully enabled${nor}.\n" || printf " ${drk_red}Failed${nor}.\n"
systemctl start ${bt_service} > /dev/null 2>&1 && printf " ${yel}${bt_service} ${drk_grn}successfully started${nor}.\n" || printf " ${drk_red}Failed${nor}.\n"
let svc_count++
fi
if [[ "${serial_fix_flag}" -gt 0 ]] ; then
printf "\n${nor}Enabeling and starting getty@ttyGS0.service:${nor}\n"
systemctl enable getty@ttyGS0.service > /dev/null 2>&1 && printf " ${nor}getty@ttyGS0.service ${drk_grn}successfully ${nor}enabled.\n" || break
systemctl start getty@ttyGS0.service > /dev/null 2>&1 && printf " ${nor}getty@ttyGS0.service ${drk_grn}successfully ${nor}started.\n" || break
let svc_count++
fi
svc_count=0
if [ "${bin_flag}" -gt 0 ] ; then
printf "\n${nor}Restarting services that are associated to running scripts:${nor}\n"
if [ "${svc_1_bin_flag}" -eq 1 ] ; then
systemctl restart "${service_assoc[1]}" && printf " ${yel}${service_assoc[1]} ${drk_grn}Successfully ${nor}Restarted.\n" || printf "${drk_red}Failed ${yellow}${svc_file}${nor}!\n"
let svc_count++
fi
if [ "${svc_3_bin_flag}" -eq 1 ] ; then
systemctl restart "${service_assoc[3]}" && printf " ${yel}${service_assoc[3]} ${drk_grn}Successfully ${nor}Restarted.\n" || printf "${drk_red}Failed ${yellow}${svc_file}${nor}!\n"
let svc_count++
fi
if [ "${svc_5_bin_flag}" -eq 1 ] ; then
systemctl restart "${service_assoc[5]}" && printf " ${yel}${service_assoc[5]} ${drk_grn}Successfully ${nor}Restarted.\n" || printf "${drk_red}Failed ${yellow}${svc_file}${nor}!\n"
let svc_count++
fi
fi
#updating rc.local:
printf "\nUpdating the ${yel}${rc_local}${nor} addon (if it's already on, it will be removed and replaced):\n" | fmt --goal=91 --width=${last_col}
#First compare whats in bashrc matches the addendum:
existing_rc_ver=$(awk '/^#BEGIN/,/^#END/ { print }' ${etc_folder}${rc_local})
official_rc_ver="${rc_local_fix_content}"
diff --suppress-common-lines -W 160 -y <(printf "${existing_rc_ver}") <(printf "${official_rc_ver}") &>/dev/null
if [ ${?} -eq 1 ]; then
printf " ${nor}There is a difference between what is supposed to be in ${yel}${rc_local}${nor}, will clean and re-apply ${yel}ser2bt-bridge${nor} related configurations:\n" | fmt --goal=91 --width=${last_col}
sed -i '/^#BEGIN ser2b-bridge rc.local add-in/, /^#END ser2b-bridge rc.local add-in/d' ${etc_folder}${rc_local}
sed -i '/^exit 0/d' ${etc_folder}${rc_local}
printf "${official_rc_ver}" >> ${etc_folder}${rc_local} && printf " ${completed}.\n" || printf " ${drk_grn}Failed${nor}.\n"
printf "\nexit 0" >> ${etc_folder}${rc_local} && printf " ${completed}.\n" || printf " ${drk_grn}Failed${nor}.\n"
else
printf " ${nor}Both versions are the same, skipping...\n"
fi
#refreshing .bashrc:
printf "\nAdding the ${yel}.bashrc${nor} addon (if it's already on, it will be removed and replaced):\n" | fmt --goal=91 --width=${last_col}
#First compare whats in bashrc matches the addendum:
existing_ver=$(awk '/^#BEGIN/,/^#END/ { print }' ${home}.bashrc)
official_ver=$(awk '/^#BEGIN/,/^#END/ { print }' ${project_folder}bashrc_addendum.sh)
if [ "${existing_ver}" != "${official_ver}" ] ; then
printf " ${nor}There is a difference between what is supposed to be in ${yel}.bashrc${nor}, will clean and re-apply ${yel}ser2bt-bridge${nor} related configurations:\n" | fmt --goal=91 --width=${last_col}
sed -i '/^#BEGIN ser2b-bridge .bashrc add-in/, /^#END ser2b-bridge .bashrc add-in/d' ${home}.bashrc
cat bashrc_addendum.sh >> ${home}.bashrc && printf " ${completed}.\n" || printf " ${drk_grn}Failed${nor}.\n"
else
printf " ${nor}Both versions are the same, skipping...\n"
fi
#check for and shutdown the e-paper service if it is currently running:
if [ "${e_ink}" = "True" ]; then
e-paper_service_toggle ${start}
fi
exit 0