-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path_make_symlinks.sh
77 lines (66 loc) · 1.81 KB
/
_make_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
#!/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
function create_symlink {
echo "Created symlink: $2 -> $1"
ln -s $1 $2
}
declare -a dotfile_whitelist=(
.bash_profile\
.bash_includes\
.gitconfig\
.gitignore\
.zshrc\
)
backup_dir="${HOME}/old_dotfiles"
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