aboutsummaryrefslogtreecommitdiffstats
path: root/copy.sh
blob: 5275eeb5dbee7e14edd354f64a9d5b9c821bfc59 (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
#!/usr/bin/env bash
# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */  #
# Purpose:
#   Orchestrates copying/upgrading JaKooLit's Hyprland dotfiles into ~/.config.
#   Handles interactive prompts, backups/restores, per-app tweaks, and express mode.
#
# Layout (high-level; future modularization targets):
#   - Constants/colors, helper sourcing (copy_menu.sh, lib_backup.sh, lib_detect.sh, lib_prompts.sh).
#   - Version helpers and CLI parsing (install/upgrade/express).
#   - Safety checks (non-root), banners/notices.
#   - Environment/distro checks and warnings.
#   - GPU/VM/NixOS detection tweaks (lib_detect.sh).
#   - Input prompts (keyboard, resolution, clock format, animations) (lib_prompts.sh).
#   - Workflow selection effects (express vs standard).
#   - Backup/restore helpers (in scripts/lib_backup.sh).
#   - Copy phases:
#       * Part 1: fastfetch/kitty/rofi/swaync (prompted replace).
#       * Waybar special handling (symlinks, configs/styles restore).
#       * Part 2: other configs (btop, cava, hypr, etc.).
#       * App-specific installs (ghostty, wezterm, ags, quickshell).
#   - UserConfigs/UserScripts and hypr file restores.
#   - Wallpaper handling (default + optional 1GB pack).
#   - Backup cleanup (auto in express).
#   - Final symlinks (waybar) and wallust init.
#
# Next modular step:
#   Extract per-app installers (ags/quickshell/ghostty/wezterm) and editor selection
#   into scripts/lib_apps.sh; then consider splitting copy phases into dedicated helpers.

clear
wallpaper=$HOME/.config/hypr/wallpaper_effects/.wallpaper_current
waybar_style="$HOME/.config/waybar/style/[Extra] Neon Circuit.css"
waybar_config="$HOME/.config/waybar/configs/[TOP] Default"
waybar_config_laptop="$HOME/.config/waybar/configs/[TOP] Default Laptop"

# Set some colors for output messages
OK="$(tput setaf 2)[OK]$(tput sgr0)"
ERROR="$(tput setaf 1)[ERROR]$(tput sgr0)"
NOTE="$(tput setaf 3)[NOTE]$(tput sgr0)"
INFO="$(tput setaf 4)[INFO]$(tput sgr0)"
WARN="$(tput setaf 1)[WARN]$(tput sgr0)"
CAT="$(tput setaf 6)[ACTION]$(tput sgr0)"
MAGENTA="$(tput setaf 5)"
ORANGE="$(tput setaf 214)"
WARNING="$(tput setaf 1)"
YELLOW="$(tput setaf 3)"
GREEN="$(tput setaf 2)"
BLUE="$(tput setaf 4)"
SKY_BLUE="$(tput setaf 6)"
RESET="$(tput sgr0)"
MIN_EXPRESS_VERSION="2.3.18"
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MENU_HELPER="$SCRIPT_DIR/scripts/copy_menu.sh"
BACKUP_HELPER="$SCRIPT_DIR/scripts/lib_backup.sh"
DETECT_HELPER="$SCRIPT_DIR/scripts/lib_detect.sh"
PROMPTS_HELPER="$SCRIPT_DIR/scripts/lib_prompts.sh"
if [ -f "$MENU_HELPER" ]; then
  # shellcheck source=./scripts/copy_menu.sh
  . "$MENU_HELPER"
fi
if [ -f "$BACKUP_HELPER" ]; then
  # shellcheck source=./scripts/lib_backup.sh
  . "$BACKUP_HELPER"
else
  echo "${ERROR} Backup helper not found at $BACKUP_HELPER. Exiting."
  exit 1
fi
if [ -f "$DETECT_HELPER" ]; then
  # shellcheck source=./scripts/lib_detect.sh
  . "$DETECT_HELPER"
else
  echo "${ERROR} Detect helper not found at $DETECT_HELPER. Exiting."
  exit 1
fi
if [ -f "$PROMPTS_HELPER" ]; then
  # shellcheck source=./scripts/lib_prompts.sh
  . "$PROMPTS_HELPER"
else
  echo "${ERROR} Prompts helper not found at $PROMPTS_HELPER. Exiting."
  exit 1
fi

version_gte() {
  [ "$1" = "$(echo -e "$1\n$2" | sort -V | tail -n1)" ]
}

get_installed_dotfiles_version() {
  local hypr_dir="$HOME/.config/hypr"
  local version_file
  if [ -d "$hypr_dir" ]; then
    version_file=$(find "$hypr_dir" -maxdepth 1 -name "v*.*.*" | head -n 1)
    if [ -n "$version_file" ]; then
      basename "$version_file" | sed 's/^v//'
    fi
  fi
}

express_supported() {
  local current_version
  current_version=$(get_installed_dotfiles_version)
  if [ -z "$current_version" ]; then
    return 1
  fi
  version_gte "$current_version" "$MIN_EXPRESS_VERSION"
}
print_usage() {
  cat <<'EOF'
Usage: copy.sh [--upgrade] [--express-upgrade] [--help]

Options:
  --upgrade           Run the script in upgrade mode (can still prompt for express).
  --express-upgrade   Upgrade with express behavior (no restore prompts, trims backups).
  -h, --help          Show this help message and exit.
EOF
}

UPGRADE_MODE=0
EXPRESS_MODE=0
RUN_MODE=""

while [[ $# -gt 0 ]]; do
  case "$1" in
  --upgrade)
    UPGRADE_MODE=1
    RUN_MODE="upgrade"
    ;;
  --express-upgrade)
    UPGRADE_MODE=1
    EXPRESS_MODE=1
    RUN_MODE="express"
    ;;
  -h | --help)
    print_usage
    exit 0
    ;;
  *)
    echo "${ERROR} Unknown option: $1"
    print_usage
    exit 1
    ;;
  esac
  shift
done
EXPRESS_SUPPORTED=0
if express_supported; then
  EXPRESS_SUPPORTED=1
fi
if [ "$EXPRESS_MODE" -eq 1 ] && [ "$EXPRESS_SUPPORTED" -eq 0 ]; then
  echo "${WARN} Express upgrade requires installed dotfiles v${MIN_EXPRESS_VERSION} or newer. Falling back to standard upgrade."
  EXPRESS_MODE=0
  RUN_MODE="upgrade"
fi

