-
Notifications
You must be signed in to change notification settings - Fork 1
/
create-symlinks.sh
executable file
·96 lines (80 loc) · 2.17 KB
/
create-symlinks.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
#!/bin/sh
set -e
reldir=".config/dotfiles"
### sanity checks
if [ "$HOME/$reldir" != "$(pwd)" ]; then
echo "Please clone this repo to ~/$reldir and run from there."
exit 1
fi
### functions
confirm() {
choice=n
printf "%s [yn] " "$1"
# allow single-key responses on shells that support it, fall back to POSIX-compatible
# shellcheck disable=SC2039
read -n1 -r choice 2> /dev/null || read -r choice
printf "\n\n"
case $choice in
[Yy] )
;;
* )
exit 1
;;
esac
}
backup_and_symlink() {
_target="$1"
_linkprefix="$2"
_filename="$3"
printf "%50s " "$_filename"
existing="$HOME/$_filename"
if [ -L "$existing" ] &&
[ "$_linkprefix/$_filename" = "$(readlink $existing)" ]; then
printf "[already linked]\n"
return
elif [ -h "$existing" ] || [ -w "$existing" ]; then
mv "$existing" "$backupdir"
printf "[backup] "
else
printf "[ ] "
fi
ln -sf "$_linkprefix/$_filename" "$_target"
printf "[symlink]\n"
handled=$((handled + 1))
}
link_dotfiles() {
for file in .??* # make sure we exclude . and .. even in Bourne shell
do
# skip .git and .config
case "$file" in
.git ) continue ;;
.config ) continue ;;
esac
backup_and_symlink "$HOME" "$reldir" "$file"
done
}
link_config_subdirs() {
# note: dotfiles inside .config are intentionally not handled
for file in .config/*
do
case "$file" in
dotfiles ) continue ;;
.config/\* ) continue ;;
esac
backup_and_symlink "$HOME/.config" "dotfiles" "$file"
done
}
### global vars
backupdir="backup/$(date +%Y-%m-%d_%H%M)"
handled=0
### ask for confirmation, except for automated installs
if [ -z "$DOTFILES_AUTO_INSTALL" ]; then
printf "This script is about to replace your existing dotfiles with symlinks to the ones in %s (DANGEROUS).\n" "$(pwd)"
confirm "Are you sure?"
fi
### run
mkdir -p "$backupdir"
printf "\nMaking backups in %s.\n" "$backupdir"
link_dotfiles
link_config_subdirs
printf "\n%d symlinks successfully created.\n" $handled