-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootstrap.sh
executable file
·106 lines (95 loc) · 2.45 KB
/
bootstrap.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
97
98
99
100
101
102
103
104
105
106
#!/bin/sh
# originally copied from from 'https://github.com/mathiasbynens/dotfiles/blob/master/bootstrap.sh'
# extended to not only work with a bash shell, but also with a zsh or ksh93 shell
# it won't work from a dash shell or other ksh variant such as pdksh
# as it turns out, posix doesn't offer any reliable way to get the sourced script's absolute path
force_builtin() {
cmd="$1"
shift
# shellcheck disable=SC2039
if [ -n "$ZSH_VERSION" ]; then
builtin "$cmd" "$@" || return 1
else
command "$cmd" "$@" || return 1
fi
}
cd_builtin() {
force_builtin cd "$@"
}
printf_builtin() {
force_builtin printf "$@"
}
read_builtin() {
force_builtin read "$@"
}
# inspiration for the shell case differentiation drawn from 'http://unix.stackexchange.com/a/96238/104230'
# shellcheck disable=SC2128
if [ -n "$BASH_SOURCE" ]; then
script_name="$BASH_SOURCE"
elif [ -n "$ZSH_VERSION" ]; then
# shellcheck disable=SC2039
builtin setopt function_argzero
script_name="$0"
elif eval '[ -n "$(command echo ${.sh.version})" ]' 2>/dev/null; then # ksh93 flavor
eval 'script_name="${.sh.file}"'
else
printf_builtin "Unsupported shell. Please use bash, zsh or ksh93\n" >&2
return 1
fi
script_dir="$(command dirname "$script_name")"
cd_builtin "$script_dir"
do_it() {
command rsync --exclude ".git/" \
--exclude ".gitignore" \
--exclude ".DS_Store" \
--exclude "bootstrap.sh" \
--exclude "README.md" \
--exclude "LICENSE.txt" \
--exclude "setup/" \
-avh --no-perms . "$HOME"
if [ -n "$BASH" ]; then
case "$BASH" in
*/sh)
# shellcheck source=/dev/null
. "${HOME}/.shrc"
;;
*)
# shellcheck source=/dev/null
. "${HOME}/.bash_profile"
;;
esac
elif [ -n "${ZSH_VERSION}" ]; then
# shellcheck source=/dev/null
. "${HOME}/.zshrc"
else
# shellcheck source=/dev/null
. "${HOME}/.kshrc"
fi
}
if [ "$1" = "--force" ] || [ "$1" = "-f" ]; then
do_it
elif [ -n "$1" ]; then
printf_builtin "Unsupported option or argument \"%s\"\n" "$1" >&2
return 1
else
printf_builtin "This may overwrite existing files in your home directory. Are you sure? (y/n) "
while true; do
read_builtin -r reply
# shellcheck disable=SC2154
case "$reply" in
[yY][eE][sS]|[yY])
do_it
return
;;
[nN][oO]|[nN])
return
;;
*)
printf_builtin "Please answer yes or no: "
;;
esac
done
fi
cd_builtin "$OLDPWD" >/dev/null
unset cmd script_name script_dir old_dir reply
unset -f force_builtin cd_builtin printf_builtin read_builtin do_it