if [ -z "$RUN_MODE" ]; then
  if declare -f show_copy_menu >/dev/null 2>&1; then
    while [ -z "$RUN_MODE" ]; do
      show_copy_menu "$EXPRESS_SUPPORTED"
      choice_lower=$(echo "$COPY_MENU_CHOICE" | tr '[:upper:]' '[:lower:]')
      case "$choice_lower" in
      install)
        RUN_MODE="install"
        UPGRADE_MODE=0
        EXPRESS_MODE=0
        ;;
      upgrade)
        RUN_MODE="upgrade"
        UPGRADE_MODE=1
        EXPRESS_MODE=0
        ;;
      express)
        if [ "$EXPRESS_SUPPORTED" -eq 0 ]; then
          echo "${WARN} Express mode requires installed dotfiles v${MIN_EXPRESS_VERSION} or newer. Please choose another option."
          continue
        fi
        RUN_MODE="express"
        UPGRADE_MODE=1
        EXPRESS_MODE=1
        ;;
      quit)
        echo "${NOTE} Exiting per user selection."
        exit 0
        ;;
      *)
        echo "${WARN} Invalid selection."
        ;;
      esac
    done
  else
    echo "${NOTE} Menu helper not found; defaulting to install workflow."
    RUN_MODE="install"
  fi
fi

# Check if running as root. If root, script will exit
if [[ $EUID -eq 0 ]]; then
  echo "${ERROR}  This script should ${WARNING}NOT${RESET} be executed as root!! Exiting......."
  printf "\n%.0s" {1..2}
  exit 1
fi

# Function to print colorful text
print_color() {
  # Use %b for the message to interpret backslash escapes like \n, \t, etc.
  printf "%b%b%b\n" "$1" "$2" "$RESET"
}

# Check /etc/os-release for Ubuntu or Debian and warn about Hyprland version requirement
if grep -iqE '^(ID_LIKE|ID)=.*(ubuntu|debian)' /etc/os-release >/dev/null 2>&1; then
  printf "\n%.0s" {1..1}
  print_color $WARNING "\nThese Dotfiles are only supported on Hyprland v0.50 or greater. Do not install on older versions of Hyprland.\n"
  while true; do
    echo -n "${CAT} Do you want to continue anyway? (y/N): "
    read _continue
    _continue=$(echo "${_continue}" | tr '[:upper:]' '[:lower:]')
    case "${_continue}" in
    y | yes)
      echo "${NOTE} Proceeding on Ubuntu/Debian by user confirmation."
      break
      ;;
    n | no | "")
      printf "\n%.0s" {1..1}
      echo "${INFO} Aborting per user choice. No changes made."
      printf "\n%.0s" {1..1}
      exit 1
      ;;
    *)
      echo "${WARN} Please answer 'y' or 'n'."
      ;;
    esac
  done
fi

printf "\n%.0s" {1..1}
echo -e "\e[35m
    ╦╔═┌─┐┌─┐╦    ╔╦╗┌─┐┌┬┐┌─┐
    ╠╩╗│ ││ │║     ║║│ │ │ └─┐ 2025
    ╩ ╩└─┘└─┘╩═╝  ═╩╝└─┘ ┴ └─┘
\e[0m"
printf "\n%.0s" {1..1}

####### Announcement
echo "${WARNING}A T T E N T I O N !${RESET}"
echo "${MAGENTA}Kindly visit KooL Hyprland Own Wiki for changelogs${RESET}"
printf "\n%.0s" {1..1}

# Create Directory for Copy Logs
if [ ! -d Copy-Logs ]; then
  mkdir Copy-Logs
fi

# Set the name of the log file to include the current date and time
LOG="Copy-Logs/install-$(date +%d-%H%M%S)_dotfiles.log"

# update home directories
xdg-user-dirs-update 2>&1 | tee -a "$LOG" || true
echo "${INFO} Selected workflow: ${RUN_MODE}" 2>&1 | tee -a "$LOG"
if [ "$UPGRADE_MODE" -eq 1 ]; then
  echo "${INFO} Upgrade mode enabled." 2>&1 | tee -a "$LOG"
fi
if [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${INFO} Express mode enabled. Optional restore prompts will be skipped." 2>&1 | tee -a "$LOG"
fi

detect_nvidia_adjust "$LOG"
detect_vm_adjust "$LOG"
detect_nixos_adjust "$LOG"

# activating hyprcursor on env by checking if the directory ~/.icons/Bibata-Modern-Ice/hyprcursors exists
if [ -d "$HOME/.icons/Bibata-Modern-Ice/hyprcursors" ]; then
  HYPRCURSOR_ENV_FILE="config/hypr/configs/ENVariables.conf"
  echo "${INFO} Bibata-Hyprcursor directory detected. Activating Hyprcursor...." 2>&1 | tee -a "$LOG" || true
  sed -i 's/^#env = HYPRCURSOR_THEME,Bibata-Modern-Ice/env = HYPRCURSOR_THEME,Bibata-Modern-Ice/' "$HYPRCURSOR_ENV_FILE"
  sed -i 's/^#env = HYPRCURSOR_SIZE,24/env = HYPRCURSOR_SIZE,24/' "$HYPRCURSOR_ENV_FILE"
fi

printf "\n%.0s" {1..1}

layout=$(prompt_detect_layout)
prompt_keyboard_layout "$layout" "$LOG"

# Check if asusctl is installed and add rog-control-center on Startup
if command -v asusctl >/dev/null 2>&1; then
  OVERLAY_SA="config/hypr/configs/Startup_Apps.conf"
  mkdir -p "$(dirname "$OVERLAY_SA")"
  touch "$OVERLAY_SA"
  grep -qx 'exec-once = rog-control-center' "$OVERLAY_SA" || echo 'exec-once = rog-control-center' >>"$OVERLAY_SA"
fi

# Check if blueman-applet is installed and add blueman-applet on Startup
if command -v blueman-applet >/dev/null 2>&1; then
  OVERLAY_SA="config/hypr/configs/Startup_Apps.conf"
  mkdir -p "$(dirname "$OVERLAY_SA")"
  touch "$OVERLAY_SA"
  grep -qx 'exec-once = blueman-applet' "$OVERLAY_SA" || echo 'exec-once = blueman-applet' >>"$OVERLAY_SA"
fi

# Check if ags is installed and enable it
if command -v ags >/dev/null 2>&1; then
  echo "${INFO} AGS detected - enabling in startup and refresh scripts" 2>&1 | tee -a "$LOG"
  OVERLAY_SA="config/hypr/configs/Startup_Apps.conf"
  mkdir -p "$(dirname "$OVERLAY_SA")"
  touch "$OVERLAY_SA"
  grep -qx 'exec-once = ags' "$OVERLAY_SA" || echo 'exec-once = ags' >>"$OVERLAY_SA"
  sed -i '/#ags -q && ags &/s/^#//' config/hypr/scripts/RefreshNoWaybar.sh
  sed -i '/#ags -q && ags &/s/^#//' config/hypr/scripts/Refresh.sh
fi

# Check if quickshell is installed and enable it
if command -v qs >/dev/null 2>&1; then
  echo "${INFO} Quickshell detected - enabling in startup and refresh scripts" 2>&1 | tee -a "$LOG"
  OVERLAY_SA="config/hypr/configs/Startup_Apps.conf"
  mkdir -p "$(dirname "$OVERLAY_SA")"
  touch "$OVERLAY_SA"
  grep -qx 'exec-once = qs' "$OVERLAY_SA" || echo 'exec-once = qs' >>"$OVERLAY_SA"
  sed -i '/#pkill qs && qs &/s/^#//' config/hypr/scripts/RefreshNoWaybar.sh
  sed -i '/#pkill qs && qs &/s/^#//' config/hypr/scripts/Refresh.sh
fi

# Ensure layout-aware keybinds init runs on startup (adds to user overlay so it survives composes)
OVERLAY_SA="config/hypr/configs/Startup_Apps.conf"
mkdir -p "$(dirname "$OVERLAY_SA")"
if ! grep -qx 'exec-once = \$scriptsDir/KeybindsLayoutInit.sh' "$OVERLAY_SA"; then
  echo 'exec-once = $scriptsDir/KeybindsLayoutInit.sh' >>"$OVERLAY_SA"
  echo "${INFO} Added KeybindsLayoutInit.sh to user Startup_Apps overlay" 2>&1 | tee -a "$LOG"
fi

printf "\n%.0s" {1..1}

# Checking if neovim or vim is installed and offer user if they want to make as default editor
# Function to modify the ENVariables.conf file
update_editor() {
  local editor=$1
  sed -i "s/#env = EDITOR,.*/env = EDITOR,$editor #default editor/" config/hypr/UserConfigs/01-UserDefaults.conf
  echo "${OK} Default editor set to ${MAGENTA}$editor${RESET}." 2>&1 | tee -a "$LOG"
}

EDITOR_SET=0
# Check for neovim if installed
if command -v nvim &>/dev/null; then
  printf "${INFO} ${MAGENTA}neovim${RESET} is detected as installed\n"
  echo -n "${CAT} Do you want to make ${MAGENTA}neovim${RESET} the default editor? (y/N): "
  read EDITOR_CHOICE
  if [[ "$EDITOR_CHOICE" == "y" || "$EDITOR_CHOICE" == "Y" ]]; then
    update_editor "nvim"
    EDITOR_SET=1
  fi
fi

printf "\n"

# Check for vim if installed, but only if neovim wasn't chosen
if [[ "$EDITOR_SET" -eq 0 ]] && command -v vim &>/dev/null; then
  printf "${INFO} ${MAGENTA}vim${RESET} is detected as installed\n"
  echo -n "${CAT} Do you want to make ${MAGENTA}vim${RESET} the default editor? (y/N): "
  read EDITOR_CHOICE
  if [[ "$EDITOR_CHOICE" == "y" || "$EDITOR_CHOICE" == "Y" ]]; then
    update_editor "vim"
    EDITOR_SET=1
  fi
fi

printf "\n"
resolution=""
while true; do
  echo "${INFO} Select monitor resolution for scaling:"
  echo "  1) < 1440p   (lower DPI; smaller displays)"
  echo "  2) ≥ 1440p   (default; 1440p/2k/4k)"
  echo -n "${CAT} Enter the number of your choice (1 or 2): "
  read -r choice
  case "$choice" in
    1) resolution="< 1440p"; break ;;
    2) resolution="≥ 1440p"; break ;;
    *) echo "${ERROR} Invalid choice. Please enter 1 or 2.";;
  esac
