-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall-dotfiles
executable file
·65 lines (51 loc) · 2.17 KB
/
install-dotfiles
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
#!/bin/sh
set -eu
root="$(dirname "$(realpath "$0")")"
if [ "$#" -eq 0 ]; then
# Install all modules
modules=$(find "$root/"* -maxdepth 0 -type d -exec basename {} \;)
else
modules="$*"
fi
backup_path="$HOME/.dotfiles-backup/$(date '+%Y_%m_%d-%H_%M_%S')"
for module in $modules; do
module_path="$root/$module"
if [ ! -d "$module_path" ]; then
>&2 echo "Module $module not found"
continue
fi
echo "Installing dotfiles for module $module"
source_dotfiles_path="$module_path/dotfiles"
dotfiles_submodules_path="$module_path/dotfiles-submodules"
if [ -d "$dotfiles_submodules_path" ]; then
echo "Pulling Git submodules for dotfiles of module $module"
for submodule_path in "$dotfiles_submodules_path/"*; do
submodule_canon_path="$(readlink -f "$submodule_path")" # solve symlinks if any
git submodule update --init "$submodule_canon_path"
done
fi
for source_dotfile_path in "$source_dotfiles_path/."[!.]*; do
target_dotfile_relpath="$(basename "$source_dotfile_path" | sed "s/%2F/\//g")"
mkdir -p "$(dirname "$HOME"/"$target_dotfile_relpath")" # rsync expects parent dir to exist
if [ -d "$source_dotfile_path" ]; then
rsync --archive --copy-unsafe-links --delete \
--backup --backup-dir="$backup_path/$target_dotfile_relpath" \
"$source_dotfile_path/" "$HOME/$target_dotfile_relpath" # NOTE: trailing / is important
else
backup_dir="$backup_path/$(dirname "$target_dotfile_relpath")"
if [ "${backup_dir#"${backup_dir%/.}"}" = "/." ]; then
# Remove the trailing /. as rsync will break otherwise
backup_dir="${backup_dir%/.}"
fi
rsync --archive --copy-unsafe-links \
--backup --backup-dir="$backup_dir" \
"$source_dotfile_path" "$HOME/$target_dotfile_relpath"
fi
done
echo "Successfully installed dotfiles for module $module"
done
if [ -e "$backup_path" ]; then
echo "Backed up overwritten dotfiles in $backup_path"
else
echo 'All dotfiles already up-to-date, no files overwritten'
fi