-
Notifications
You must be signed in to change notification settings - Fork 1
/
install-dotfiles
executable file
·142 lines (122 loc) · 2.83 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/bin/bash
#
# install-dotfiles
#
# Installs symlinks to dotfiles in this repo into current user's $HOME dir.
#
# Blows away existing dotfiles, if they're currently symlinks. Does not check to see
# if those symlinks are pointing to what looks like this dotfiles repo or a variant
# of it, so be careful.
set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then set -o xtrace; fi
PROGRAM_NAME="$(basename $0)"
function usage() {
cat <<EOF
$PROGRAM_NAME - install dotfile symlinks to the repo this script is in
Usage:
$PROGRAM_NAME [-x] [-v]
$PROGRAM_NAME -h
Options:
-h display help
-v verbose output
-x trace program execution
EOF
}
VERBOSE=0
OPT_TRACE=0
while getopts vkxh opt; do
case $opt in
v)
VERBOSE=1
;;
x)
OPT_TRACE=1
;;
h)
usage
exit 0
;;
\?)
echo >&2 "See '$PROGRAM_NAME -h' for usage"
exit 1
;;
esac
done
function v_echo() {
if [[ $VERBOSE == 1 ]]; then
echo "$@"
fi
}
function symlink() {
local source="$1"
local target="$2"
if [[ -L "$target" ]]; then
local current_source=$(readlink "$target")
if [[ "$current_source" == "$source" ]]; then
v_echo "Symlink '$target' is already set up"
return
fi
elif [[ -d "$target" && ! -L "$target" ]]; then
echo >&2 "ERROR: '$target' is a non-symlink directory"
exit 1
elif [[ -f "$target" ]]; then
echo "SKIPPED: File '$target' is a regular file"
return
fi
ln -sfn "$source" "$target"
printf '%-18s -> %s\n' "$target" "$source"
CHANGED=1
}
function detect_os_type() {
# Post: $OSNAME is set, or script has exited with an error status
uname=$(uname)
case $uname in
Darwin)
if which sw_vers &>/dev/null && [[ $(sw_vers -productName) == "Mac OS X" ]]; then
OSNAME=macos
else
OSNAME=darwin
fi
;;
*)
OSNAME="$uname"
;;
esac
}
function main() {
sourcedir=$(dirname "$0")
dotfiles_repo_dir="$sourcedir"
dotdir=$(cd $sourcedir/dotfiles; pwd)
todir=$HOME
detect_os_type
(
CHANGED=0
cd $todir
# These files have their names munged to add a leading dot and strip extension.
# Makes it easier to work with them in shell and editors.
if [[ -e "$dotdir/$OSNAME" ]]; then
dotfiles=( $( (ls $dotdir/generic; ls $dotdir/$OSNAME) | sort | uniq ) )
else
dotfiles=($(ls $dotdir/generic))
fi
for file in "${dotfiles[@]}"; do
tofile=".${file%\.sh}"
if [[ -e "$dotdir/$OSNAME/$file" ]]; then
fromfile="$dotdir/$OSNAME/$file"
else
fromfile="$dotdir/generic/$file"
fi
symlink "$fromfile" "$tofile"
done
CHANGED=0
symlink "${dotfiles_repo_dir}/bin" "bin"
if [[ $CHANGED == 1 ]]; then
echo "Linked the ~/bin directory. Don't forget to add it to your \$PATH."
fi
)
echo "Dotfiles are now linked to $dotdir."
}
main
exit 0