done
echo "${OK} You have chosen $resolution resolution." 2>&1 | tee -a "$LOG"
if [ "$resolution" == "< 1440p" ]; then
  # kitty font size
  sed -i 's/font_size 16.0/font_size 14.0/' config/kitty/kitty.conf

  # hyprlock matters
  if [ -f config/hypr/hyprlock.conf ]; then
    mv config/hypr/hyprlock.conf config/hypr/hyprlock-2k.conf
  fi
  if [ -f config/hypr/hyprlock-1080p.conf ]; then
    mv config/hypr/hyprlock-1080p.conf config/hypr/hyprlock.conf
  fi

  # rofi fonts reduction
  rofi_config_file="config/rofi/0-shared-fonts.rasi"
  if [ -f "$rofi_config_file" ]; then
    sed -i '/element-text {/,/}/s/[[:space:]]*font: "JetBrainsMono Nerd Font SemiBold 13"/font: "JetBrainsMono Nerd Font SemiBold 11"/' "$rofi_config_file" 2>&1 | tee -a "$LOG"
    sed -i '/configuration {/,/}/s/[[:space:]]*font: "JetBrainsMono Nerd Font SemiBold 15"/font: "JetBrainsMono Nerd Font SemiBold 13"/' "$rofi_config_file" 2>&1 | tee -a "$LOG"
  fi
fi

printf "\n%.0s" {1..1}
prompt_clock_12h "$LOG"
printf "\n%.0s" {1..1}
prompt_rainbow_borders "$LOG" >/dev/null
printf "\n%.0s" {1..1}
prompt_express_upgrade "$EXPRESS_SUPPORTED" "$LOG"

set -e

# Check if the ~/.config/ directory exists
if [ ! -d "$HOME/.config" ]; then
  echo "${ERROR} - $HOME/.config directory does not exist. Creating it now."
  mkdir -p "$HOME/.config" && echo "Directory created successfully." || echo "Failed to create directory."
fi

printf "${INFO} - copying dotfiles ${SKY_BLUE}first${RESET} part\n"
# Config directories which will ask the user whether to replace or not
DIRS="fastfetch kitty rofi swaync"

