-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdotmanager.sh
More file actions
executable file
·223 lines (181 loc) · 6.9 KB
/
dotmanager.sh
File metadata and controls
executable file
·223 lines (181 loc) · 6.9 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
#!/usr/bin/env bash
set -euo pipefail
# ─── CONFIG ────────────────────────────────────────────────────────────────
DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
CONFIG_DIR="$HOME/.config"
BACKUP_DIR="$HOME/.config_backup"
GITHUB_REPO="GitHub: https://github.com/Ly-sec/dotfiles"
# ─── STYLING ───────────────────────────────────────────────────────────────
YELLOW="\033[1;33m"
GREEN="\033[1;32m"
RED="\033[1;31m"
BLUE="\033[1;34m"
RESET="\033[0m"
INFO="➜"
CHECK="✔"
CROSS="✖"
WARN="⚠"
# ─── FLAGS ─────────────────────────────────────────────────────────────────
DRY_RUN=false
# ─── FUNCTIONS ─────────────────────────────────────────────────────────────
print_header() {
local width=90
printf "${BLUE}${RESET} Dotfiles Symlink Setup Script${RESET}"
printf "%*s${BLUE}${RESET}\n" $((width - 27)) ""
printf "${BLUE}${RESET} Safely replaces configs in ~/.config with links from: ${DOTFILES_DIR}${RESET}"
printf "%*s${BLUE}${RESET}\n" $((width - 53)) ""
printf "${BLUE}${RESET} ${GITHUB_REPO}${RESET}"
printf "%*s${BLUE}${RESET}\n" $((width - 44)) ""
}
parse_flags() {
while [[ $# -gt 0 ]]; do
case "$1" in
--dry-run) DRY_RUN=true ;;
*) echo -e "${RED}${CROSS}${RESET} Unknown option: $1" && exit 1 ;;
esac
shift
done
}
is_git_clean() {
if git -C "$DOTFILES_DIR" diff --quiet && \
git -C "$DOTFILES_DIR" diff --cached --quiet && \
! git -C "$DOTFILES_DIR" ls-files --others --exclude-standard | grep -q '.'; then
return 0
fi
return 1
}
show_git_changes() {
echo -e "\n${YELLOW} ${INFO}${RESET} Git status summary in ${DOTFILES_DIR}:"
local unstaged staged untracked
staged=$(git -C "$DOTFILES_DIR" diff --cached --name-only)
unstaged=$(git -C "$DOTFILES_DIR" diff --name-only)
untracked=$(git -C "$DOTFILES_DIR" ls-files --others --exclude-standard)
[[ -n "$staged" ]] && {
echo -e " ${GREEN}Staged but uncommitted:${RESET}"
echo "$staged" | sed 's/^/ - /'
}
[[ -n "$unstaged" ]] && {
echo -e " ${YELLOW}Modified (unstaged):${RESET}"
echo "$unstaged" | sed 's/^/ - /'
}
[[ -n "$untracked" ]] && {
echo -e " ${RED}Untracked files:${RESET}"
echo "$untracked" | sed 's/^/ - /'
}
echo
}
is_git_behind_remote() {
local behind
behind=$(git -C "$DOTFILES_DIR" rev-list --left-right --count origin/HEAD...HEAD 2>/dev/null || echo "")
if [[ "$behind" =~ ^([0-9]+)[[:space:]]([0-9]+)$ ]]; then
local behind_count=${BASH_REMATCH[1]}
if (( behind_count > 0 )); then
return 0
fi
fi
return 1
}
prompt_pull() {
while true; do
read -rp "Local branch is behind remote. Pull latest changes now? [Y/n]: " yn
case "$yn" in
[Yy]* | "")
echo -e " ${INFO} Pulling latest changes..."
git -C "$DOTFILES_DIR" pull --ff-only
return 0
;;
[Nn]* )
echo -e " ${WARN} Skipping pull. You might be working with outdated files."
return 1
;;
* ) echo "Please answer yes or no." ;;
esac
done
}
choose_folders() {
mapfile -t FOLDERS < <(
find "$DOTFILES_DIR" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' |
grep -vE '^\.' | # exclude dotfolders
sort
)
}
prompt_backup_or_skip() {
local target="$1"
local choice
while true; do
read -rp "\"$target\" exists and is not a symlink. Back up and replace? [y/N]: " yn
case "$yn" in
[Yy]*) choice="Backup & replace"; break ;;
[Nn]*|"") choice="Skip"; break ;;
*) echo "Please answer yes or no." ;;
esac
done
echo "$choice"
}
perform_symlinks() {
echo -e "${YELLOW} ${INFO}${RESET} Preparing to link ${#FOLDERS[@]} folders from ${DOTFILES_DIR}\n"
echo
for folder in "${FOLDERS[@]}"; do
local source="$DOTFILES_DIR/$folder"
local target="$CONFIG_DIR/$folder"
if [ ! -d "$source" ]; then
echo -e " ${RED} ${CROSS}${RESET} Source not found: $source"
continue
fi
echo -e "${BLUE}Folder:${RESET} $folder"
if ! git -C "$DOTFILES_DIR" ls-files --error-unmatch "$folder" &>/dev/null; then
echo -e " ${WARN} Warning: Not tracked in git"
fi
if [ -e "$target" ]; then
if [ -L "$target" ]; then
echo -e " ${INFO} Removing existing symlink: $target"
$DRY_RUN || rm "$target"
else
local action
action=$(prompt_backup_or_skip "$target")
if [[ "$action" == "Backup & replace" ]]; then
mkdir -p "$BACKUP_DIR"
echo -e " ${INFO} Backing up $target → $BACKUP_DIR/"
$DRY_RUN || mv "$target" "$BACKUP_DIR/"
else
echo -e " ${CROSS} Skipping: $target exists and is not a symlink."
continue
fi
fi
fi
echo -e " ${INFO} Linking: ~/.config/$folder → ~/$(basename "$DOTFILES_DIR")/$folder"
$DRY_RUN || ln -sfn "$source" "$target"
echo -e " ${GREEN} ${CHECK}${RESET} Linked successfully"
echo
done
echo -e "${GREEN} ${CHECK}${RESET} All dotfiles processed."
}
main() {
print_header
parse_flags "$@"
if [ ! -d "$DOTFILES_DIR/.git" ]; then
echo -e "${WARN} Warning: '${DOTFILES_DIR}' is not a git repository. Git checks will be limited."
else
if ! is_git_clean; then
echo -e "${WARN} Warning: You have uncommitted changes or untracked files in your dotfiles directory."
show_git_changes
fi
if is_git_behind_remote; then
echo -e "${RED}${WARN} Warning: Your local branch is behind the remote repository.${RESET}"
prompt_pull
fi
fi
choose_folders
echo -e "${YELLOW} ${INFO}${RESET} Found ${#FOLDERS[@]} folders to process in dotfiles:"
for folder in "${FOLDERS[@]}"; do
printf " - \033[1m%s\033[0m\n" "$folder"
done
echo
read -rp " Proceed with creating symlinks for these folders? [Y/n]: " proceed
case "$proceed" in
[Nn]*) echo "Aborted by user."; exit 1 ;;
*) echo -e " ${INFO} Proceeding with symlink creation..." ;;
esac
perform_symlinks
}
main "$@"