-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·359 lines (310 loc) · 12.8 KB
/
install.sh
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
#!/usr/bin/env bash
# -----------------------------------------------------------------------------
# Zero-Clause BSD
#
# Permission to use, copy, modify, and/or distribute this software for
# any purpose with or without fee is hereby granted.
#
# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
# FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
# DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
# AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# -----------------------------------------------------------------------------
if [ -z "$BASH_VERSION" ]; then
echo "error: This script must be executed with Bash." >&2
exit 1
fi
dot_dir="$HOME/.dotfiles" # where the dotfiles are
bin_path="$HOME/.bin" # where dotfiles shell scripts will be link or hard-copied
# arguments flags
backup=1
recreate_symlinks=0
use_copy=0
should_remove_files=0
backup_path="$dot_dir/backup"
backup_file="$backup_path/$(date +%H-%M-%S)"
# names of files to symlink or copy ( the dot is optional if you remove it it will link as 'fonts' )
home_files=".zshenv .gitconfig .fonts"
config_files="zsh fish nvim kitty tmux"
binaries="$(ls $dot_dir/bin)"
usage() {
name=$(basename "$0")
echo " $name [OPTIONS...]"
echo ""
echo " DESCRIPTION"
echo " This script facilitates the management of dotfiles by creating"
echo " symbolic links to target files defined within the script."
echo " If symbolic links are unavailable, files are copied directly."
echo " Additionally, the script allows for file exclusion using the '--exclude' option."
echo " You can get a list of excludable files by using '--list-exclude' option."
echo ""
echo ""
echo " The following options are available:"
echo ""
echo " --skip-backup"
echo " Skip creating a backup of existing files."
echo ""
echo " --recreate-symlinks"
echo " By default, existing symbolic links are skipped during symlink creation."
echo " Use this option to force the script to recreate symbolic links, even if they already exist."
echo ""
echo " --force-copy"
echo " Use copy instead of symbolic link. This will use 'cp' command"
echo " which will overwrite your files. By default, a backup will be made"
echo " which can be omitted by passing '--skip-backup' option."
echo ""
echo " --backup-path [$backup_path]"
echo " Set a backup path the default is '$backup_path'"
echo " For symbolic links, the backup will be skipped."
echo ""
echo " USAGE: [ ./$name --backup-path '~/.my-backup' ]"
echo ""
echo " --list-exclude"
echo " Prints a list with the names of all files that can be excluded."
echo ""
echo " --remove-all, --rm-all"
echo " Remove all symbolic links and hard-copied dotfiles. This option"
echo " can be used along with '--exclude' to provide a list of"
echo " files that will be excluded from the deletion process."
echo " A backup will be made if the target files are not symbolic links."
echo ""
echo " USAGE: [ ./$name --rm-all ] or [ ./$name --rm-all-dotfiles --exclude 'zsh, ...' ]"
echo ""
echo " -rm, --remove"
echo " Remove specific symbolic links and hard-copied dotfiles."
echo " A backup will be made if the target file is not a symbolic link."
echo ""
echo " USAGE: [ ./$name --rm 'zsh, ...' ]"
echo ""
echo " -e or --exclude"
echo " Exclude specific files from installation."
echo " You could use '--list-exclude' to show a list of excludable files."
echo ""
echo " USAGE: [ ./$name --exclude 'zsh kitty ...' ] or [ ./$name --exclude 'zsh, kitty, ...' ]"
echo ""
echo " -h or --help"
echo " Display this help message."
echo ""
echo ""
echo " EXAMPLES"
echo ""
echo " 1. Using copy instead of symbolic links (without backup)"
echo " ./$name --force-copy --skip-backup"
echo ""
echo " 2. Using copy with a custom backup path"
echo " ./$name --force-copy --backup-path '~/my-backup'"
echo ""
echo " 3. Recreating symbolic links excluding specific files"
echo " ./$name --exclude 'zsh, tmux' --recreate-symlinks"
echo ""
echo " 4. Removing specific files"
echo " ./$name --remove 'zsh, tmux'"
echo ""
}
while [ "$#" -gt 0 ]; do
name=$(basename "$0")
case $1 in
"")
# skip
;;
"--help" | "-h")
usage
exit 0
;;
"--skip-backup")
backup=0
;;
"--remove" | "-rm")
if [ -z "$2" ] || [ "${2#--}" != "$2" ]; then
echo "./$name: Expected [ $1 'zsh ...' ] but instead got [ $1 $2 ]" >&2
exit 1
fi
files_to_rm=$(echo "$2" | tr ',' ' ')
temp_home_files=$(echo "$home_files")
temp_config_files=$(echo "$config_files")
temp_binaries=$(echo "$binaries")
home_files=""
config_files=""
binaries=""
for name in ${files_to_rm}; do
if echo "$temp_home_files" | grep -qE "(^|\s)$name($|\s)"; then
home_files=$(echo "${home_files} ${name}")
elif echo "$temp_config_files" | grep -qE "(^|\s)$name($|\s)"; then
config_files=$(echo "${config_files} ${name}")
elif echo "$temp_binaries" | grep -qE "(^|\s)$name($|\s)"; then
binaries=$(echo "${binaries} ${name}")
fi
done
# Remove extra spaces resulting from removals
config_files=$(echo "$config_files" | tr -s ' ')
home_files=$(echo "$home_files" | tr -s ' ')
binariess=$(echo "$binaries" | tr -s ' ')
# Trim leading and trailing spaces
home_files=$(echo "$home_files" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
config_files=$(echo "$config_files" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
binaries=$(echo "$binaries" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
should_remove_files=1
break # do not parse more options
;;
"--remove-all" | "--rm-all")
should_remove_files=1
;;
"--recreate-symlinks")
recreate_symlinks=1
;;
"--force-copy")
use_copy=1
;;
"--list-exclude")
excludable_files=$(echo "$home_files $config_files $binaries")
for file_name in ${excludable_files}; do
echo " $file_name"
done
exit 0
;;
"--backup-path")
if [ -z "$2" ] || [ "${2#--}" != "$2" ]; then
echo "./$name: Expected [ $1 '~/path-to-backup' ] but instead got [ $1 $2 ]" >&2
exit 1
fi
backup_path="$2"
shift
;;
"--exclude" | "-e")
if [ -z "$2" ] || [ "${2#--}" != "$2" ]; then
echo "./$name: Expected [ $1 'zsh ...' ] but instead got [ $1 $2 ]" >&2
exit 1
fi
files_to_rm=$(echo "$2" | tr ',' ' ')
for name in ${files_to_rm}; do
home_files=$(echo "$home_files" | sed "s/\b$name\b//g")
config_files=$(echo "$config_files" | sed "s/\b$name\b//g")
binaries=$(echo "$binaries" | sed "s/\b$name\b//g")
done
# Remove extra spaces resulting from removals
config_files=$(echo "$config_files" | tr -s ' ')
home_files=$(echo "$home_files" | tr -s ' ')
binaries=$(echo "$binaries" | tr -s ' ')
# Trim leading and trailing spaces
home_files=$(echo "$home_files" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
config_files=$(echo "$config_files" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
binaries=$(echo "$binaries" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
shift
;;
*)
echo "Unknown argument: $1" >&2
exit 1
;;
esac
shift
done
deletetor() {
file_path="$1"
if [ "$file_path" = "/" ]; then
echo "[REMOVE] REMOVE FROM '/' IS NOT ALLOWED"
else
if [ -L "$file_path" ]; then
rm "$file_path"
echo "[REMOVE] SYMLINK $file_path"
elif [ -d "$file_path" ]; then
rm -r "$file_path"
echo "[REMOVE] DIR $file_path"
elif [ -f "$file_path" ]; then
rm "$file_path"
echo "[REMOVE] FILE $file_path"
fi
fi
}
find_dotfile() {
file_name="$1"
file_name="${file_name#.}"
path=$(find "${dot_dir}" -name "${file_name}" -o -name ".${file_name}" | head -n 1)
echo "$path"
}
makebackup() {
file_path="$1"
if [ -L "$file_path" ]; then
return
elif [ -d "$file_path" ]; then
mkdir -p "$backup_file"
cp -r "$file_path" "$backup_file"
echo "[BACKUP] DIR $file_path IN $backup_file"
elif [ -f "$file_path" ]; then
mkdir -p "$backup_file"
cp "$file_path" "$backup_file"
echo "[BACKUP] FILE $file_path IN $backup_file"
fi
}
removator() {
target_path="${1}"
source_path=""
file_path=""
for file_name in ${2}; do
file_path=$(echo "${target_path:?}/$file_name" | tr -s '/')
source_path=$(find_dotfile "${file_name}")
if [ "$backup" -eq 1 ]; then
makebackup "${file_path}"
fi
deletetor "${file_path}"
done
}
copynator() {
target_path="${1}"
source_path=""
file_path=""
for file_name in ${2}; do
file_path=$(echo "${target_path:?}/$file_name" | tr -s '/')
source_path=$(find_dotfile "${file_name}")
if [ "$backup" -eq 1 ]; then
makebackup "${file_path}"
fi
deletetor "${file_path}"
if [ -d "${source_path}" ]; then
cp -r "${source_path}" "${file_path}"
echo "[COPY] DIR ${source_path} TO ${file_path}"
else
cp "${source_path}" "${file_path}"
echo "[COPY] FILE ${source_path} TO ${file_path}"
fi
done
}
syminator() {
target_path="${1}"
source_path=""
file_path=""
for file_name in ${2}; do
file_path=$(echo "${target_path:?}/$file_name" | tr -s '/')
if [ -L "${file_path}" ] && [ "$recreate_symlinks" -eq 0 ]; then
echo "[SYMLINK] TO ${file_path} ALREADY EXISTS"
else
if [ "$backup" -eq 1 ]; then
makebackup "${file_path}"
fi
deletetor "${file_path}"
source_path=$(find_dotfile "${file_name}")
ln -sv "${source_path}" "${file_path}"
fi
done
}
if [ "$should_remove_files" -eq 1 ]; then
removator "$HOME/.config" "$config_files"
removator "$HOME" "$home_files"
removator "${bin_path}" "${binaries}"
exit 0
fi
# Check if 'ln' command exists
if command -v ln > /dev/null 2>&1 && [ "$use_copy" -eq 0 ]; then
echo "[CREATING SYMBOLIC LINKS]........................."
syminator "$HOME/.config" "$config_files"
syminator "$HOME" "$home_files"
for binary in "${binaries}"; do
syminator "${bin_path}" "${binary}"
done
else
echo "[COPYING DOTFILES]................................"
copynator "$HOME/.config" "$config_files"
copynator "$HOME" "$home_files"
fi