for DIR2 in $DIRS; do
  DIRPATH="$HOME/.config/$DIR2"

  if [ -d "$DIRPATH" ]; then
    while true; do
      printf "\n${INFO} Found ${YELLOW}$DIR2${RESET} config found in ~/.config/\n"
      echo -n "${CAT} Do you want to replace ${YELLOW}$DIR2${RESET} config? (y/n): "
      read DIR1_CHOICE

      case "$DIR1_CHOICE" in
      [Yy]*)
        BACKUP_DIR=$(get_backup_dirname)
        # Backup the existing directory
        mv "$DIRPATH" "$DIRPATH-backup-$BACKUP_DIR" 2>&1 | tee -a "$LOG"
        echo -e "${NOTE} - Backed up $DIR2 to $DIRPATH-backup-$BACKUP_DIR." 2>&1 | tee -a "$LOG"

        # Copy the new config
        cp -r "config/$DIR2" "$HOME/.config/$DIR2" 2>&1 | tee -a "$LOG"
        echo -e "${OK} - Replaced $DIR2 with new configuration." 2>&1 | tee -a "$LOG"

        # Restoring rofi themes directory unique themes
        if [ "$DIR2" = "rofi" ]; then
          if [ -d "$DIRPATH-backup-$BACKUP_DIR/themes" ]; then
            for file in "$DIRPATH-backup-$BACKUP_DIR/themes"/*; do
              [ -e "$file" ] || continue # Skip if no files are found
              echo "Copying $file to $HOME/.config/rofi/themes/" >>"$LOG"
              cp -n "$file" "$HOME/.config/rofi/themes/" >>"$LOG" 2>&1
            done || true
          fi

          # restoring global 0-shared-fonts.rasi
          if [ -f "$DIRPATH-backup-$BACKUP_DIR/0-shared-fonts.rasi" ]; then
            echo "Restoring $DIRPATH-backup-$BACKUP_DIR/0-shared-fonts.rasi to $HOME/.config/rofi/" >>"$LOG"
            cp "$DIRPATH-backup-$BACKUP_DIR/0-shared-fonts.rasi" "$HOME/.config/rofi/0-shared-fonts.rasi" >>"$LOG" 2>&1
          fi

        fi

        break
        ;;
      [Nn]*)
        echo -e "${NOTE} - Skipping ${YELLOW}$DIR2${RESET}" 2>&1 | tee -a "$LOG"
        break
        ;;
      *)
        echo -e "${WARN} - Invalid choice. Please enter Y or N."
        ;;
      esac
    done
  else
    # Copy new config if directory does not exist
    cp -r "config/$DIR2" "$HOME/.config/$DIR2" 2>&1 | tee -a "$LOG"
    echo -e "${OK} - Copy completed for ${YELLOW}$DIR2${RESET}" 2>&1 | tee -a "$LOG"
  fi
done

printf "\n%.0s" {1..1}

# for waybar special part since it contains symlink
DIRW="waybar"
DIRPATHw="$HOME/.config/$DIRW"
if [ -d "$DIRPATHw" ]; then
  while true; do
    echo -n "${CAT} Do you want to replace ${YELLOW}$DIRW${RESET} config? (y/n): "
    read DIR1_CHOICE

    case "$DIR1_CHOICE" in
    [Yy]*)
      BACKUP_DIR=$(get_backup_dirname)
      cp -r "$DIRPATHw" "$DIRPATHw-backup-$BACKUP_DIR" 2>&1 | tee -a "$LOG"
      echo -e "${NOTE} - Backed up $DIRW to $DIRPATHw-backup-$BACKUP_DIR." 2>&1 | tee -a "$LOG"

      # Remove the old $DIRPATHw and copy the new one
      rm -rf "$DIRPATHw" && cp -r "config/$DIRW" "$DIRPATHw" 2>&1 | tee -a "$LOG"

      # Step 1: Handle waybar symlinks
      for file in "config" "style.css"; do
        symlink="$DIRPATHw-backup-$BACKUP_DIR/$file"
        target_file="$DIRPATHw/$file"

        if [ -L "$symlink" ]; then
          symlink_target=$(readlink "$symlink")
          if [ -f "$symlink_target" ]; then
            rm -f "$target_file" && cp -f "$symlink_target" "$target_file"
            echo -e "${NOTE} - Copied $file as a regular file."
          else
            echo -e "${WARN} - Symlink target for $file does not exist."
          fi
        fi
      done

      # Step 2: Copy non-existing directories and files under waybar/configs
      for dir in "$DIRPATHw-backup-$BACKUP_DIR/configs"/*; do
        [ -e "$dir" ] || continue # Skip if no files are found
        if [ -d "$dir" ]; then
          target_dir="$HOME/.config/waybar/configs/$(basename "$dir")"
          if [ ! -d "$target_dir" ]; then
            echo "Copying directory $dir to $HOME/.config/waybar/configs/" >>"$LOG"
            cp -r "$dir" "$HOME/.config/waybar/configs/"
          else
            echo "Directory $target_dir already exists. Skipping." >>"$LOG"
          fi
        fi
      done

      for file in "$DIRPATHw-backup-$BACKUP_DIR/configs"/*; do
        [ -e "$file" ] || continue
        target_file="$HOME/.config/waybar/configs/$(basename "$file")"
        if [ ! -e "$target_file" ]; then
          echo "Copying $file to $HOME/.config/waybar/configs/" >>"$LOG"
          cp "$file" "$HOME/.config/waybar/configs/"
        else
          echo "File $target_file already exists. Skipping." >>"$LOG"
        fi
      done || true

      # Step 3: Copy unique files in waybar/style
      for file in "$DIRPATHw-backup-$BACKUP_DIR/style"/*; do
        [ -e "$file" ] || continue

        if [ -d "$file" ]; then
          target_dir="$HOME/.config/waybar/style/$(basename "$file")"
          if [ ! -d "$target_dir" ]; then
            echo "Copying directory $file to $HOME/.config/waybar/style/" >>"$LOG"
            cp -r "$file" "$HOME/.config/waybar/style/"
          else
            echo "Directory $target_dir already exists. Skipping." >>"$LOG"
          fi
        else
          target_file="$HOME/.config/waybar/style/$(basename "$file")"
          if [ ! -e "$target_file" ]; then
            echo "Copying file $file to $HOME/.config/waybar/style/" >>"$LOG"
            cp "$file" "$HOME/.config/waybar/style/"
          else
            echo "File $target_file already exists. Skipping." >>"$LOG"
          fi
        fi
      done || true

      # Step 4: restore Modules_Extras
      BACKUP_FILEw="$DIRPATHw-backup-$BACKUP_DIR/UserModules"
      if [ -f "$BACKUP_FILEw" ]; then
        cp -f "$BACKUP_FILEw" "$DIRPATHw/UserModules"
      fi

      break
      ;;
    [Nn]*)
      echo -e "${NOTE} - Skipping ${YELLOW}$DIRW${RESET} config replacement." 2>&1 | tee -a "$LOG"
      break
      ;;
    *)
      echo -e "${WARN} - Invalid choice. Please enter Y or N."
      ;;
    esac
  done
else
  cp -r "config/$DIRW" "$DIRPATHw" 2>&1 | tee -a "$LOG"
  echo -e "${OK} - Copy completed for ${YELLOW}$DIRW${RESET}" 2>&1 | tee -a "$LOG"
fi

printf "\n%.0s" {1..1}

printf "${INFO} - Copying dotfiles ${SKY_BLUE}second${RESET} part\n"

# Check if the config directory exists
if [ ! -d "config" ]; then
  echo "${ERROR} - The 'config' directory does not exist."
  exit 1
fi

DIR="btop cava hypr Kvantum qt5ct qt6ct swappy wallust wlogout"

for DIR_NAME in $DIR; do
  DIRPATH="$HOME/.config/$DIR_NAME"

  # Backup the existing directory if it exists
  if [ -d "$DIRPATH" ]; then
    echo -e "\n${NOTE} - Config for ${YELLOW}$DIR_NAME${RESET} found, attempting to back up."
    BACKUP_DIR=$(get_backup_dirname)

    # Backup the existing directory
    mv "$DIRPATH" "$DIRPATH-backup-$BACKUP_DIR" 2>&1 | tee -a "$LOG"
    if [ $? -eq 0 ]; then
      echo -e "${NOTE} - Backed up $DIR_NAME to $DIRPATH-backup-$BACKUP_DIR."
    else
      echo "${ERROR} - Failed to back up $DIR_NAME."
      exit 1
    fi
  fi

  # Copy the new config
  if [ -d "config/$DIR_NAME" ]; then
    cp -r "config/$DIR_NAME/" "$HOME/.config/$DIR_NAME" 2>&1 | tee -a "$LOG"
    if [ $? -eq 0 ]; then
      echo "${OK} - Copy of config for ${YELLOW}$DIR_NAME${RESET} completed!"
    else
      echo "${ERROR} - Failed to copy $DIR_NAME."
      exit 1
    fi
  else
    echo "${ERROR} - Directory config/$DIR_NAME does not exist to copy."
  fi
done

# Install Ghostty config
GHOSTTY_SRC="config/ghostty/ghostty.config"
GHOSTTY_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/ghostty"
GHOSTTY_DEST="$GHOSTTY_DIR/config"

if [ -f "$GHOSTTY_SRC" ]; then
  mkdir -p "$GHOSTTY_DIR"
  install -m 0644 "$GHOSTTY_SRC" "$GHOSTTY_DEST" 2>&1 | tee -a "$LOG"
  echo "${OK} - Installed Ghostty config to ${MAGENTA}$GHOSTTY_DEST${RESET}" 2>&1 | tee -a "$LOG"
  # Normalize existing wallust.conf palette syntax if present (convert ':' to '=')
  if [ -f "$GHOSTTY_DIR/wallust.conf" ]; then
    sed -i -E 's/^(\s*palette\s*=\s*)([0-9]{1,2}):/\1\2=/' "$GHOSTTY_DIR/wallust.conf" 2>&1 | tee -a "$LOG" || true
  fi
else
  echo "${ERROR} - $GHOSTTY_SRC not found; skipping Ghostty config install." 2>&1 | tee -a "$LOG"
fi

# Install WezTerm config
WEZTERM_SRC="config/wezterm/wezterm.lua"
WEZTERM_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/wezterm"
WEZTERM_DEST="$WEZTERM_DIR/wezterm.lua"

if [ -f "$WEZTERM_SRC" ]; then
  mkdir -p "$WEZTERM_DIR"
  install -m 0644 "$WEZTERM_SRC" "$WEZTERM_DEST" 2>&1 | tee -a "$LOG"
  echo "${OK} - Installed WezTerm config to ${MAGENTA}$WEZTERM_DEST${RESET}" 2>&1 | tee -a "$LOG"
else
  echo "${ERROR} - $WEZTERM_SRC not found; skipping WezTerm config install." 2>&1 | tee -a "$LOG"
fi

printf "\\n%.0s" {1..1}

# ags config
# Check if ags is installed
if command -v ags >/dev/null 2>&1; then
  echo -e "${NOTE} - ${YELLOW}ags${RESET} is detected as installed"

  DIRPATH_AGS="$HOME/.config/ags"

  if [ ! -d "$DIRPATH_AGS" ]; then
    echo "${INFO} - ags config not found, copying new config."
    if [ -d "config/ags" ]; then
      cp -r "config/ags/" "$DIRPATH_AGS" 2>&1 | tee -a "$LOG"
    fi
  else
    read -p "${CAT} Do you want to overwrite your existing ${YELLOW}ags${RESET} config? [y/N] " answer_ags
    case "$answer_ags" in
    [Yy]*)
      BACKUP_DIR=$(get_backup_dirname)
      mv "$DIRPATH_AGS" "$DIRPATH_AGS-backup-$BACKUP_DIR" 2>&1 | tee -a "$LOG"
      echo -e "${NOTE} - Backed up ags config to $DIRPATH_AGS-backup-$BACKUP_DIR"

      if cp -r "config/ags/" "$DIRPATH_AGS" 2>&1 | tee -a "$LOG"; then
        echo "${OK} - ${YELLOW}ags configs${RESET} overwritten successfully."
      else
        echo "${ERROR} - Failed to copy ${YELLOW}ags${RESET} config."
        exit 1
      fi
      ;;
    *)
      echo "${NOTE} - Skipping overwrite of ags config."
      ;;
    esac
  fi
fi

printf "\n%.0s" {1..1}

# quickshell (ags alternative)
# Check if quickshell is installed
if command -v qs >/dev/null 2>&1; then
  echo -e "${NOTE} - ${YELLOW}quickshell${RESET} is detected as installed"

  DIRPATH_QS="$HOME/.config/quickshell"

  if [ ! -d "$DIRPATH_QS" ]; then
    echo "${INFO} - quickshell config not found, copying new config."
    if [ -d "config/quickshell" ]; then
      cp -r "config/quickshell/" "$DIRPATH_QS" 2>&1 | tee -a "$LOG"
    fi
  else
    # If default shell.qml exists, it blocks named config subdirectory detection
    # Remove it to enable the overview config to be found
    if [ -f "$DIRPATH_QS/shell.qml" ]; then
      echo "${NOTE} - Removing default shell.qml to enable quickshell overview config detection" 2>&1 | tee -a "$LOG"
      rm "$DIRPATH_QS/shell.qml"
    fi
    
    read -p "${CAT} Do you want to overwrite your existing ${YELLOW}quickshell${RESET} config? [y/N] " answer_qs
    case "$answer_qs" in
    [Yy]*)
      BACKUP_DIR=$(get_backup_dirname)
      mv "$DIRPATH_QS" "$DIRPATH_QS-backup-$BACKUP_DIR" 2>&1 | tee -a "$LOG"
      echo -e "${NOTE} - Backed up quickshell to $DIRPATH_QS-backup-$BACKUP_DIR"

      cp -r "config/quickshell/" "$DIRPATH_QS" 2>&1 | tee -a "$LOG"
      if [ $? -eq 0 ]; then
        echo "${OK} - ${YELLOW}quickshell${RESET} overwritten successfully."
        # Remove default shell.qml from new copy to enable overview detection
        rm -f "$DIRPATH_QS/shell.qml" 2>&1 | tee -a "$LOG"
      else
        echo "${ERROR} - Failed to copy ${YELLOW}quickshell${RESET} config."
        exit 1
      fi
      ;;
    *)
      echo "${NOTE} - Skipping overwrite of quickshell config."
      ;;
    esac
  fi
  
  # Ensure overview subdirectory exists and is up to date
  DIRPATH_OVERVIEW="$DIRPATH_QS/overview"
  if [ ! -d "$DIRPATH_OVERVIEW" ] && [ -d "config/quickshell/overview" ]; then
    echo "${INFO} - Copying quickshell overview config..." 2>&1 | tee -a "$LOG"
    cp -r "config/quickshell/overview" "$DIRPATH_QS/" 2>&1 | tee -a "$LOG"
    echo "${OK} - Quickshell overview config copied successfully" 2>&1 | tee -a "$LOG"
  fi
  
  # Check for old quickshell startup commands and update them
  HYPR_STARTUP="$HOME/.config/hypr/configs/Startup_Apps.conf"
  if [ -f "$HYPR_STARTUP" ]; then
    if grep -q '^exec-once = qs\s*$\|^exec-once = qs &' "$HYPR_STARTUP"; then
      echo "${NOTE} - Found old Quickshell startup command, updating to new overview config..." 2>&1 | tee -a "$LOG"
      # Replace old 'qs' or 'qs &' with new 'qs -c overview'
      sed -i 's/^\(\s*\)exec-once = qs\s*$/\1exec-once = qs -c overview  # Quickshell Overview/' "$HYPR_STARTUP" 2>&1 | tee -a "$LOG"
      sed -i 's/^\(\s*\)exec-once = qs &$/\1exec-once = qs -c overview  # Quickshell Overview/' "$HYPR_STARTUP" 2>&1 | tee -a "$LOG"
      echo "${OK} - Updated Quickshell startup command to use overview config" 2>&1 | tee -a "$LOG"
    fi
  fi
fi
printf "\n%.0s" {1..1}

# Restore automatically Animations and Monitor-Profiles
# including monitors.conf and workspaces.conf
HYPR_DIR="$HOME/.config/hypr"
BACKUP_DIR=$(get_backup_dirname)
BACKUP_HYPR_PATH="$HYPR_DIR-backup-$BACKUP_DIR"

if [ -d "$BACKUP_HYPR_PATH" ]; then
  if [ "$EXPRESS_MODE" -eq 1 ]; then
    echo "${NOTE} Express mode: skipping automatic restoration of animations and monitor profiles." 2>&1 | tee -a "$LOG"
  else
    echo -e "\n${NOTE} Restoring ${SKY_BLUE}Animations & Monitor Profiles${RESET} directories into ${YELLOW}$HYPR_DIR${RESET}..."

    DIR_B=("Monitor_Profiles" "animations" "wallpaper_effects")
    # Restore directories automatically
    for DIR_RESTORE in "${DIR_B[@]}"; do
      BACKUP_SUBDIR="$BACKUP_HYPR_PATH/$DIR_RESTORE"
      if [ -d "$BACKUP_SUBDIR" ]; then
        cp -r "$BACKUP_SUBDIR" "$HYPR_DIR/"
        echo "${OK} - Restored directory: ${MAGENTA}$DIR_RESTORE${RESET}" 2>&1 | tee -a "$LOG"
      fi
    done

    # Restore files automatically
    FILE_B=("monitors.conf" "workspaces.conf")
    for FILE_RESTORE in "${FILE_B[@]}"; do
      BACKUP_FILE="$BACKUP_HYPR_PATH/$FILE_RESTORE"

      if [ -f "$BACKUP_FILE" ]; then
        cp "$BACKUP_FILE" "$HYPR_DIR/$FILE_RESTORE"
        echo "${OK} - Restored file: ${MAGENTA}$FILE_RESTORE${RESET}" 2>&1 | tee -a "$LOG"
      fi
    done
  fi
fi

printf "\n%.0s" {1..1}

# Restoring UserConfigs and UserScripts
# Helper to extract overlay (additions) and optional disables from a previous user file compared to vendor base
compose_overlay_from_backup() {
  local type="$1" # startup|windowrules
  local base_file="$2"
  local old_user_file="$3"
  local new_user_file="$4"
  local disable_file="$5"

  mkdir -p "$(dirname "$new_user_file")"
  : >"$new_user_file"
  : >"$disable_file"

  if [ "$type" = "startup" ]; then
    # additions: exec-once lines present in old user but not in base
    grep -E '^\s*exec-once\s*=' "$old_user_file" | sed -E 's/^\s+//;s/\s+$//' | sort -u >"$old_user_file.tmp.exec"
    grep -E '^\s*exec-once\s*=' "$base_file" | sed -E 's/^\s+//;s/\s+$//' | sort -u >"$base_file.tmp.exec"
    comm -23 "$old_user_file.tmp.exec" "$base_file.tmp.exec" >"$new_user_file"
    # treat commented exec-once in old user as disables
    grep -E '^\s*#\s*exec-once\s*=' "$old_user_file" |
      sed -E 's/^\s*#\s*exec-once\s*=\s*//' |
      sed -E 's/^\s+//;s/\s+$//' |
      grep -Ev '^\$scriptsDir/KeybindsLayoutInit\.sh$' |
      sort -u >"$disable_file"
    rm -f "$old_user_file.tmp.exec" "$base_file.tmp.exec"
  elif [ "$type" = "windowrules" ]; then
    # additions
    grep -E '^(windowrule|layerrule)\s*=' "$old_user_file" | sed -E 's/^\s+//;s/\s+$//' | sort -u >"$old_user_file.tmp.rules"
    grep -E '^(windowrule|layerrule)\s*=' "$base_file" | sed -E 's/^\s+//;s/\s+$//' | sort -u >"$base_file.tmp.rules"
    comm -23 "$old_user_file.tmp.rules" "$base_file.tmp.rules" >"$new_user_file"
    # disables: lines commented in old user
    grep -E '^\s*#\s*(windowrule|layerrule)\s*=' "$old_user_file" | sed -E 's/^\s*#\s*//' | sed -E 's/^\s+//;s/\s+$//' | sort -u >"$disable_file"
    rm -f "$old_user_file.tmp.rules" "$base_file.tmp.rules"
  fi
}


