-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·65 lines (50 loc) · 1.34 KB
/
setup.sh
File metadata and controls
executable file
·65 lines (50 loc) · 1.34 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
#!/bin/bash
#
# foax/dotfiles/setup.sh
# ======================
#
# Perform initial setup for dotfiles.
set -e
dotfile_basedir=$(cd $(dirname $0); pwd)
# I would use an associative array here, but this is only supported by
# bash version 4+. So normal arrays it is.
dotfiles=(oh-my-zsh zshrc zshenv terraformrc)
echoerr() {
echo "$@" 1>&2
}
errexit() {
echoerr "ERROR: $1"
exit $2
}
# Compute relative path between two absolute paths
# https://unix.stackexchange.com/a/269303
relpath() {
local pos="${1%%/}" ref="${2%%/}" down=''
while :; do
test "$pos" = '/' && break
case "$ref" in $pos/*) break;; esac
down="../$down"
pos=${pos%/*}
done
echo "$down${ref##$pos/}"
}
for target in "${dotfiles[@]}"; do
file=".${target}"
abs_file="$HOME/$file"
abs_target="$dotfile_basedir/$target"
if [[ -e $abs_file ]]; then
# Check if a symlink already exists to dotfile target
if [[ -L $abs_file ]]; then
symlink_inode=$(stat -L -f %i "$abs_file")
target_inode=$(stat -L -f %i "$abs_target")
if [[ $symlink_inode == $target_inode ]]; then
continue
fi
fi
echo "Dot file $file already exists. Remove this file and try again afterwards."
else
echo "Setting up symlink $file -> $target"
rel_target="$(relpath "$HOME" "$abs_target")"
ln -s $rel_target $abs_file
fi
done