-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathautoinstall_oled_stats.sh
More file actions
923 lines (827 loc) Β· 38.5 KB
/
autoinstall_oled_stats.sh
File metadata and controls
923 lines (827 loc) Β· 38.5 KB
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
#!/bin/bash
# OLED Stats Display Installation Script
# Version: v0.14.3
# Script Author: 4ngel2769 / @angeldev0
# Original OLED Stats Code: MKlement (mklements)
# Repository: https://github.com/4ngel2769/rpi_oled_stats
# Original Code: https://github.com/mklements/OLED_Stats
# Automates the installation process for Raspberry Pi OS Bookworm
# Usage: curl -fsSL https://raw.githubusercontent.com/4ngel2769/rpi_oled_stats/main/autoinstall_oled_stats.sh | bash
# Usage with verbose: curl -fsSL https://raw.githubusercontent.com/4ngel2769/rpi_oled_stats/main/autoinstall_oled_stats.sh | bash -s -- -v
set -e # Exit on any error
# Script version
SCRIPT_VERSION="v0.14.3"
SCRIPT_AUTHOR="4ngel2769 / @angeldev0"
ORIGINAL_AUTHOR="MKlement (mklements)"
# Default settings
VERBOSE=false
UNATTENDED=false
SKIP_APT_UPDATE=false
SILENT=false
AUTO_REBOOT=true # Auto-reboot behavior (works for both normal and unattended)
DEFAULT_SCRIPT_CHOICE=2 # Default to monitor.py for unattended mode (1=stats.py, 2=monitor.py, 3=psutilstats.py, 4=status.py)
ROTATION=1 # Default rotation (1 = normal, 2 = upside down)
# ================================================================================
# THEME CONFIGURATION
# ================================================================================
# Available themes: STANDARD, HTB, PASTEL
COLOR_SCHEME="PASTEL" # Default theme
# Color resets
# WHITE='\033[1;37m'
NC='\033[0m'
# ================================================================================
# STANDARD COLOR PALETTE
# ================================================================================
STANDARD_PRIMARY='\033[1;36m' # Cyan bold for borders/boxes
STANDARD_SECONDARY='\033[1;35m' # Magenta bold for titles
STANDARD_ACCENT='\033[1;33m' # Yellow bold for labels
STANDARD_SUCCESS='\033[0;32m' # Green for success
STANDARD_WARNING='\033[1;33m' # Yellow for warnings
STANDARD_ERROR='\033[0;31m' # Red for errors
STANDARD_INFO='\033[0;34m' # Blue for info
STANDARD_HIGHLIGHT='\033[0;36m' # Cyan for verbose
STANDARD_TEXT='\033[1;37m' # White bold for text
STANDARD_SPECIAL='\033[1;32m' # Green bold for special items
# ================================================================================
# HTB COLOR PALETTE (Hack The Box inspired)
# ================================================================================
HTB_PRIMARY="\033[38;5;19m" # Deep blue for borders
HTB_SECONDARY="\033[38;5;82m" # Bright neon green for titles
HTB_ACCENT="\033[38;5;226m" # Bright yellow for labels
HTB_SUCCESS="\033[38;5;46m" # Matrix green for success
HTB_WARNING="\033[38;5;214m" # HTB orange for warnings
HTB_ERROR="\033[38;5;196m" # Bright red for errors
HTB_INFO="\033[38;5;33m" # Vivid blue for info
HTB_HIGHLIGHT="\033[38;5;51m" # Bright cyan for verbose
HTB_TEXT="\033[38;5;250m" # Light grey for text
HTB_SPECIAL="\033[38;5;118m" # Lime for special items
# ================================================================================
# PASTEL COLOR PALETTE (Soft and pleasant)
# ================================================================================
PASTEL_PRIMARY="\033[38;5;159m" # Soft cyan for borders
PASTEL_SECONDARY="\033[38;5;141m" # Soft purple for titles
PASTEL_ACCENT="\033[38;5;110m" # Pastel blue for version
PASTEL_SUCCESS="\033[38;5;120m" # Soft green for success
PASTEL_WARNING="\033[38;5;215m" # Soft orange for warnings
PASTEL_ERROR="\033[38;5;203m" # Soft red for errors
PASTEL_INFO="\033[38;5;117m" # Soft blue for info
PASTEL_HIGHLIGHT="\033[38;5;123m" # Soft cyan for verbose
PASTEL_TEXT="\033[1;37m" # White for text
PASTEL_SPECIAL="\033[38;5;114m" # Nice green for special items
PASTEL_GOLD="\033[38;5;220m" # Gold for credits
# ================================================================================
# DYNAMIC COLOR FUNCTIONS
# ================================================================================
get_color() {
local color_name="$1"
case "$COLOR_SCHEME" in
"STANDARD")
case "$color_name" in
"PRIMARY") echo "$STANDARD_PRIMARY" ;;
"SECONDARY") echo "$STANDARD_SECONDARY" ;;
"ACCENT") echo "$STANDARD_ACCENT" ;;
"SUCCESS") echo "$STANDARD_SUCCESS" ;;
"WARNING") echo "$STANDARD_WARNING" ;;
"ERROR") echo "$STANDARD_ERROR" ;;
"INFO") echo "$STANDARD_INFO" ;;
"HIGHLIGHT") echo "$STANDARD_HIGHLIGHT" ;;
"TEXT") echo "$STANDARD_TEXT" ;;
"SPECIAL") echo "$STANDARD_SPECIAL" ;;
"GOLD") echo "$STANDARD_ACCENT" ;;
*) echo "$NC" ;;
esac
;;
"HTB")
case "$color_name" in
"PRIMARY") echo "$HTB_PRIMARY" ;;
"SECONDARY") echo "$HTB_SECONDARY" ;;
"ACCENT") echo "$HTB_ACCENT" ;;
"SUCCESS") echo "$HTB_SUCCESS" ;;
"WARNING") echo "$HTB_WARNING" ;;
"ERROR") echo "$HTB_ERROR" ;;
"INFO") echo "$HTB_INFO" ;;
"HIGHLIGHT") echo "$HTB_HIGHLIGHT" ;;
"TEXT") echo "$HTB_TEXT" ;;
"SPECIAL") echo "$HTB_SPECIAL" ;;
"GOLD") echo "$HTB_ACCENT" ;;
*) echo "$NC" ;;
esac
;;
"PASTEL")
case "$color_name" in
"PRIMARY") echo "$PASTEL_PRIMARY" ;;
"SECONDARY") echo "$PASTEL_SECONDARY" ;;
"ACCENT") echo "$PASTEL_ACCENT" ;;
"SUCCESS") echo "$PASTEL_SUCCESS" ;;
"WARNING") echo "$PASTEL_WARNING" ;;
"ERROR") echo "$PASTEL_ERROR" ;;
"INFO") echo "$PASTEL_INFO" ;;
"HIGHLIGHT") echo "$PASTEL_HIGHLIGHT" ;;
"TEXT") echo "$PASTEL_TEXT" ;;
"SPECIAL") echo "$PASTEL_SPECIAL" ;;
"GOLD") echo "$PASTEL_GOLD" ;;
*) echo "$NC" ;;
esac
;;
*)
echo "$NC"
;;
esac
}
# Convenient color functions
c_primary() { get_color "PRIMARY"; }
c_secondary() { get_color "SECONDARY"; }
c_accent() { get_color "ACCENT"; }
c_success() { get_color "SUCCESS"; }
c_warning() { get_color "WARNING"; }
c_error() { get_color "ERROR"; }
c_info() { get_color "INFO"; }
c_highlight() { get_color "HIGHLIGHT"; }
c_text() { get_color "TEXT"; }
c_special() { get_color "SPECIAL"; }
c_gold() { get_color "GOLD"; }
set_theme() {
local theme_num="$1"
case "$theme_num" in
1)
COLOR_SCHEME="STANDARD"
;;
2)
COLOR_SCHEME="HTB"
;;
3)
COLOR_SCHEME="PASTEL"
;;
*)
echo "β Invalid theme number. Using default PASTEL theme."
COLOR_SCHEME="PASTEL"
;;
esac
}
# Function to show version information
show_version() {
echo ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)β$(c_secondary) π OLED Stats Installation Script $(c_primary)β${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e " $(c_accent)Version:${NC} $SCRIPT_VERSION${NC}"
echo -e " $(c_special)Script Author:${NC} $SCRIPT_AUTHOR${NC}"
echo -e " $(c_special)Original Code:${NC} $ORIGINAL_AUTHOR${NC}"
echo -e ""
echo -e " $(c_text)π Repository:${NC}"
echo -e " https://github.com/4ngel2769/rpi_oled_stats"
echo -e " $(c_text)π Original Code:${NC}"
echo -e " https://github.com/mklements/OLED_Stats"
echo -e ""
echo -e " $(c_text)About:${NC}"
echo -e " Automates OLED Stats Display installation for"
echo -e " Raspberry Pis, making it easy to set up for anyone."
echo -e ""
echo -e " $(c_gold)π Credits:${NC}"
echo -e " β’ Installation script by $SCRIPT_AUTHOR"
echo -e " β’ Original OLED Stats by $ORIGINAL_AUTHOR"
echo -e "${NC}"
echo ""
exit 0
}
# Function to show help information
show_help() {
echo ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)β$(c_secondary) π HELP & USAGE GUIDE $(c_primary)β${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "${NC} $(c_accent)Script:${NC} OLED Stats Installation Script $SCRIPT_VERSION${NC}"
echo -e "${NC} $(c_special)Author:${NC} $SCRIPT_AUTHOR${NC}"
echo -e "${NC} $(c_special)Original:${NC} $ORIGINAL_AUTHOR${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΈ${NC}"
echo -e "${NC} $(c_info)π USAGE:${NC}"
echo -e "${NC} curl -fsSL [URL] | bash [OPTIONS]${NC}"
echo -e ""
echo -e "${NC} $(c_accent)βοΈ OPTIONS:${NC}"
echo -e "${NC} $(c_special)-v, --verbose${NC} Enable detailed output${NC}"
echo -e "${NC} $(c_special)-u, --unattended${NC} Run in non-interactive mode (uses defaults)${NC}"
echo -e "${NC} $(c_special)-s, --silent${NC} Silent mode with progress bar only${NC}"
echo -e "${NC} $(c_special)-S, --skip-update${NC} Skip system package updates${NC}"
echo -e "${NC} $(c_special)-N, --no-reboot${NC} Disable automatic reboot${NC}"
echo -e "${NC} $(c_special)-t, --theme <1-3>${NC} Set color theme (1=Standard, 2=HTB, 3=Pastel)${NC}"
echo -e "${NC} $(c_special)-r, --rotation <1-2>${NC} Set display rotation (1=Normal, 2=Upside Down)${NC}"
echo -e "${NC} $(c_special)-V, --version${NC} Show version information${NC}"
echo -e "${NC} $(c_special)-h, --help${NC} Show this help message${NC}"
echo -e ""
echo -e "${NC} $(c_secondary)π¨ AVAILABLE THEMES:${NC}"
echo -e "${NC} $(c_special)1${NC} - STANDARD (Classic terminal colors)${NC}"
echo -e "${NC} $(c_special)2${NC} - HTB (Hack The Box cybersec style)${NC}"
echo -e "${NC} $(c_special)3${NC} - PASTEL (Soft and pleasant colors) [Default]${NC}"
echo -e ""
echo -e "${NC} $(c_secondary)UNATTENDED MODE:${NC}"
echo -e "${NC} Uses default settings for automated deployments:${NC}"
echo -e "${NC} - Default script: monitor.py (enhanced compatibility)${NC}"
echo -e "${NC} - Available scripts: stats.py, monitor.py, psutilstats.py, status.py${NC}"
echo -e "${NC} - No user prompts, automatic reboot${NC}"
echo -e "${NC} - Works well for scripts and CI/CD pipelines${NC}"
echo -e ""
echo -e "${NC} $(c_secondary)π‘ EXAMPLES:${NC}"
echo -e ""
echo -e "${NC} $(c_text) - Standard installation:${NC}"
echo -e "${NC} curl -fsSL https://raw.githubusercontent.com/4ngel2769/rpi_oled_stats/main/autoinstall_oled_stats.sh | bash${NC}"
echo -e ""
echo -e "${NC} $(c_text) - Unattended installation:${NC}"
echo -e "${NC} curl -fsSL https://raw.githubusercontent.com/4ngel2769/rpi_oled_stats/main/autoinstall_oled_stats.sh | bash -s -- -u${NC}"
echo -e ""
echo -e "${NC} $(c_text) - HTB theme with verbose:${NC}"
echo -e "${NC} curl -fsSL https://raw.githubusercontent.com/4ngel2769/rpi_oled_stats/main/autoinstall_oled_stats.sh | bash -s -- --theme 2 --verbose${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΈ${NC}"
echo ""
exit 0
}
# Progress bar function for silent mode
show_progress() {
local current=$1
local total=$2
local width=50
local percentage=$((current * 100 / total))
local completed=$((width * current / total))
local remaining=$((width - completed))
printf "\r["
printf "%${completed}s" | tr ' ' '='
printf "%${remaining}s" | tr ' ' '-'
printf "] %d%%" $percentage
}
# Helper function to update progress (prevents set -e from breaking again)
update_progress() {
((CURRENT_STEP++)) || true
if [ "$SILENT" = true ]; then
show_progress $CURRENT_STEP $TOTAL_STEPS
fi
}
print_silent() {
if [ "$SILENT" = true ]; then
return
fi
echo -e "$1"
}
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-v|--verbose)
VERBOSE=true
shift
;;
-u|--unattended)
UNATTENDED=true
shift
;;
-s|--silent)
SILENT=true
shift
;;
-S|--skip-update)
SKIP_APT_UPDATE=true
shift
;;
-N|--no-reboot)
AUTO_REBOOT=false
shift
;;
-t|--theme)
if [[ -n $2 && $2 =~ ^[1-3]$ ]]; then
set_theme "$2"
shift 2
else
echo "β Error: --theme requires a valid number (1-3)"
echo " 1 = Standard, 2 = HTB, 3 = Pastel"
exit 1
fi
;;
--theme=*)
theme_value="${1#*=}"
if [[ $theme_value =~ ^[1-3]$ ]]; then
set_theme "$theme_value"
shift
else
echo "β Error: --theme requires a valid number (1-3)"
echo " 1 = Standard, 2 = HTB, 3 = Pastel"
exit 1
fi
;;
-r|--rotation)
if [[ -n $2 && $2 =~ ^[12]$ ]]; then
ROTATION="$2"
shift 2
else
echo "β Error: --rotation requires 1 (normal) or 2 (upside down)"
exit 1
fi
;;
--rotation=*)
rotation_value="${1#*=}"
if [[ $rotation_value =~ ^[12]$ ]]; then
ROTATION="$rotation_value"
shift
else
echo "β Error: --rotation requires 1 (normal) or 2 (upside down)"
exit 1
fi
;;
-V|--version)
show_version
;;
-h|--help)
show_help
;;
*)
shift
;;
esac
done
# Override silent/verbose conflicts - verbose takes priority
if [ "$VERBOSE" = true ]; then
SILENT=false
elif [ "$SILENT" = true ]; then
VERBOSE=false
fi
# Function to print colored output
print_status() {
if [ "$SILENT" = false ]; then
echo -e "$(c_info)[<< INFO >>]${NC} $1"
fi
}
print_success() {
if [ "$SILENT" = false ]; then
echo -e "$(c_success)[<< SUCCESS]${NC} $1"
fi
}
print_warning() {
if [ "$SILENT" = false ]; then
echo -e "$(c_warning)[β οΈ WARNING]${NC} $1"
fi
}
print_error() {
# Always show errors, even in silent mode
echo -e "$(c_error)[β ERROR]${NC} $1"
}
print_verbose() {
if [ "$VERBOSE" = true ] && [ "$SILENT" = false ]; then
echo -e "$(c_highlight)[<< VERBOSE]${NC} $1"
fi
}
print_version_info() {
if [ "$SILENT" = false ]; then
echo ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)β$(c_secondary) π Starting OLED Installation... $(c_primary)β${NC}"
echo -e "$(c_primary)β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£${NC}"
echo -e "$(c_primary)β${NC} $(c_text)Script Version:${NC} $SCRIPT_VERSION $(c_primary)β${NC}"
echo -e "$(c_primary)β${NC} $(c_text)Script Author:${NC} $SCRIPT_AUTHOR $(c_primary)β${NC}"
echo -e "$(c_primary)β${NC} $(c_text)Original Code:${NC} $ORIGINAL_AUTHOR $(c_primary)β${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
fi
}
# Function to run commands with optional verbose output
run_command() {
local cmd="$1"
local description="$2"
if [ "$VERBOSE" = true ]; then
print_verbose "Running: $cmd"
eval "$cmd"
else
if [ -n "$description" ]; then
eval "$cmd" >/dev/null 2>&1 || {
print_error "$description failed"
exit 1
}
else
eval "$cmd" >/dev/null 2>&1
fi
fi
}
# Function to check if running on Raspberry Pi
check_raspberry_pi() {
print_verbose "π Checking if running on Raspberry Pi..."
if ! grep -q "Raspberry Pi" /proc/device-tree/model 2>/dev/null; then
print_error "π« This script is designed for Raspberry Pi only!"
exit 1
fi
if [ "$VERBOSE" = true ]; then
local pi_model
pi_model=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0')
print_verbose "Detected: $pi_model"
fi
print_success "Raspberry Pi detected"
}
# Check Pi model compatibility
check_pi_compatibility() {
print_verbose "Checking Raspberry Pi model compatibility..."
local pi_model
pi_model=$(cat /proc/device-tree/model 2>/dev/null | tr -d '\0')
print_verbose "Detected: $pi_model"
# Check for known compatible models
if echo "$pi_model" | grep -qE "(Raspberry Pi (3|4|5|Zero|Zero 2))"; then
print_success "Pi model is compatible with OLED Stats"
if [ "$VERBOSE" = true ]; then
case "$pi_model" in
# *"Pi 2"*) print_verbose "Pi 2 detected" ;;
*"Pi 3"*) print_verbose "Pi 3 detected" ;;
*"Pi 4"*) print_verbose "Pi 4 detected" ;;
*"Pi 5"*) print_verbose "Pi 5 detected" ;;
*"Zero"*) print_verbose "Pi Zero detected" ;;
esac
fi
else
print_warning "β οΈ Unknown Pi model. Script may work but is not tested on this model."
fi
return 0
}
# Verify Python library installation
verify_python_libraries() {
print_status "π§ͺ Verifying Python library installation..."
# Test if libraries can be imported
local test_script="
import sys
try:
import board
import busio
import digitalio
import adafruit_ssd1306
from PIL import Image, ImageDraw, ImageFont
import psutil
print('SUCCESS: All required libraries imported successfully')
sys.exit(0)
except ImportError as e:
print(f'ERROR: Failed to import library: {e}')
sys.exit(1)
except Exception as e:
print(f'ERROR: Unexpected error: {e}')
sys.exit(1)
"
print_verbose "π§ͺ Testing Python library imports..."
if sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && python3 -c '$test_script'" >/dev/null 2>&1; then
print_success "Python libraries verified successfully"
return 0
else
print_error "Python library verification failed"
if [ "$VERBOSE" = true ]; then
print_verbose "Library test output:"
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && python3 -c '$test_script'" || true
fi
return 1
fi
}
# Function to check if I2C is enabled
check_i2c_enabled() {
print_verbose "π Checking if I2C interface is enabled..."
if ! lsmod | grep -q i2c_bcm2835; then
if [ "$UNATTENDED" = true ]; then
print_warning "π§ I2C interface is not enabled. Attempting to enable automatically..."
# Enable I2C automatically in unattended mode
sudo raspi-config nonint do_i2c 0 >/dev/null 2>&1 || {
print_error "Failed to enable I2C automatically"
print_error "π‘ Please enable I2C manually using 'sudo raspi-config'"
return 1
}
print_success "I2C enabled automatically"
else
print_warning "I2C interface is not enabled. Please enable it manually using 'sudo raspi-config'"
print_warning "Go to: 3 Interfacing Options -> I5 I2C -> Yes -> Ok -> Finish"
read -p "β³ Press Enter after enabling I2C and rebooting..." < /dev/tty
fi
else
print_verbose "β
I2C interface is enabled"
fi
}
# Function to detect OLED display
detect_oled() {
print_status "π Checking for OLED display at address 0x3c..."
print_verbose "π©Ί Running i2cdetect to scan for devices..."
if [ "$VERBOSE" = true ]; then
echo "π I2C scan results:"
sudo i2cdetect -y 1
fi
if sudo i2cdetect -y 1 | grep -q "3c"; then
print_success "π OLED display detected at address 0x3c"
return 0
else
print_error "β OLED display not detected. Please check your connections:"
print_error " π GND -> Pin 9 (Ground)"
print_error " π VCC -> Pin 1 (3.3V)"
print_error " π SDA -> Pin 3 (GPIO 2)"
print_error " π SCL -> Pin 5 (GPIO 3)"
return 1
fi
}
# Get username (handle both pi and custom usernames)
get_username() {
if [ "$USER" = "root" ]; then
# If running as root, get the actual user
ACTUAL_USER=$(who am i | awk '{print $1}')
if [ -z "$ACTUAL_USER" ]; then
ACTUAL_USER="pi" # Default fallback
fi
else
ACTUAL_USER="$USER"
fi
echo "$ACTUAL_USER"
}
# Main installation function
main() {
# Show version info at start
print_version_info
echo ""
# Silent mode progress tracking
TOTAL_STEPS=10
CURRENT_STEP=0
if [ "$SILENT" = true ]; then
echo "π OLED Stats Installation - Silent Mode"
show_progress $CURRENT_STEP $TOTAL_STEPS
fi
if [ "$VERBOSE" = true ]; then
print_verbose "π§ Verbose mode enabled"
print_verbose "π¨ Using [$COLOR_SCHEME] theme"
print_verbose "π Script arguments: $*"
fi
if [ "$UNATTENDED" = true ]; then
print_status "Running in unattended mode with default settings"
print_verbose "Unattended mode: using monitor.py as default script"
if [ "$AUTO_REBOOT" = true ]; then
print_verbose "Unattended mode: will auto-reboot after installation"
else
print_verbose "Unattended mode: will NOT auto-reboot after installation"
fi
fi
print_status "π Starting OLED Stats Display installation..."
# Check if running on Raspberry Pi
check_raspberry_pi
# Check Pi model compatibility (no increment, part of same step)
check_pi_compatibility
# Now increment after both checks are done
update_progress
# Get the actual username
USERNAME=$(get_username)
HOME_DIR="/home/$USERNAME"
print_status "π€ Installing for user: $USERNAME"
print_status "π Home directory: $HOME_DIR"
print_verbose "π Current working directory: $(pwd)"
print_verbose "π€ Current user: $(whoami)"
# Step 2: Update system (conditional)
if [ "$SKIP_APT_UPDATE" = false ]; then
print_status "βοΈ Updating system packages..."
if [ "$VERBOSE" = true ]; then
sudo apt-get update
else
sudo apt-get update -qq
fi
print_success "π¦ System updated"
else
print_status "βοΈ Skipping system update (--skip-update flag enabled)"
fi
update_progress
# Step 3: Install required packages
print_status "π¦ Installing required packages..."
print_verbose "π¦ Installing: python3-pip python3-venv git i2c-tools"
if [ "$VERBOSE" = true ]; then
sudo apt-get install -y python3-pip python3-venv git i2c-tools
sudo apt-get install --upgrade python3-setuptools -y
else
sudo apt-get install -y python3-pip python3-venv git i2c-tools >/dev/null 2>&1
sudo apt-get install --upgrade python3-setuptools -y >/dev/null 2>&1
fi
print_success "π¦ Required packages installed"
update_progress
# Check I2C
check_i2c_enabled
# Step 4: Create virtual environment
print_status "π Creating Python virtual environment..."
cd "$HOME_DIR" || {
print_error "Failed to change to directory: $HOME_DIR"
exit 1
}
print_verbose "π Changed to directory: $HOME_DIR"
# Remove existing virtual environment if it exists
if [ -d "stats_env" ]; then
print_warning "ποΈ Existing stats_env found, removing..."
print_verbose "ποΈ Removing directory: $HOME_DIR/stats_env"
rm -rf stats_env
fi
print_verbose "π Creating virtual environment with system site packages..."
sudo -u "$USERNAME" python3 -m venv stats_env --system-site-packages
print_success "π Virtual environment created"
update_progress
# Step 5: Install Python libraries
print_status "π¦ Installing required Python libraries..."
print_verbose "π¦ Installing libraries directly in virtual environment..."
if [ "$VERBOSE" = true ]; then
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install --upgrade adafruit-blinka"
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install adafruit-circuitpython-ssd1306"
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install psutil"
sudo apt-get install -y python3-pil
else
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install --upgrade adafruit-blinka" >/dev/null 2>&1
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install adafruit-circuitpython-ssd1306" >/dev/null 2>&1
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && pip3 install psutil" >/dev/null 2>&1
sudo apt-get install -y python3-pil >/dev/null 2>&1
fi
print_success "π Python libraries installed"
update_progress
# Verify Python library installation
if ! verify_python_libraries; then
print_warning "Library verification failed, but continuing installation..."
fi
update_progress
# Step 6: Clone the repository
print_status "π₯ Downloading OLED Stats scripts..."
cd "$HOME_DIR" || {
print_error "Failed to change to directory: $HOME_DIR"
exit 1
}
# Remove existing directory if it exists
if [ -d "rpi_oled_stats" ]; then
print_warning "ποΈ Existing rpi_oled_stats directory found, removing..."
print_verbose "ποΈ Removing directory: $HOME_DIR/rpi_oled_stats"
sudo rm -rf rpi_oled_stats
fi
print_verbose "π₯ Cloning repository from GitHub..."
if [ "$VERBOSE" = true ]; then
sudo -u "$USERNAME" git clone https://github.com/4ngel2769/rpi_oled_stats.git rpi_oled_stats
else
sudo -u "$USERNAME" git clone https://github.com/4ngel2769/rpi_oled_stats.git rpi_oled_stats >/dev/null 2>&1
fi
cd rpi_oled_stats || {
print_error "Failed to change to directory: $HOME_DIR/rpi_oled_stats"
exit 1
}
print_verbose "π Changed to directory: $HOME_DIR/rpi_oled_stats"
# Download font files
if [ ! -f "PixelOperator.ttf" ]; then
print_status "π€ Downloading PixelOperator font..."
print_verbose "π₯ Font not found, downloading PixelOperator.ttf..."
sudo -u "$USERNAME" wget -q "https://github.com/mklements/OLED_Stats/raw/main/PixelOperator.ttf"
else
print_verbose "β
PixelOperator.ttf already exists"
fi
if [ ! -f "lineawesome-webfont.ttf" ]; then
print_status "π€ Downloading LineAwesome font..."
print_verbose "π₯ Font not found, downloading lineawesome-webfont.ttf..."
sudo -u "$USERNAME" wget -q "https://github.com/mklements/OLED_Stats/raw/main/lineawesome-webfont.ttf"
else
print_verbose "β
lineawesome-webfont.ttf already exists"
fi
print_success "π¦ Scripts downloaded"
update_progress
# Step 7: Detect OLED display
if ! detect_oled; then
print_warning "β οΈ OLED display not detected. The script will still create the startup configuration."
print_warning "π§ Please check your connections and the display should work after reboot."
fi
# Step 8: Choose and test the scripts
print_status "ποΈ Selecting OLED display script..."
if [ "$UNATTENDED" = true ] || [ "$SILENT" = true ]; then
DEFAULT_SCRIPT="monitor.py"
print_status "Using monitor.py as default"
else
# Interactive script selection
echo ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)β$(c_secondary) π SCRIPT SELECTION $(c_primary)β${NC}"
echo -e "$(c_primary)β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ£${NC}"
echo -e "$(c_primary)β${NC} $(c_special)1)${NC} stats.py - Simple text-based display $(c_primary)β${NC}"
echo -e "$(c_primary)β${NC} $(c_special)2)${NC} monitor.py - Display with icons $(c_primary)β${NC}"
echo -e "$(c_primary)β${NC} $(c_special)3)${NC} psutilstats.py - Enhanced compatibility $(c_primary)β${NC}"
echo -e "$(c_primary)β${NC} $(c_special)4)${NC} status.py - Clean exit & atexit enabled $(c_primary)β${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo ""
read -p "π― Which script would you like to use as default? (1-4): " SCRIPT_CHOICE < /dev/tty
SCRIPT_CHOICE=${SCRIPT_CHOICE:-$DEFAULT_SCRIPT_CHOICE}
case $SCRIPT_CHOICE in
1) DEFAULT_SCRIPT="stats.py" ;;
2) DEFAULT_SCRIPT="monitor.py" ;;
3) DEFAULT_SCRIPT="psutilstats.py" ;;
4) DEFAULT_SCRIPT="status.py" ;;
*)
print_warning "Invalid choice, using monitor.py as default"
DEFAULT_SCRIPT="monitor.py"
;;
esac
fi
print_verbose "π― Selected script: $DEFAULT_SCRIPT"
update_progress
# Test the selected script for 5 seconds if OLED was detected
if sudo i2cdetect -y 1 2>/dev/null | grep -q "3c" && [ "$SILENT" = false ]; then
print_status "π§ͺ Testing $DEFAULT_SCRIPT for 5 seconds..."
print_verbose "π§ͺ Running test command: timeout 5 python3 $DEFAULT_SCRIPT"
if [ "$VERBOSE" = true ]; then
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && cd $HOME_DIR/rpi_oled_stats && timeout 5 python3 $DEFAULT_SCRIPT || true"
else
sudo -u "$USERNAME" bash -c "source $HOME_DIR/stats_env/bin/activate && cd $HOME_DIR/rpi_oled_stats && timeout 5 python3 $DEFAULT_SCRIPT || true" >/dev/null 2>&1
fi
print_success "π§ͺ Script test completed"
else
print_status "βοΈ Skipping script test (OLED not detected or silent mode)"
fi
# Step 9: Create startup script
print_status "π Creating startup script..."
print_verbose "π Creating startup script at: $HOME_DIR/oled_display_start.sh"
cat > "$HOME_DIR/oled_display_start.sh" << EOF
#!/bin/bash
# OLED Stats Display Startup Script
# Generated by: OLED Stats Installation Script $SCRIPT_VERSION
# Script Author: $SCRIPT_AUTHOR
# Original Code: $ORIGINAL_AUTHOR
# Wait for system to fully boot
sleep 30
export OLED_ROTATION="$ROTATION"
source $HOME_DIR/stats_env/bin/activate
cd $HOME_DIR/rpi_oled_stats
python3 $DEFAULT_SCRIPT
EOF
chmod +x "$HOME_DIR/oled_display_start.sh"
chown "$USERNAME:$USERNAME" "$HOME_DIR/oled_display_start.sh"
print_success "π Startup script created"
update_progress
# Step 10: Setup auto-start
print_status "βοΈ Setting up auto-start on boot..."
# Add to crontab for the user
CRON_JOB="@reboot $HOME_DIR/oled_display_start.sh &"
print_verbose "β° Cron job: $CRON_JOB"
# Check if cron job already exists
if ! sudo -u "$USERNAME" crontab -l 2>/dev/null | grep -q "oled_display_start.sh"; then
print_verbose "β° Adding cron job for auto-start..."
(sudo -u "$USERNAME" crontab -l 2>/dev/null; echo "$CRON_JOB") | sudo -u "$USERNAME" crontab -
print_success "β° Auto-start configured"
else
print_warning "β° Auto-start already configured"
fi
update_progress
# Completion message
if [ "$SILENT" = true ]; then
echo "" # New line after progress bar
echo "β
Installation completed successfully!"
else
print_success "π Installation completed successfully!"
echo ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)β$(c_special) π INSTALLATION COMPLETE! $(c_primary)β${NC}"
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${NC}"
echo -e "$(c_primary)${NC} - Script version: $SCRIPT_VERSION${NC}"
echo -e "$(c_primary)${NC} - System updated${NC}"
echo -e "$(c_primary)${NC} - Required packages installed${NC}"
echo -e "$(c_primary)${NC} - Python libraries verified${NC}"
echo -e "$(c_primary)${NC} - Virtual environment created at: $HOME_DIR/stats_env${NC}"
echo -e "$(c_primary)${NC} - Scripts installed at: $HOME_DIR/rpi_oled_stats${NC}"
echo -e "$(c_primary)${NC} - Default script set to: $DEFAULT_SCRIPT${NC}"
echo -e "$(c_primary)${NC} - Display rotation: $ROTATION (1=Normal, 2=Upside Down)${NC}"
echo -e "$(c_primary)${NC} - Auto-start configured with 30-second boot delay${NC}"
if [ "$UNATTENDED" = false ]; then
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΈ${NC}"
echo -e "$(c_primary)$(c_accent) MANUAL COMMANDS${NC}"
echo -e ""
echo -e "$(c_primary)${NC} π Start manually:${NC}"
echo -e "$(c_primary)${NC} β° $HOME_DIR/oled_display_start.sh${NC}"
echo -e ""
echo -e "$(c_primary)${NC} βοΈ Change script:${NC}"
echo -e "$(c_primary)${NC} β° Edit $HOME_DIR/oled_display_start.sh${NC}"
echo -e ""
echo -e "$(c_primary)${NC} π The display will start automatically 30 seconds after boot.${NC}"
echo -e "$(c_primary)${NC} β° To change delay, edit: crontab -e${NC}"
fi
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΈ${NC}"
echo -e "$(c_primary)$(c_gold) π CREDITS${NC}"
echo -e ""
echo -e "$(c_primary)${NC} Installation script by $SCRIPT_AUTHOR${NC}"
echo -e "$(c_primary)${NC} Original OLED Stats code by $ORIGINAL_AUTHOR${NC}"
echo -e ""
echo -e "$(c_primary)ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββΈ${NC}"
echo ""
fi
if [ "$VERBOSE" = true ]; then
print_verbose "π» System information:"
print_verbose "π§ Kernel: $(uname -r)"
print_verbose "π₯οΈ OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2)"
print_verbose "π Python version: $(python3 --version)"
print_verbose "πΎ Disk space available: $(df -h $HOME_DIR | tail -1 | awk '{print $4}')"
fi
# Handle reboot
if [ "$AUTO_REBOOT" = true ]; then
if [ "$UNATTENDED" = true ] || [ "$SILENT" = true ]; then
print_status "Auto-rebooting in 10 seconds..."
sleep 10
sudo reboot
else
read -p "π Reboot now to start the display? (y/n): " REBOOT_CHOICE < /dev/tty
REBOOT_CHOICE=${REBOOT_CHOICE:-n}
if [[ $REBOOT_CHOICE =~ ^[Yy]$ ]]; then
print_status "π Rebooting system..."
sudo reboot
else
print_status "β
Installation complete. Reboot when ready."
print_status "π You can start the display manually with: $HOME_DIR/oled_display_start.sh"
print_status "π Or reboot to start automatically: sudo reboot"
fi
fi
else
print_status "β
Installation complete. No automatic reboot configured."
print_status "π You can start the display manually with: $HOME_DIR/oled_display_start.sh"
print_status "π Reboot manually when ready: sudo reboot"
fi
}
# Run main function
main "$@"