DIRH="hypr"
DIRPATH="$HOME/.config/$DIRH"
BACKUP_DIR=$(get_backup_dirname)
BACKUP_DIR_PATH="$DIRPATH-backup-$BACKUP_DIR/UserConfigs"

if [ -z "$BACKUP_DIR" ]; then
  echo "${ERROR} - Backup directory name is empty. Exiting."
  exit 1
fi

if [ -d "$BACKUP_DIR_PATH" ] && [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${NOTE} Express mode: skipping UserConfigs restoration prompts." 2>&1 | tee -a "$LOG"
fi

if [ -d "$BACKUP_DIR_PATH" ] && [ "$EXPRESS_MODE" -eq 0 ]; then
  # Detect version
  VERSION_FILE=$(find "$DIRPATH" -maxdepth 1 -name "v*.*.*" | head -n 1)
  CURRENT_VERSION="999.9.9"
  if [ -n "$VERSION_FILE" ]; then
    CURRENT_VERSION=$(basename "$VERSION_FILE" | sed 's/^v//')
  fi

  TARGET_VERSION="2.3.19"

  echo -e "${NOTE} Restoring previous ${MAGENTA}User-Configs${RESET}... "
  print_color $WARNING "
    █▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀█
            NOTES for RESTORING PREVIOUS CONFIGS
    █▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄█

    The 'UserConfigs' directory is for all your personal settings.
    Files in this directory will override the default configurations,
    so your customizations are not lost when you update.
    "

  if version_gte "$CURRENT_VERSION" "$TARGET_VERSION"; then
    # NEW BEHAVIOR (>= 2.3.19) - Bulk Restore
    echo -n "${CAT} Do you want to restore your previous UserConfigs directory? (Y/n): "
    read -r restore_userconfigs_dir

    if [[ "$restore_userconfigs_dir" != [Nn]* ]]; then
      echo "${NOTE} Restoring UserConfigs directory..." 2>&1 | tee -a "$LOG"
      # Use rsync to copy contents, overwriting existing files.
      rsync -a "$BACKUP_DIR_PATH/" "$DIRPATH/UserConfigs/" 2>&1 | tee -a "$LOG"
      echo "${OK} - UserConfigs directory restored." 2>&1 | tee -a "$LOG"
    else
      echo "${NOTE} - Skipped restoring UserConfigs." 2>&1 | tee -a "$LOG"
    fi

  else
    # OLD BEHAVIOR (<= 2.3.18) - Selective Restore
    echo -e "${NOTE} Detected version ${YELLOW}v$CURRENT_VERSION${RESET} (older than v$TARGET_VERSION). Using legacy restoration mode."

    FILES_TO_RESTORE=(
      "01-UserDefaults.conf"
      "ENVariables.conf"
      "LaptopDisplay.conf"
      "Laptops.conf"
      "Startup_Apps.conf"
      "UserDecorations.conf"
      "UserAnimations.conf"
      "UserKeybinds.conf"
      "UserSettings.conf"
      "WindowRules.conf"
    )

    for FILE_NAME in "${FILES_TO_RESTORE[@]}"; do
      BACKUP_FILE="$BACKUP_DIR_PATH/$FILE_NAME"
      if [ -f "$BACKUP_FILE" ]; then
        # Special handling for Startup_Apps.conf and WindowRules.conf
        if [ "$FILE_NAME" = "Startup_Apps.conf" ]; then
          compose_overlay_from_backup "startup" "$DIRPATH/configs/Startup_Apps.conf" "$BACKUP_FILE" "$DIRPATH/UserConfigs/Startup_Apps.conf" "$DIRPATH/UserConfigs/Startup_Apps.disable"
          echo "${OK} - Migrated overlay for ${YELLOW}$FILE_NAME${RESET}" 2>&1 | tee -a "$LOG"
          continue
        fi
        if [ "$FILE_NAME" = "WindowRules.conf" ]; then
          compose_overlay_from_backup "windowrules" "$DIRPATH/configs/WindowRules.conf" "$BACKUP_FILE" "$DIRPATH/UserConfigs/WindowRules.conf" "$DIRPATH/UserConfigs/WindowRules.disable"
          echo "${OK} - Migrated overlay for ${YELLOW}$FILE_NAME${RESET}" 2>&1 | tee -a "$LOG"
          continue
        fi

        printf "\n${INFO} Found ${YELLOW}$FILE_NAME${RESET} in hypr backup...\n"
        echo -n "${CAT} Do you want to restore ${YELLOW}$FILE_NAME${RESET} from backup? (Y/n): "
        read file_restore

        if [[ "$file_restore" != [Nn]* ]]; then
          if cp "$BACKUP_FILE" "$DIRPATH/UserConfigs/$FILE_NAME"; then
            echo "${OK} - $FILE_NAME restored!" 2>&1 | tee -a "$LOG"
          else
            echo "${ERROR} - Failed to restore $FILE_NAME!" 2>&1 | tee -a "$LOG"
          fi
        else
          echo "${NOTE} - Skipped restoring $FILE_NAME." 2>&1 | tee -a "$LOG"
        fi
      fi
    done
  fi
fi

printf "\n%.0s" {1..1}

# Restoring previous UserScripts
DIRSH="hypr"
SCRIPTS_TO_RESTORE=(
  "RofiBeats.sh"
  "Weather.py"
  "Weather.sh"
)

DIRSHPATH="$HOME/.config/$DIRSH"
BACKUP_DIR_PATH_S="$DIRSHPATH-backup-$BACKUP_DIR/UserScripts"

if [ -d "$BACKUP_DIR_PATH_S" ] && [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${NOTE} Express mode: skipping UserScripts restoration prompts." 2>&1 | tee -a "$LOG"
fi

if [ -d "$BACKUP_DIR_PATH_S" ] && [ "$EXPRESS_MODE" -eq 0 ]; then
  echo -e "${NOTE} Restoring previous ${MAGENTA}User-Scripts${RESET}..."

  for SCRIPT_NAME in "${SCRIPTS_TO_RESTORE[@]}"; do
    BACKUP_SCRIPT="$BACKUP_DIR_PATH_S/$SCRIPT_NAME"

    if [ -f "$BACKUP_SCRIPT" ]; then
      printf "\n${INFO} Found ${YELLOW}$SCRIPT_NAME${RESET} in hypr backup...\n"
      echo -n "${CAT} Do you want to restore ${YELLOW}$SCRIPT_NAME${RESET} from backup? (y/N): "
      read script_restore

      if [[ "$script_restore" == [Yy]* ]]; then
        if cp "$BACKUP_SCRIPT" "$DIRSHPATH/UserScripts/$SCRIPT_NAME"; then
          echo "${OK} - $SCRIPT_NAME restored!" 2>&1 | tee -a "$LOG"
        else
          echo "${ERROR} - Failed to restore $SCRIPT_NAME!" 2>&1 | tee -a "$LOG"
        fi
      else
        echo "${NOTE} - Skipped restoring $SCRIPT_NAME." 2>&1 | tee -a "$LOG"
      fi
    fi
  done
fi

printf "\n%.0s" {1..1}

# restoring some files in ~/.config/hypr
DIR_H="hypr"
FILES_2_RESTORE=(
  "hyprlock.conf"
  "hypridle.conf"
)

DIRPATH="$HOME/.config/$DIR_H"
BACKUP_DIR=$(get_backup_dirname)
BACKUP_DIR_PATH_F="$DIRPATH-backup-$BACKUP_DIR"

if [ -d "$BACKUP_DIR_PATH_F" ] && [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${NOTE} Express mode: skipping individual hypr file restoration prompts." 2>&1 | tee -a "$LOG"
fi

if [ -d "$BACKUP_DIR_PATH_F" ] && [ "$EXPRESS_MODE" -eq 0 ]; then
  echo -e "${NOTE} Restoring some files in ${MAGENTA}$HOME/.config/hypr directory${RESET}..."

  for FILE_RESTORE in "${FILES_2_RESTORE[@]}"; do
    BACKUP_FILE="$BACKUP_DIR_PATH_F/$FILE_RESTORE"

    if [ -f "$BACKUP_FILE" ]; then
      echo -e "\n${INFO} Found ${YELLOW}$FILE_RESTORE${RESET} in hypr backup..."
      echo -n "${CAT} Do you want to restore ${YELLOW}$FILE_RESTORE${RESET} from backup? (y/N): "
      read file2restore

      if [[ "$file2restore" == [Yy]* ]]; then
        if cp "$BACKUP_FILE" "$DIRPATH/$FILE_RESTORE"; then
          echo "${OK} - $FILE_RESTORE restored!" 2>&1 | tee -a "$LOG"
        else
          echo "${ERROR} - Failed to restore $FILE_RESTORE!" 2>&1 | tee -a "$LOG"
        fi
      else
        echo "${NOTE} - Skipped restoring $FILE_RESTORE." 2>&1 | tee -a "$LOG"
      fi
    else
      echo "${ERROR} - Backup file $BACKUP_FILE does not exist."
    fi
  done
fi

printf "\n%.0s" {1..1}

# Define the target directory for rofi themes
rofi_DIR="$HOME/.local/share/rofi/themes"

if [ ! -d "$rofi_DIR" ]; then
  mkdir -p "$rofi_DIR"
fi
if [ -d "$HOME/.config/rofi/themes" ]; then
  if [ -z "$(ls -A $HOME/.config/rofi/themes)" ]; then
    echo '/* Dummy Rofi theme */' >"$HOME/.config/rofi/themes/dummy.rasi"
  fi
  ln -snf "$HOME/.config/rofi/themes/"* "$HOME/.local/share/rofi/themes/"
  # Delete the dummy file if it was created
  if [ -f "$HOME/.config/rofi/themes/dummy.rasi" ]; then
    rm "$HOME/.config/rofi/themes/dummy.rasi"
  fi
fi

printf "\n%.0s" {1..1}

# wallpaper stuff
PICTURES_DIR="$(xdg-user-dir PICTURES 2>/dev/null || echo "$HOME/Pictures")"
mkdir -p "$PICTURES_DIR/wallpapers"
if cp -r wallpapers "$PICTURES_DIR/"; then
  echo "${OK} Some ${MAGENTA}wallpapers${RESET} copied successfully!" | tee -a "$LOG"
else
  echo "${ERROR} Failed to copy some ${YELLOW}wallpapers${RESET}" | tee -a "$LOG"
fi

# Set some files as executable
chmod +x "$HOME/.config/hypr/scripts/"* 2>&1 | tee -a "$LOG"
chmod +x "$HOME/.config/hypr/UserScripts/"* 2>&1 | tee -a "$LOG"
# Set executable for initial-boot.sh
chmod +x "$HOME/.config/hypr/initial-boot.sh" 2>&1 | tee -a "$LOG"

chassis_type=$(detect_waybar_config)
if [ "$chassis_type" = "desktop" ]; then
  config_file="$waybar_config"
  config_remove=" Laptop"
else
  config_file="$waybar_config_laptop"
  config_remove=""
fi

# Check if ~/.config/waybar/config does not exist or is a symlink
if [ ! -e "$HOME/.config/waybar/config" ] || [ -L "$HOME/.config/waybar/config" ]; then
  ln -sf "$config_file" "$HOME/.config/waybar/config" 2>&1 | tee -a "$LOG"
fi

# Remove inappropriate waybar configs
rm -rf "$HOME/.config/waybar/configs/[TOP] Default$config_remove" \
  "$HOME/.config/waybar/configs/[BOT] Default$config_remove" \
  "$HOME/.config/waybar/configs/[TOP] Default$config_remove (old v1)" \
  "$HOME/.config/waybar/configs/[TOP] Default$config_remove (old v2)" \
  "$HOME/.config/waybar/configs/[TOP] Default$config_remove (old v3)" \
  "$HOME/.config/waybar/configs/[TOP] Default$config_remove (old v4)" 2>&1 | tee -a "$LOG" || true

printf "\n%.0s" {1..1}

# for SDDM (simple_sddm_2)
sddm_simple_sddm_2="/usr/share/sddm/themes/simple_sddm_2"
if [ -d "$sddm_simple_sddm_2" ] && [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${NOTE} Express mode: skipping SDDM wallpaper prompt." 2>&1 | tee -a "$LOG"
elif [ -d "$sddm_simple_sddm_2" ]; then
  while true; do
    echo -n "${CAT} SDDM simple_sddm_2 theme detected! Apply current wallpaper as SDDM background? (y/n): "
    read SDDM_WALL

    # Remove any leading/trailing whitespace or newlines from input
    SDDM_WALL=$(echo "$SDDM_WALL" | tr -d '\n' | tr -d ' ')

    case $SDDM_WALL in
    [Yy])
      # Copy the wallpaper, ignore errors if the file exists or fails
      sudo -n cp -r "config/hypr/wallpaper_effects/.wallpaper_current" "/usr/share/sddm/themes/simple_sddm_2/Backgrounds/default" || true
      echo "${NOTE} Current wallpaper applied as default SDDM background" 2>&1 | tee -a "$LOG"
      break
      ;;
    [Nn])
      echo "${NOTE} You chose not to apply the current wallpaper to SDDM." 2>&1 | tee -a "$LOG"
      break
      ;;
    *)
      echo "Please enter 'y' or 'n' to proceed."
      ;;
    esac
  done
fi

# additional wallpapers
printf "\n%.0s" {1..1}
echo "${MAGENTA}By default only a few wallpapers are copied${RESET}..."

if [ "$EXPRESS_MODE" -eq 1 ]; then
  echo "${NOTE} Express mode: skipping additional wallpaper download prompt." 2>&1 | tee -a "$LOG"
else
  while true; do
    echo "${NOTE} A number of these wallpapers are AI generated or enhanced. Select (N/n) if this is an issue for you. "
    echo -n "${CAT} Would you like to download additional wallpapers? ${WARN} This is 1GB in size (y/n): "
    read WALL

    case $WALL in
    [Yy])
      echo "${NOTE} Downloading additional wallpapers..."
      if git clone "https://github.com/JaKooLit/Wallpaper-Bank.git"; then
        echo "${OK} Wallpapers downloaded successfully." 2>&1 | tee -a "$LOG"

        # Check if wallpapers directory exists and create it if not
        if [ ! -d "$PICTURES_DIR/wallpapers" ]; then
          mkdir -p "$PICTURES_DIR/wallpapers"
          echo "${OK} Created wallpapers directory." 2>&1 | tee -a "$LOG"
        fi

        if cp -R Wallpaper-Bank/wallpapers/* "$PICTURES_DIR/wallpapers/" >>"$LOG" 2>&1; then
          echo "${OK} Wallpapers copied successfully." 2>&1 | tee -a "$LOG"
          rm -rf Wallpaper-Bank 2>&1 # Remove cloned repository after copying wallpapers
          break
        else
          echo "${ERROR} Copying wallpapers failed" 2>&1 | tee -a "$LOG"
        fi
      else
        echo "${ERROR} Downloading additional wallpapers failed" 2>&1 | tee -a "$LOG"
      fi
      ;;
    [Nn])
      echo "${NOTE} You chose not to download additional wallpapers." 2>&1 | tee -a "$LOG"
      break
      ;;
    *)
      echo "Please enter 'y' or 'n' to proceed."
      ;;
    esac
  done
fi

# Execute the cleanup function
if [ "$EXPRESS_MODE" -eq 1 ]; then
  cleanup_backups auto "$LOG"
else
  cleanup_backups prompt "$LOG"
fi

# Check if ~/.config/waybar/style.css does not exist or is a symlink
if [ ! -e "$HOME/.config/waybar/style.css" ] || [ -L "$HOME/.config/waybar/style.css" ]; then
  ln -sf "$waybar_style" "$HOME/.config/waybar/style.css" 2>&1 | tee -a "$LOG"
fi

printf "\n%.0s" {1..1}

# initialize wallust to avoid config error on hyprland
wallust run -s $wallpaper 2>&1 | tee -a "$LOG"

printf "\n%.0s" {1..2}
printf "${OK} GREAT! KooL's Hyprland-Dots is now Loaded & Ready !!! "
printf "\n%.0s" {1..1}
printf "${INFO} However, it is ${MAGENTA}HIGHLY SUGGESTED${RESET} to logout and re-login or better reboot to avoid any issues"
printf "\n%.0s" {1..1}
printf "${SKY_BLUE}Thank you${RESET} for using ${MAGENTA}KooL's Hyprland Configuration${RESET}... ${YELLOW}ENJOY!!!${RESET}"
printf "\n%.0s" {1..3}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage