-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·115 lines (105 loc) · 2.93 KB
/
install.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
107
108
109
110
111
112
113
114
115
#!/bin/bash
DOTFILES="$(cd -- "$(dirname -- "${BASH_SOURCE[0]:-$0}")" && pwd -P)"
IGNORED_TARGETS=(
'.gitignore'
'install.sh'
'zsh/functions'
'zsh/init.zsh'
)
MAKE_DIRECTORY=(
'bin'
'vim/.goenv'
'vim/.ndenv'
'vim/.pyenv'
'vim/.rbenv'
'vim/.rsenv'
'zsh'
)
usage() {
cat >&2 <<'EOF'
Usage: install.sh [-af] [-d DESTDIR] [-t {all|dev,tmux,x11}]
Options:
-a prompt before install
-d destination directory (default: $HOME)
-f force to overwrite an existing destination files
-t install targets
EOF
exit 1
}
overwrite_prompt() {
local -l choice
read -r -n 1 -u 3 -p "$1 already exists. overwrite? [y/N]: " choice
[[ "${choice}" != $'\n' ]] && echo 1>&2
[[ "${choice}" == y ]]
}
ask_prompt() {
local -l choice
read -r -n 1 -u 3 -p "install $1? [Y/n]: " choice
[[ "${choice}" != $'\n' ]] && echo 1>&2
[[ "${choice}" != n ]]
}
list_targets() {
local makedirs dir target pattern
readarray -t makedirs < <(printf '%s\n' "${MAKE_DIRECTORY[@]}" \
| sed -n -e 'p;:a' -e '/\//{s:/[^/]*$::;p;ta;}' | sort -ur)
git -C "${DOTFILES}" ls-files -co | while IFS= read -r target; do
[[ -f "${DOTFILES}/${target}" ]] || continue
for pattern in "${IGNORED_TARGETS[@]}"; do
[[ "${target}/" == "${pattern}"/* ]] && continue 2
done
for dir in "${makedirs[@]}"; do
[[ "${target}/" == "${dir}"/* ]] || continue
target="${target#"${dir}"/}"
echo "${dir}/${target%%/*}"
continue 2
done
echo "${target%%/*}"
done | sort -u
}
OPT_ASK=0
OPT_FORCE=0
DESTDIR="${HOME}"
TARGETS=""
while getopts :afd:t: opt; do
case "${opt}" in
a) OPT_ASK=1 ;;
f) OPT_FORCE=1 ;;
d) DESTDIR="${OPTARG}" ;;
t)
if [[ "${OPTARG}" == all ]]; then
TARGETS="dev,tmux,x11"
else
TARGETS="${OPTARG}"
fi
;;
*) usage ;;
esac
done
(( OPT_FORCE )) && OPT_ASK=0
if [[ ",${TARGETS}," != *,dev,* ]]; then
IGNORED_TARGETS+=( bin/git-delta gitconfig gitignore )
IGNORED_TARGETS+=( vim/.goenv vim/.ndenv vim/.pyenv vim/.rbenv vim/.rsenv )
IGNORED_TARGETS+=( vim/devplugins.toml vim/efm-langserver.yaml )
fi
if [[ ",${TARGETS}," != *,tmux,* ]]; then
IGNORED_TARGETS+=( tmux tmux.conf )
fi
if [[ ",${TARGETS}," != *,x11,* ]]; then
IGNORED_TARGETS+=( xprofile )
fi
exec 3<&0
list_targets | while IFS= read -r target; do
src="${DOTFILES}/${target}"
dst="${DESTDIR}/.${target}"
[[ "${src}" -ef "${dst}" ]] && continue
if [[ -e "${dst}" ]]; then
if (( OPT_FORCE )) || overwrite_prompt "${dst}"; then
rm -r -- "${dst}" || exit 1
else
continue
fi
elif (( OPT_ASK )) && ! ask_prompt "${dst}"; then
continue
fi
mkdir -p -- "${dst%/*}" && ln -sfT -- "${src}" "${dst}" || exit 1
done