-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.sh
More file actions
executable file
·1742 lines (1524 loc) · 64.1 KB
/
update.sh
File metadata and controls
executable file
·1742 lines (1524 loc) · 64.1 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
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
#
# ██████╗ ██████╗ █████╗ ██████╗ ██╗ ██╗██╗ ██╗ ██████╗ ███████╗
# ██╔══██╗██╔══██╗██╔══██╗██╔══██╗██║ ██║╚██╗ ██╔╝██╔═══██╗██╔════╝
# ██║ ██║██████╔╝███████║██████╔╝███████║ ╚████╔╝ ██║ ██║███████╗
# ██║ ██║██╔══██╗██╔══██║██╔═══╝ ██╔══██║ ╚██╔╝ ██║ ██║╚════██║
# ██████╔╝██║ ██║██║ ██║██║ ██║ ██║ ██║ ╚██████╔╝███████║
# ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝
#
# draphyOS System Updater
# https://github.com/draphy/draphyOS
#
# A safe, interactive system update tool for Fedora
#
# Options:
# --vm Enable VM mode (xrender backend for picom)
# --no-vm Disable VM mode (glx backend for picom)
#
set -u
# Validate HOME directory
if [ -z "${HOME:-}" ] || [ ! -d "$HOME" ]; then
echo "ERROR: HOME directory not set or does not exist"
exit 1
fi
if [ ! -w "$HOME" ]; then
echo "ERROR: HOME directory is not writable"
exit 1
fi
# draphyOS paths
INSTALL_DIR="$HOME/.draphyOS"
MARKER_FILE="$HOME/.draphyOS-installed"
CHECKSUM_FILE="$HOME/.draphyOS-config-checksums"
UPGRADE_STATE_FILE="$HOME/.draphyOS-upgrade-state"
# Colors (with fallback for non-color terminals)
if [[ -t 1 ]] && [[ "${TERM:-dumb}" != "dumb" ]]; then
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
MINT='\033[38;2;154;184;124m'
BOLD='\033[1m'
NC='\033[0m'
else
RED=''
GREEN=''
YELLOW=''
BLUE=''
CYAN=''
MINT=''
BOLD=''
NC=''
fi
# Print functions (matching install.sh style)
print_header() {
clear
echo ""
echo -e "${MINT}"
echo " ██████╗ ██████╗ █████╗ ██████╗ ██╗ ██╗██╗ ██╗ ██████╗ ███████╗"
echo " ██╔══██╗██╔══██╗██╔══██╗██╔══██╗██║ ██║╚██╗ ██╔╝██╔═══██╗██╔════╝"
echo " ██║ ██║██████╔╝███████║██████╔╝███████║ ╚████╔╝ ██║ ██║███████╗"
echo " ██║ ██║██╔══██╗██╔══██║██╔═══╝ ██╔══██║ ╚██╔╝ ██║ ██║╚════██║"
echo " ██████╔╝██║ ██║██║ ██║██║ ██║ ██║ ██║ ╚██████╔╝███████║"
echo " ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚══════╝"
echo -e "${NC}"
echo -e " ${CYAN}System Updater${NC}"
echo ""
}
print_step() {
echo -e "${GREEN}[*]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[!]${NC} $1"
}
print_error() {
echo -e "${RED}[✗]${NC} $1"
}
print_success() {
echo -e "${GREEN}[✓]${NC} $1"
}
print_info() {
echo -e "${CYAN}[i]${NC} $1"
}
# Get system information
get_system_info() {
FEDORA_VERSION=$(rpm -E %fedora 2>/dev/null || echo "unknown")
KERNEL_VERSION=$(uname -r)
LAST_UPDATE=$(rpm -qa --last 2>/dev/null | head -1 | awk '{print $2, $3, $4}' || echo "unknown")
}
# Display system status with proper alignment
show_system_status() {
get_system_info
# Calculate dynamic box width based on content
local header="System Information"
local fedora_text="Fedora Version: $FEDORA_VERSION"
local kernel_text="Kernel: $KERNEL_VERSION"
local update_text="Last Package Change: $LAST_UPDATE"
# Find the longest line (add 4 for " " padding on each side)
local max_len=${#header}
[ ${#fedora_text} -gt $max_len ] && max_len=${#fedora_text}
[ ${#kernel_text} -gt $max_len ] && max_len=${#kernel_text}
[ ${#update_text} -gt $max_len ] && max_len=${#update_text}
# Box width = max content + 4 (2 spaces padding each side)
local box_width=$((max_len + 4))
# Minimum width of 49 for aesthetics
[ $box_width -lt 49 ] && box_width=49
# Generate horizontal border
local border=""
for ((i=0; i<box_width; i++)); do border+="─"; done
echo -e " ┌${border}┐"
printf " │ ${BOLD}%-$((box_width - 4))s${NC} │\n" "$header"
echo -e " ├${border}┤"
# Content lines with dynamic padding
# Format: " │ <label>: " + value + " │" = box_width inside
printf " │ Fedora Version: ${GREEN}%-$((box_width - 20))s${NC} │\n" "$FEDORA_VERSION"
printf " │ Kernel: ${CYAN}%-$((box_width - 12))s${NC} │\n" "$KERNEL_VERSION"
printf " │ Last Package Change: ${YELLOW}%-$((box_width - 25))s${NC} │\n" "$LAST_UPDATE"
echo -e " └${border}┘"
echo ""
}
# Check if running as root (we need sudo, not root)
check_not_root() {
if [ "$(id -u)" -eq 0 ]; then
print_error "Please run this script as a normal user, not as root."
print_info "The script will ask for sudo when needed."
exit 1
fi
}
# Check and get sudo access
check_sudo() {
print_step "Checking sudo access..."
if ! sudo -v 2>/dev/null; then
print_error "Sudo access required."
exit 1
fi
# Keep sudo alive in background (with error handling)
(while true; do sudo -v 2>/dev/null || break; sleep 50; done) &
SUDO_KEEPER_PID=$!
# Cleanup function that handles sudo keeper
cleanup_sudo() {
if [ -n "${SUDO_KEEPER_PID:-}" ] && kill -0 "$SUDO_KEEPER_PID" 2>/dev/null; then
kill "$SUDO_KEEPER_PID" 2>/dev/null
fi
}
trap cleanup_sudo EXIT INT TERM
print_success "Sudo access OK"
echo ""
}
# Check for updates without installing
check_updates() {
print_step "Checking for available updates..."
echo ""
local updates
updates=$(dnf check-update 2>/dev/null)
local exit_code=$?
# dnf check-update returns 100 if updates available, 0 if none, 1 on error
if [ $exit_code -eq 100 ]; then
local count
count=$(echo "$updates" | grep -v "^$" | grep -v "^Last metadata" | grep -v "^Obsoleting" | wc -l)
print_info "Found ${YELLOW}$count${NC} packages with updates available"
echo ""
echo "$updates" | head -30
if [ "$count" -gt 30 ]; then
echo -e "${YELLOW}... and $((count - 30)) more${NC}"
fi
return 0
elif [ $exit_code -eq 0 ]; then
print_success "System is up to date! No updates available."
return 1
else
print_error "Error checking for updates"
return 1
fi
}
# Update all packages
do_update() {
print_step "Preparing to update packages..."
echo ""
# First check what updates are available
print_info "Refreshing package metadata..."
local refresh_exit_code
sudo dnf check-update --refresh 2>/dev/null
refresh_exit_code=$?
# Exit code 100 means updates available, 0 means no updates, other = error
if [ $refresh_exit_code -ne 100 ] && [ $refresh_exit_code -ne 0 ]; then
print_warning "Could not refresh metadata, continuing anyway..."
fi
echo ""
# Confirm with user
echo -e "Do you want to ${GREEN}install all available updates${NC}? (y/n)"
read -r response </dev/tty || response="n"
if [[ ! "$response" =~ ^[Yy](es)?$ ]]; then
print_step "Update cancelled"
return
fi
echo ""
print_step "Installing updates..."
print_info "This may take a while depending on the number of updates."
echo ""
if sudo dnf upgrade --refresh -y; then
print_success "Updates installed successfully!"
else
print_error "Some updates may have failed. Check the output above."
fi
# Update Flatpak apps if installed
if command -v flatpak &>/dev/null; then
local flatpak_count
flatpak_count=$(flatpak list --app --columns=application 2>/dev/null | wc -l)
if [ "$flatpak_count" -gt 0 ]; then
echo ""
print_step "Updating Flatpak apps ($flatpak_count installed)..."
if flatpak update -y 2>/dev/null; then
print_success "Flatpak apps updated"
else
print_warning "Some Flatpak apps may have failed to update"
fi
fi
fi
check_reboot_needed
}
# Security updates only
do_security_update() {
print_step "Checking for security updates..."
echo ""
# Check if security updates are available
local sec_updates
sec_updates=$(dnf updateinfo list --security 2>/dev/null | grep -v "^Last metadata" | grep -v "^$")
if [ -z "$sec_updates" ]; then
print_success "No security updates available."
return
fi
print_info "Security updates available:"
echo "$sec_updates" | head -20
echo ""
echo -e "Do you want to install ${RED}security updates only${NC}? (y/n)"
read -r response </dev/tty || response="n"
if [[ ! "$response" =~ ^[Yy](es)?$ ]]; then
print_step "Security update cancelled"
return
fi
echo ""
print_step "Installing security updates..."
if sudo dnf upgrade --security --refresh -y; then
print_success "Security updates installed!"
else
print_error "Some security updates may have failed."
fi
check_reboot_needed
}
# Full maintenance (update + clean + autoremove)
do_full_maintenance() {
print_step "Full System Maintenance"
echo ""
print_info "This will:"
echo " 1. Update all packages"
echo " 2. Remove orphaned packages (with your approval)"
echo " 3. Update Flatpak apps (if installed)"
echo " 4. Clean package cache"
echo ""
echo -e "Do you want to proceed with ${CYAN}full maintenance${NC}? (y/n)"
read -r response </dev/tty || response="n"
if [[ ! "$response" =~ ^[Yy](es)?$ ]]; then
print_step "Maintenance cancelled"
return
fi
echo ""
# Step 1: Update packages
print_step "Step 1/4: Updating packages..."
if sudo dnf upgrade --refresh -y; then
print_success "Packages updated"
else
print_warning "Some packages may have failed to update"
fi
echo ""
# Step 2: Check for orphaned packages
print_step "Step 2/4: Checking for orphaned packages..."
local orphans
orphans=$(dnf repoquery --unneeded 2>/dev/null | head -50)
if [ -n "$orphans" ]; then
local orphan_count
orphan_count=$(echo "$orphans" | wc -l)
print_info "Found ${YELLOW}$orphan_count${NC} orphaned packages:"
echo "$orphans" | head -20
if [ "$orphan_count" -gt 20 ]; then
echo -e " ${YELLOW}... and $((orphan_count - 20)) more${NC}"
fi
echo ""
echo -e "Do you want to ${RED}remove orphaned packages${NC}? (y/n)"
read -r confirm </dev/tty || confirm="n"
if [[ "$confirm" =~ ^[Yy](es)?$ ]]; then
print_step "Removing orphaned packages..."
if sudo dnf autoremove -y; then
print_success "Orphaned packages removed"
else
print_warning "Some packages could not be removed"
fi
else
print_step "Keeping orphaned packages"
fi
else
print_success "No orphaned packages found"
fi
echo ""
# Step 3: Update Flatpak apps
if command -v flatpak &>/dev/null; then
local flatpak_count
flatpak_count=$(flatpak list --app --columns=application 2>/dev/null | wc -l)
if [ "$flatpak_count" -gt 0 ]; then
print_step "Step 3/4: Updating Flatpak apps ($flatpak_count installed)..."
if flatpak update -y 2>/dev/null; then
print_success "Flatpak apps updated"
else
print_warning "Some Flatpak apps may have failed to update"
fi
else
print_step "Step 3/4: No Flatpak apps installed, skipping"
fi
else
print_step "Step 3/4: Flatpak not installed, skipping"
fi
echo ""
# Step 4: Clean cache
print_step "Step 4/4: Cleaning package cache..."
if sudo dnf clean all >/dev/null 2>&1; then
print_success "Package cache cleaned"
else
print_warning "Could not clean cache"
fi
# Clean unused Flatpak runtimes
if command -v flatpak &>/dev/null; then
flatpak uninstall --unused -y 2>/dev/null || true
fi
echo ""
print_success "Full maintenance complete!"
check_reboot_needed
}
# Fedora version upgrade
do_version_upgrade() {
print_header
echo -e " ${BOLD}${CYAN}Fedora Version Upgrade${NC}"
echo ""
get_system_info
print_info "Current Fedora version: ${GREEN}$FEDORA_VERSION${NC}"
echo ""
# Important warnings
echo -e " ${YELLOW}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${YELLOW}║ ${BOLD}IMPORTANT: Read before proceeding${NC} ${YELLOW}║${NC}"
echo -e " ${YELLOW}╠═══════════════════════════════════════════════════════════╣${NC}"
echo -e " ${YELLOW}║${NC} • Back up your important data before upgrading ${YELLOW}║${NC}"
echo -e " ${YELLOW}║${NC} • Ensure you have a stable internet connection ${YELLOW}║${NC}"
echo -e " ${YELLOW}║${NC} • The system will reboot during the upgrade ${YELLOW}║${NC}"
echo -e " ${YELLOW}║${NC} • Upgrade may take 30-60 minutes depending on system ${YELLOW}║${NC}"
echo -e " ${YELLOW}║${NC} • Fedora recommends upgrading one version at a time ${YELLOW}║${NC}"
echo -e " ${YELLOW}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
# Calculate available versions
local current_ver=$FEDORA_VERSION
local next_ver=$((current_ver + 1))
local next_next_ver=$((current_ver + 2))
echo -e "Available upgrade targets:"
echo -e " ${GREEN}1)${NC} Fedora ${next_ver} (recommended - next version)"
echo -e " ${YELLOW}2)${NC} Fedora ${next_next_ver} (skip one version - not officially recommended)"
echo -e " ${CYAN}3)${NC} Enter custom version number"
echo -e " ${RED}4)${NC} Cancel"
echo ""
echo -n "Select option [1-4]: "
read -r choice </dev/tty || choice="4"
local target_version=""
case "$choice" in
1) target_version=$next_ver ;;
2) target_version=$next_next_ver ;;
3)
echo -n "Enter target Fedora version number: "
read -r target_version </dev/tty || target_version=""
if ! [[ "$target_version" =~ ^[0-9]+$ ]]; then
print_error "Invalid version number"
return
fi
;;
*)
print_step "Version upgrade cancelled"
return
;;
esac
if [ -z "$target_version" ]; then
print_error "No version selected"
return
fi
if [ "$target_version" -le "$current_ver" ]; then
print_error "Target version must be higher than current version ($current_ver)"
return
fi
echo ""
print_info "Target version: Fedora ${GREEN}$target_version${NC}"
echo ""
# Final confirmation
echo -e "${RED}${BOLD}WARNING:${NC} You are about to upgrade from Fedora $current_ver to Fedora $target_version"
echo -e "This process will ${YELLOW}download packages${NC} and then ${YELLOW}reboot your system${NC}."
echo ""
echo -e "Type ${GREEN}yes${NC} to confirm upgrade (anything else cancels): "
read -r confirm </dev/tty || confirm="no"
if [ "$confirm" != "yes" ]; then
print_step "Upgrade cancelled"
return
fi
echo ""
# Step 1: Update current system first (required)
print_step "Step 1/4: Updating current system (required before upgrade)..."
if ! sudo dnf upgrade --refresh -y; then
print_error "Failed to update current system. Please fix any issues and try again."
return
fi
print_success "Current system updated"
echo ""
# Check if reboot needed before upgrade
if dnf needs-restarting -r >/dev/null 2>&1; then
: # No reboot needed, continue
else
print_warning "A reboot is recommended before upgrading."
echo -e "Do you want to ${YELLOW}reboot now${NC} and run the upgrade after? (y/n)"
read -r reboot_first </dev/tty || reboot_first="n"
if [[ "$reboot_first" =~ ^[Yy](es)?$ ]]; then
print_info "Please run 'update-draphyOS' again after reboot to continue the upgrade."
echo ""
echo -e "Press Enter to reboot..."
read -r </dev/tty
sudo systemctl reboot
exit 0
fi
fi
# Step 2: Download upgrade packages
print_step "Step 2/4: Downloading upgrade packages for Fedora $target_version..."
print_info "This may take a while depending on your internet connection."
echo ""
local download_output
download_output=$(mktemp)
chmod 600 "$download_output" # Restrict permissions on temp file
# Use subshell with pipefail to capture dnf exit code correctly
local dnf_exit=0
(
set -o pipefail
sudo dnf system-upgrade download --releasever="$target_version" -y 2>&1 | tee "$download_output"
) || dnf_exit=$?
if [ "$dnf_exit" -ne 0 ]; then
# Check if version doesn't exist (catch 404 errors, missing repos, etc.)
if grep -qiE "unable to find.*releasever|no such.*version|not found|status code: 404|Failed to download metadata|Cannot prepare internal mirrorlist" "$download_output" 2>/dev/null; then
print_error "Fedora $target_version is not available yet."
print_info "This version may not be released or may not be supported for upgrade."
echo ""
# Suggest the latest available version
local latest_stable=$((FEDORA_VERSION + 1))
print_info "Try Fedora $latest_stable if it's available, or wait for the release."
rm -f "$download_output"
return
fi
print_error "Failed to download upgrade packages."
print_info "This may be due to package conflicts that need resolution."
echo ""
echo -e " ${RED}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${RED}║ ${BOLD}WARNING: --allowerasing option${NC} ${RED}║${NC}"
echo -e " ${RED}╠═══════════════════════════════════════════════════════════╣${NC}"
echo -e " ${RED}║${NC} This option will REMOVE conflicting packages to proceed. ${RED}║${NC}"
echo -e " ${RED}║${NC} Some applications you use may be removed! ${RED}║${NC}"
echo -e " ${RED}║${NC} Review the list carefully before confirming. ${RED}║${NC}"
echo -e " ${RED}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "Do you want to retry with ${YELLOW}--allowerasing${NC}? (y/n)"
read -r retry </dev/tty || retry="n"
if [[ "$retry" =~ ^[Yy](es)?$ ]]; then
print_warning "Using --allowerasing - review removed packages carefully!"
# Use subshell with pipefail to capture dnf exit code correctly
local dnf_exit=0
(
set -o pipefail
sudo dnf system-upgrade download --releasever="$target_version" --allowerasing -y 2>&1 | tee "$download_output"
) || dnf_exit=$?
if [ "$dnf_exit" -ne 0 ]; then
if grep -qiE "unable to find.*releasever|no such.*version|not found|status code: 404|Failed to download metadata|Cannot prepare internal mirrorlist" "$download_output" 2>/dev/null; then
print_error "Fedora $target_version is not available."
rm -f "$download_output"
return
fi
print_error "Upgrade download failed. Please check the errors above."
rm -f "$download_output"
return
fi
else
print_step "Upgrade cancelled"
rm -f "$download_output"
return
fi
fi
rm -f "$download_output"
print_success "Upgrade packages downloaded"
echo ""
# Step 3: Inform user about the upgrade process
print_step "Step 3/4: Ready to upgrade"
echo ""
echo -e " ${CYAN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${CYAN}║ ${BOLD}What happens next:${NC} ${CYAN}║${NC}"
echo -e " ${CYAN}╠═══════════════════════════════════════════════════════════╣${NC}"
echo -e " ${CYAN}║${NC} 1. Your system will reboot ${CYAN}║${NC}"
echo -e " ${CYAN}║${NC} 2. Upgrade will run (you'll see progress on screen) ${CYAN}║${NC}"
echo -e " ${CYAN}║${NC} 3. System will reboot again when complete ${CYAN}║${NC}"
echo -e " ${CYAN}║${NC} 4. You'll be running Fedora $target_version! ${CYAN}║${NC}"
echo -e " ${CYAN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e " ${GREEN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${GREEN}║ ${BOLD}After upgrade completes, run:${NC} ${GREEN}║${NC}"
echo -e " ${GREEN}╠═══════════════════════════════════════════════════════════╣${NC}"
echo -e " ${GREEN}║${NC} ${GREEN}║${NC}"
echo -e " ${GREEN}║${NC} ${BOLD}update-draphyOS --resume${NC} ${GREEN}║${NC}"
echo -e " ${GREEN}║${NC} ${GREEN}║${NC}"
echo -e " ${GREEN}║${NC} This will clean up upgrade cache and remove orphans. ${GREEN}║${NC}"
echo -e " ${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
# Step 4: Trigger upgrade reboot
print_step "Step 4/4: Starting upgrade..."
echo ""
echo -e "Do you want to ${GREEN}reboot and start the upgrade now${NC}? (y/n)"
read -r start_upgrade </dev/tty || start_upgrade="n"
if [[ "$start_upgrade" =~ ^[Yy](es)?$ ]]; then
# Save state for resume after reboot
save_upgrade_state "fedora-upgrade" "$target_version"
print_info "Starting upgrade reboot..."
echo ""
echo -e "${GREEN}The system will now reboot to perform the upgrade.${NC}"
echo -e "${YELLOW}After upgrade completes, run: ${BOLD}update-draphyOS --resume${NC}"
echo ""
echo -e "Press Enter to continue..."
read -r </dev/tty
# Use dnf5 offline reboot for Fedora 41+ (DNF5)
if command -v dnf5 &>/dev/null; then
sudo dnf5 offline reboot
else
sudo dnf system-upgrade reboot
fi
else
# Save state anyway in case user reboots manually
save_upgrade_state "fedora-upgrade" "$target_version"
print_info "Upgrade packages are downloaded and ready."
echo ""
echo -e "To start the upgrade, run:"
echo -e " ${CYAN}sudo dnf system-upgrade reboot${NC}"
echo ""
echo -e "After upgrade completes, run:"
echo -e " ${GREEN}update-draphyOS --resume${NC}"
echo ""
echo -e "To cancel the pending upgrade:"
echo -e " ${CYAN}sudo dnf system-upgrade clean${NC}"
fi
}
# Save upgrade state for resume after reboot
save_upgrade_state() {
local operation="$1"
local target_version="$2"
echo "OPERATION=$operation" > "$UPGRADE_STATE_FILE"
echo "TARGET_VERSION=$target_version" >> "$UPGRADE_STATE_FILE"
echo "TIMESTAMP=$(date +%s)" >> "$UPGRADE_STATE_FILE"
}
# Clear upgrade state
clear_upgrade_state() {
rm -f "$UPGRADE_STATE_FILE"
}
# Check for pending upgrade and resume
check_and_resume_upgrade() {
if [ ! -f "$UPGRADE_STATE_FILE" ]; then
return 1
fi
local operation target_version
operation=$(grep "^OPERATION=" "$UPGRADE_STATE_FILE" 2>/dev/null | cut -d= -f2)
target_version=$(grep "^TARGET_VERSION=" "$UPGRADE_STATE_FILE" 2>/dev/null | cut -d= -f2)
if [ -z "$operation" ]; then
clear_upgrade_state
return 1
fi
print_header
echo -e " ${CYAN}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${CYAN}║ ${BOLD}Pending Operation Detected${NC} ${CYAN}║${NC}"
echo -e " ${CYAN}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
case "$operation" in
"fedora-upgrade")
print_info "Fedora upgrade to version $target_version was in progress"
echo ""
do_post_upgrade_cleanup "$target_version"
;;
*)
print_warning "Unknown operation: $operation"
clear_upgrade_state
;;
esac
return 0
}
# Post-upgrade cleanup after Fedora version upgrade
do_post_upgrade_cleanup() {
local target_version="$1"
get_system_info
print_info "Current Fedora version: ${GREEN}$FEDORA_VERSION${NC}"
echo ""
if [ "$FEDORA_VERSION" = "$target_version" ]; then
print_success "Fedora $target_version upgrade completed successfully!"
echo ""
else
print_warning "Expected version $target_version, but running $FEDORA_VERSION"
echo ""
fi
print_step "Running post-upgrade cleanup..."
echo ""
# Clean up upgrade cache
print_info "Cleaning system-upgrade cache..."
if sudo dnf system-upgrade clean 2>/dev/null || sudo dnf5 offline clean 2>/dev/null; then
print_success "Upgrade cache cleaned"
else
print_info "No upgrade cache to clean"
fi
echo ""
# Offer to remove orphaned packages
local orphans
orphans=$(dnf repoquery --unneeded 2>/dev/null | head -20)
if [ -n "$orphans" ]; then
print_info "Orphaned packages found after upgrade:"
echo "$orphans" | head -10
echo ""
echo -e "Do you want to ${YELLOW}remove orphaned packages${NC}? (y/n)"
read -r confirm </dev/tty || confirm="n"
if [[ "$confirm" =~ ^[Yy](es)?$ ]]; then
sudo dnf autoremove -y
print_success "Orphaned packages removed"
fi
fi
clear_upgrade_state
echo ""
print_success "Post-upgrade cleanup complete!"
echo ""
print_info "Your system is now running Fedora $FEDORA_VERSION"
}
# Check if reboot is needed
check_reboot_needed() {
echo ""
print_step "Checking if reboot is required..."
# Check for kernel updates or other reboot-requiring changes
# dnf needs-restarting -r returns 1 if reboot needed, 0 if not
if ! dnf needs-restarting -r >/dev/null 2>&1; then
echo ""
echo -e " ${YELLOW}╔═══════════════════════════════════════════════════════════╗${NC}"
echo -e " ${YELLOW}║ ${BOLD}Reboot Required${NC} ${YELLOW}║${NC}"
echo -e " ${YELLOW}╠═══════════════════════════════════════════════════════════╣${NC}"
echo -e " ${YELLOW}║${NC} A system reboot is needed to apply all changes. ${YELLOW}║${NC}"
echo -e " ${YELLOW}║${NC} This is usually due to kernel or core library updates. ${YELLOW}║${NC}"
echo -e " ${YELLOW}╚═══════════════════════════════════════════════════════════╝${NC}"
echo ""
echo -e "Do you want to ${CYAN}reboot now${NC}? (y/n)"
read -r reboot_now </dev/tty || reboot_now="n"
if [[ "$reboot_now" =~ ^[Yy](es)?$ ]]; then
print_info "Rebooting system..."
sudo systemctl reboot
else
print_info "Please remember to reboot your system soon."
fi
else
# Check for services that need restart
local services
services=$(dnf needs-restarting -s 2>/dev/null | head -10)
if [ -n "$services" ]; then
print_info "Some services may need to be restarted:"
echo "$services"
echo ""
print_info "You can restart them manually or reboot when convenient."
else
print_success "No reboot required. System is up to date!"
fi
fi
}
# ============================================================
# draphyOS Config Update Functions
# ============================================================
# Get installed draphyOS version
get_draphyos_version() {
if [ -f "$MARKER_FILE" ]; then
local version
version=$(grep "^DRAPHYOS_VERSION=" "$MARKER_FILE" 2>/dev/null | cut -d= -f2)
# Return 0 if version line doesn't exist (old installations)
echo "${version:-0}"
else
echo "0"
fi
}
# Get latest draphyOS version from repo
get_latest_version() {
if [ -f "$INSTALL_DIR/VERSION" ]; then
tr -d '[:space:]' < "$INSTALL_DIR/VERSION"
else
echo "0"
fi
}
# Check if a config file has been modified by user
# Compares user's current file against the stored checksum of what was installed
is_config_modified() {
local config_name="$1" # e.g., "i3/config"
local user_file="$2" # Full path to user's config
# If user file doesn't exist, not modified
[ ! -f "$user_file" ] && return 1
# If it's a symlink to draphyOS, not modified
if [ -L "$user_file" ]; then
local target
target=$(readlink -f "$user_file" 2>/dev/null)
if [[ "$target" == *"draphyOS"* ]]; then
return 1 # Not modified (still a symlink)
fi
fi
# Check against stored checksum (format: config_name:checksum)
if [ -f "$CHECKSUM_FILE" ]; then
local stored_sum
stored_sum=$(grep "^${config_name}:" "$CHECKSUM_FILE" 2>/dev/null | cut -d: -f2)
if [ -n "$stored_sum" ]; then
local current_sum
current_sum=$(md5sum "$user_file" 2>/dev/null | cut -d' ' -f1)
if [ "$stored_sum" != "$current_sum" ]; then
return 0 # Modified
fi
return 1 # Not modified (matches what we installed)
fi
fi
# Default: assume modified if it's a real file (not symlink)
return 0
}
# Three-way merge a config file
# Returns: 0=success (auto-merged or no changes), 1=conflict needs user input, 2=error
three_way_merge() {
local config_name="$1" # e.g., "i3/config"
local user_file="$2" # User's current file
local new_file="$3" # New version from repo
local original_file="$4" # Original file (pre-update repo version, contains user's changes if any)
# If user file doesn't exist, just use new version
if [ ! -f "$user_file" ] && [ ! -L "$user_file" ]; then
return 0
fi
# If original doesn't exist or is empty, can't do three-way merge
if [ -z "$original_file" ] || [ ! -f "$original_file" ]; then
return 1
fi
# Handle symlinks: compare original (user's version before git reset) vs new
# If user_file is a symlink, the user was editing the repo file directly
# original_file contains what the user had, new_file has upstream changes
if [ -L "$user_file" ]; then
# Check if user modified the symlinked file (original vs new)
if diff -q "$original_file" "$new_file" >/dev/null 2>&1; then
# No difference - user didn't modify or changes are same as upstream
return 0
fi
# User had changes that differ from upstream - conflict!
return 1
fi
# For regular files: check if user modified
if ! is_config_modified "$config_name" "$user_file"; then
# Not modified, safe to replace
return 0
fi
# Check if new version is different from original
if diff -q "$original_file" "$new_file" >/dev/null 2>&1; then
# No upstream changes, keep user's version
print_info " Keeping your changes: $config_name (no upstream changes)"
return 0
fi
# Both modified - try three-way merge
local merge_output
merge_output=$(mktemp)
if diff3 -m "$user_file" "$original_file" "$new_file" > "$merge_output" 2>/dev/null; then
# Clean merge - no conflicts
print_success " Auto-merged: $config_name"
cp "$merge_output" "$user_file"
rm -f "$merge_output"
return 0
else
# Conflicts exist
rm -f "$merge_output"
return 1
fi
}
# Handle a single config file conflict
handle_config_conflict() {
local config_name="$1"
local user_file="$2"
local new_file="$3"
local original_file="$4" # User's version saved before git reset
# Use original_file for comparison (user_file may be symlink pointing to new version)
local user_version="$original_file"
[ ! -f "$user_version" ] && user_version="$user_file"
# Use loop instead of recursion to avoid stack depth issues
while true; do
echo ""
# Dynamic box width based on config name length
local label="Conflict in: $config_name"
local box_width=${#label}
[ $box_width -lt 55 ] && box_width=55 # Minimum width
box_width=$((box_width + 4)) # Add padding
local border=""
for ((i=0; i<box_width; i++)); do border+="═"; done
echo -e " ${YELLOW}╔${border}╗${NC}"
printf " ${YELLOW}║${NC} Conflict in: ${BOLD}%-$((box_width - 18))s${NC} ${YELLOW}║${NC}\n" "$config_name"
echo -e " ${YELLOW}╚${border}╝${NC}"
echo ""
# Show diff summary (your version vs new version)
echo -e " ${CYAN}Changes summary (your version → new version):${NC}"
diff --color=auto -u "$user_version" "$new_file" 2>/dev/null | head -30
local diff_lines
diff_lines=$(diff -u "$user_version" "$new_file" 2>/dev/null | wc -l)
if [ "$diff_lines" -gt 30 ]; then
echo -e " ${YELLOW}... ($((diff_lines - 30)) more lines)${NC}"
fi
echo ""
echo -e " How do you want to resolve this?"
echo -e " ${GREEN}1)${NC} Keep my version (ignore upstream changes)"
echo -e " ${CYAN}2)${NC} Use new version (discard my changes)"
echo -e " ${YELLOW}3)${NC} Backup mine and use new version"
echo -e " ${BLUE}4)${NC} Show full diff"
echo ""
echo -n " Choice [1-4]: "
read -r choice </dev/tty || choice="1"
case "$choice" in
1)
# Restore user's version (convert symlink to regular file if needed)
rm -f "$user_file" 2>/dev/null
cp "$user_version" "$user_file"
print_step " Keeping your version of $config_name"
return 0
;;
2)
rm -f "$user_file" 2>/dev/null
cp "$new_file" "$user_file"
print_success " Updated to new version: $config_name"
return 0
;;
3)
local backup_file="${user_file}.backup-$(date +%Y%m%d%H%M%S)"
cp "$user_version" "$backup_file"
rm -f "$user_file" 2>/dev/null
cp "$new_file" "$user_file"
print_success " Updated $config_name (backup: $backup_file)"
return 0
;;
4)
diff --color=auto -u "$user_version" "$new_file" 2>/dev/null | less
# Loop continues to show menu again
;;
*)
# Restore user's version
rm -f "$user_file" 2>/dev/null
cp "$user_version" "$user_file"
print_step " Keeping your version of $config_name"
return 0
;;
esac
done
}
# Run migration scripts sequentially
run_migrations() {
local from_version="$1"
local to_version="$2"
# Validate inputs (backward compatibility for old installations)
# Ensure versions are valid non-negative integers
if [ -z "$from_version" ] || ! [[ "$from_version" =~ ^[0-9]+$ ]]; then
from_version=0
fi
if [ -z "$to_version" ] || ! [[ "$to_version" =~ ^[0-9]+$ ]]; then
to_version=1
fi