-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit.sh
More file actions
executable file
·254 lines (202 loc) · 5.75 KB
/
init.sh
File metadata and controls
executable file
·254 lines (202 loc) · 5.75 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env bash
set -o nounset -o pipefail -o errexit
readonly DOTFILES_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
print_title() {
local line="--------------------------------------------------------------------------------"
printf -- "%s%s%s\n" "+-" "${line:0:${#1}}" "-+"
printf -- "%s%s%s\n" "| " "${1}" " |"
printf -- "%s%s%s\n" "+-" "${line:0:${#1}}" "-+"
}
usage() {
printf -- "Usage: %s [flags]\n" "${0}"
printf -- " -a\tRun all stages: clean, install and passwords\n"
printf -- " -c\tClean install and temporary files\n"
printf -- " -d\tRecreate dotfilesrc\n"
printf -- " -i\tInstall and configure softwares\n"
printf -- " -l\tLimiting execution to given filename\n"
printf -- " -p\tRetrieve and write credentials\n"
printf -- " -h\tPrint this help\n"
}
symlink_home() {
local SYMLINK_SOURCE="${DOTFILES_DIR}/symlinks/${1}"
local SYMLINK_TARGET="${HOME}/${1}"
rm -rf "${SYMLINK_TARGET}"
if [[ ${SYMLINK_ONLY_CLEAN:-} != "true" ]]; then
if ! [[ -e "$(dirname "${SYMLINK_TARGET}")" ]]; then
mkdir -p "$(dirname "${SYMLINK_TARGET}")"
fi
[[ -r ${SYMLINK_SOURCE} ]] && [[ -e ${SYMLINK_SOURCE} ]] && ln -s "${SYMLINK_SOURCE}" "${SYMLINK_TARGET}"
fi
}
do_action() {
local ACTION_FILENAME="${1}"
shift
for action in "${@}"; do
unset -f "${action}"
done
source "${DOTFILES_DIR}/installations/${ACTION_FILENAME}"
for action in "${@}"; do
if [[ $(type -t "${action}") == "function" ]]; then
print_title "${action} - ${ACTION_FILENAME}"
"${action}"
fi
done
}
do_mandatory_actions() {
do_action "__default.sh" "${@}"
do_action "__fzf.sh" "${@}"
}
source_dotfilesrc() {
if [[ -e "${HOME}/.dotfilesrc" ]]; then
source "${HOME}/.dotfilesrc"
fi
}
create_dotfilesrc() {
printf -- "Select files to install\n"
source_dotfilesrc
local FILES=()
local FZF_BIND_ACTIONS=""
local POS=0
while IFS= read -r -d '' file; do
FILES+=("${file}")
POS=$((POS + 1))
local BASENAME_FILE
BASENAME_FILE="$(basename "${file}")"
local INSTALL_NAME
INSTALL_NAME="$(printf -- "%s" "${BASENAME_FILE%.sh}" | tr "[:lower:]" "[:upper:]")"
local ENABLE_VARIABLE_NAME="DOTFILES_${INSTALL_NAME}"
if [[ ${!ENABLE_VARIABLE_NAME:-} == "true" ]]; then
FZF_BIND_ACTIONS+="pos(${POS})+select+"
fi
done < <(find "${DOTFILES_DIR}/installations" -not -name "__*" -type f -print0 | LC_ALL=C sort --zero-terminated)
local FZF_OPTS=(--multi --print0 --preview 'cat {}')
if [[ -n ${FZF_BIND_ACTIONS} ]]; then
FZF_OPTS+=(--bind "load:${FZF_BIND_ACTIONS}pos(1)")
fi
cat >"${HOME}/.dotfilesrc" <<END_OF_DOTFILES_RC
#!/usr/bin/env bash
END_OF_DOTFILES_RC
while IFS= read -r -d '' file; do
local BASENAME_FILE
BASENAME_FILE="$(basename "${file}")"
local INSTALL_NAME
INSTALL_NAME="$(printf -- "%s" "${BASENAME_FILE%.sh}" | tr "[:lower:]" "[:upper:]")"
echo "export DOTFILES_${INSTALL_NAME}=\"true\"" >>"${HOME}/.dotfilesrc"
done < <(printf '%s\n' "${FILES[@]}" | fzf "${FZF_OPTS[@]}")
}
browse_actions() {
if [[ ${DOTFILES_RC} -ne 1 ]] && [[ -z ${FILE_LIMIT:-} ]]; then
do_mandatory_actions "${@}"
fi
local FILE_TO_INSTALL=()
while IFS= read -r -d '' file; do
local BASENAME_FILE
BASENAME_FILE="$(basename "${file}")"
local INSTALL_NAME
INSTALL_NAME="$(printf -- "%s" "${BASENAME_FILE%.sh}" | tr "[:lower:]" "[:upper:]")"
if [[ -n ${FILE_LIMIT:-} ]] && [[ ${INSTALL_NAME} != "${FILE_LIMIT}" ]]; then
continue
fi
if [[ ${FILE_RESTART} -eq 1 ]]; then
FILE_LIMIT=""
fi
local ENABLE_VARIABLE_NAME="DOTFILES_${INSTALL_NAME}"
if [[ ${!ENABLE_VARIABLE_NAME:-} != "true" ]]; then
continue
fi
FILE_TO_INSTALL+=("${BASENAME_FILE}")
done < <(find "${DOTFILES_DIR}/installations/"*.sh -not -name "__*" -type f -print0 | LC_ALL=C sort --zero-terminated)
for install_file in "${FILE_TO_INSTALL[@]}"; do
do_action "${install_file}" "${@}"
done
}
source_bashrc() {
if [[ -e "${HOME}/.bashrc" ]]; then
print_title "Sourcing ~/.bashrc"
set +u
set +e
PS1="$" source "${HOME}/.bashrc"
set -e
set -u
fi
}
main() {
local FILE_LIMIT=""
local FILE_RESTART=0
local DOTFILES_RC=0
local RUN_CLEAN=0
local RUN_INSTALL=0
local RUN_PASSWORDS=0
OPTIND=0
while getopts ":l:r:acdhip" option; do
case "${option}" in
a)
RUN_CLEAN=1
RUN_INSTALL=1
RUN_PASSWORDS=1
;;
c)
RUN_CLEAN=1
;;
d)
DOTFILES_RC=1
;;
h)
usage
return 1
;;
i)
RUN_INSTALL=1
;;
p)
RUN_PASSWORDS=1
;;
l)
FILE_LIMIT="$(printf -- "%s" "${OPTARG}" | tr "[:lower:]" "[:upper:]")"
printf -- "Limiting to %s\n" "${FILE_LIMIT}"
;;
r)
FILE_LIMIT="$(printf -- "%s" "${OPTARG}" | tr "[:lower:]" "[:upper:]")"
FILE_RESTART=1
printf -- "Restarting at %s\n" "${FILE_LIMIT}"
;;
:)
printf -- "option -%s requires a value\n" "${OPTARG}" >&2
exit 1
;;
\?)
printf -- "option -%s is invalid\n" "${OPTARG}" >&2
usage
exit 2
;;
esac
done
shift $((OPTIND - 1))
local ACTIONS=()
if [[ ${RUN_CLEAN} -eq 1 ]]; then
ACTIONS+=("clean")
fi
if [[ ${RUN_INSTALL} -eq 1 ]]; then
ACTIONS+=("install")
fi
rm -f "${HOME}/.bashrc"
ln -s "${DOTFILES_DIR}/symlinks/.bashrc" "${HOME}/.bashrc"
source_bashrc
if [[ ${DOTFILES_RC} -eq 1 ]]; then
if ! command -v fzf >/dev/null 2>&1; then
do_mandatory_actions "${ACTIONS[@]}"
fi
source_bashrc
create_dotfilesrc
fi
source_dotfilesrc
if [[ ${#ACTIONS[@]} -ne 0 ]]; then
browse_actions "${ACTIONS[@]}"
packages_clean
fi
source_bashrc
if [[ ${RUN_PASSWORDS} -eq 1 ]]; then
browse_actions "credentials"
fi
}
main "${@}"