-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstall_make_symlinks.sh
More file actions
executable file
·123 lines (106 loc) · 3.19 KB
/
install_make_symlinks.sh
File metadata and controls
executable file
·123 lines (106 loc) · 3.19 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
#!/bin/bash
# Install all the dotfiles in this directory (symlink them into place)
# When a file already exists, copy it to an "old_dotfiles" directory first
# When a symlink already exists, replace it
declare -a dotfile_whitelist=(
.bash_profile\
.bash_includes\
.zshrc\
.p10k.zsh\
.gitconfig-shared\
.gitignore\
.inputrc\
.vim\
.vimrc\
)
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
backup_dir="${DIR}/old_dotfiles"
function create_symlink {
echo "created symlink: $2 -> $1"
ln -s $1 $2
}
cd `dirname $0`
for f in ${dotfile_whitelist[@]}; do
# In the repo I don't include the leading dot so they're not all hidden
if [[ $f =~ ^\. ]]
then
repo_filename=${f:1}
else
repo_filename=${f}
fi
repo_file=$(pwd)/${repo_filename}
# if whitelisted file isn't actually in repo, skip
if [ ! -f ${repo_file} -a ! -d ${repo_file} ]
then
continue
fi
dotfile=${HOME}/.${repo_filename}
# if file or dir exists, back it up then create symlink
if [ -f ${dotfile} -o -d ${dotfile} ] && [ ! -h ${dotfile} ]
then
if [ ! -d ${backup_dir} ]
then
echo "WARN: made backup directory ${backup_dir}"
mkdir ${backup_dir}
fi
echo "WARN: moving existing ${dotfile} to ${backup_dir}" 1>&2
mv ${dotfile} ${backup_dir}
create_symlink "${repo_file}" "${dotfile}"
continue
fi
# if symlink exists and is different, warn then create new symlink
if [ -h "${dotfile}" ]
then
current_target=`ls -l ${dotfile} | awk '{print $11}'`
if [ "${current_target}" != "${repo_file}" ]
then
echo "WARN: removed symlink: ${dotfile} -> ${current_target}" 1>&2
rm "${dotfile}"
create_symlink "${repo_file}" "${dotfile}"
else
echo "file ${dotfile} already set correctly" 1>&2
fi
continue
fi
# doesn't exist yet, create symlink
if [ ! -a "${dotfile}" ]
then
create_symlink "${repo_file}" "${dotfile}"
continue
fi
done
# Configs that don't fit the ~/.<name> pattern handled by the whitelist above.
# nvim: individual symlinks (not a whole-dir) so lazy.nvim's generated
# lazy-lock.json can live alongside without ending up in the repo.
# ghostty: lives under ~/.config/ghostty/config (XDG path ghostty reads on macOS).
extra_links=(
"${DIR}/nvim/init.lua:${HOME}/.config/nvim/init.lua"
"${DIR}/nvim/dcosson:${HOME}/.config/nvim/lua/dcosson"
"${DIR}/ghostty/config:${HOME}/.config/ghostty/config"
)
for pair in "${extra_links[@]}"; do
src="${pair%%:*}"
dst="${pair##*:}"
mkdir -p "$(dirname "${dst}")"
if [ -h "${dst}" ]; then
current_target=$(readlink "${dst}")
if [ "${current_target}" != "${src}" ]; then
echo "WARN: removed symlink: ${dst} -> ${current_target}" 1>&2
rm "${dst}"
create_symlink "${src}" "${dst}"
else
echo "file ${dst} already set correctly" 1>&2
fi
continue
fi
if [ -e "${dst}" ]; then
if [ ! -d "${backup_dir}" ]; then
echo "WARN: made backup directory ${backup_dir}"
mkdir "${backup_dir}"
fi
echo "WARN: moving existing ${dst} to ${backup_dir}" 1>&2
mv "${dst}" "${backup_dir}/$(basename ${dst})-$(date +%s)"
fi
create_symlink "${src}" "${dst}"
done
touch "${HOME}/.dotfiles-installed"