-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.setup.sh
executable file
·288 lines (253 loc) · 7.52 KB
/
.setup.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/env bash
# Automated desktop setup, v1.0.0
# Sergi Meseguer <zigotica@gmail.com>
#
# Inspired by / heavily modified from:
# https://github.com/thoughtbot/laptop/
# https://github.com/mathiasbynens/dotfiles/
# https://github.com/LukeSmithxyz/LARBS
#
############################################################################
# UTILS
############################################################################
# Reusable defaults
dryrun=1
silent=0
answer=""
# Indicator for boolean question replies
green="$(tput setaf 2)"
reset="$(tput sgr0)"
options_def_true=$(echo -ne "(options: ${green}\e[4mY\e[0m${green}es${reset} \e[4mN\e[0mo \e[4mS\e[0mkip \e[4mQ\e[0muit")
options_def_false=$(echo -ne "(options: \e[4mY\e[0mes ${green}\e[4mN\e[0m${green}o${reset} \e[4mS\e[0mkip \e[4mQ\e[0muit")
options_end=$(echo -ne ")")
current_intro=" - current value:"
# Spacer / title utils
spacer() {
[[ $1 == "h1" ]] && echo " ############################################################################";
[[ $1 == "h2" ]] && echo " ----------------------------------------------------------------------------";
[[ $1 == "h3" ]] && echo " -----> $2";
}
h1() {
echo; echo; spacer "h1"; echo " $1"; spacer "h1";
}
h2() {
echo; spacer "h2"; echo " $1"; spacer "h2"; echo;
}
h3() {
echo; spacer "h3" "$1"; echo;
}
# Detect -f required argument and optional arguments -r (real run) and/or -s (silent)
while getopts srf: option
do
case "${option}" in
r) dryrun=0;;
s) silent=1;;
f) filename=${OPTARG};;
*)
echo "usage: $0 [-r] [-s] [-f <filename>]" >&2
exit 1
;;
esac
done
echo;echo
if [ $silent == 1 ]; then
h1 "Running Setup in silent mode"
h2 "(auto reply default answer to all questions)"
else
h1 "Running Setup in interactive mode..."
if [ ! -t 0 ]; then
echo " Interactive mode needs a terminal for STDIN! Exiting." >&2
exit 1
fi
fi
echo;echo
if [ $dryrun == 1 ]; then
h1 "Running Setup in dryrun mode"
h2 "(commands will not be run for real)"
else
h1 "Running Setup in real mode..."
fi
echo;echo
############################################################################
# COMMANDS
############################################################################
# Run depending on dryrun
# $1 command
# $2 command info (optional)
runner() {
case "$dryrun" in
1)
h3 "Dry run (fake):" "$2"
echo " $1"
;;
0)
h3 "Running:" "$2"
echo " $1"
eval "$1"
;;
esac
}
# Main ask function
# $1 question
# $2 default value
ask () {
def="Yes"
[[ $2 == false ]] && def="No"
while true; do
read -p " $1 " -n 1 -s -r;
case ${REPLY:0:1} in
"" )
echo "Accepted default ($def)"
answer="accept"
;;
y|Y|1 )
echo "Yes"
answer=true
;;
n|N|0 )
echo "No"
answer=false
;;
q|Q|e|E )
echo "Quit setup"
exit 1;
;;
* )
echo "Skip"
answer=""
;;
esac
break
done
}
# defaults write
# $1 question
# $2 default answer
# $3 command
defaults_ask() {
# Silent, run auto
if [[ $silent == 1 && $2 == false ]]; then
runner "defaults write $3 -bool false" "$1"
elif [[ $silent == 1 ]]; then
runner "defaults write $3 -bool true" "$1"
# Not silent, ask
else
# reformat current data
echo;echo;
CURRENT="No)"
if [[ $cv == 1 || $cv == true ]]; then
CURRENT="Yes)"
fi
## Default true or default false
if [[ $2 == false ]]; then
ask "$1 ${options_def_false} ${current_intro} ${CURRENT}" "$2"
else
ask "$1 ${options_def_true} ${current_intro} ${CURRENT}" "$2"
fi
# Get value if user accepted default
if [[ $answer == "accept" && $2 == false ]]; then
answer=false
elif [[ $answer == "accept" ]]; then
answer=true
fi
# Run the answer through the dryrun check
if [[ $answer == true || $answer == false ]]; then
runner "defaults write $3 -bool $answer" "$1"
fi
fi
}
# Ask regular command
# $1 question
# $2 default answer
# $3 command if Yes
# $4 command if No (optional)
command_ask() {
# Silent, run auto
if [[ $silent == 1 && $2 == false ]]; then
# if silent and default false, check we passed its command
if [[ $4 != "" ]]; then
runner "$4" "$1"
fi
elif [[ $silent == 1 ]]; then
runner "$3" "$1"
# Not silent, ask
else
echo;echo;
# Default true or default false
if [[ $2 == false ]]; then
ask "$1 ${options_def_false}${options_end}" "$2"
else
ask "$1 ${options_def_true}${options_end}" "$2"
fi
# Get value if user accepted default
if [[ $answer == "accept" && $2 == false ]]; then
answer=false
elif [[ $answer == "accept" ]]; then
answer=true
fi
# Run the answer through the dryrun check
if [[ $answer == true ]]; then
runner "$3" "$1"
elif [[ $answer == false && $4 != "" ]]; then
runner "$4" "$1"
fi
fi
}
############################################################################
# BATCH PROCESSING
############################################################################
echo " Starting batch processing of data file..."
extension="${filename##*.}"
if [ ! -f "$filename" ]; then
h2 "$filename is not a readable file, exit installation"
exit 1
elif [[ ! $extension =~ "txt" && ! $extension =~ "csv" ]]; then
h2 "$filename is not a txt nor csv file, exit installation"
exit 1
fi
echo " Reading $filename"
############################################################################
# CSV PARSING
############################################################################
if [[ $extension == "csv" ]]; then
loop_data_file() {
(cp "$filename" ./tmp.csv) || sed '/^#/d' > ./tmp.csv
IFS=$'\n' read -d '' -ra lines < ./tmp.csv ;
rm ./tmp.csv
}
loop_data_file
for line in "${lines[@]}"; do
IFS=',' read -r what question defaultanswer commandYes commandNo <<< "$line"
case "$what" in
"#")
h1 "$question"
;;
"##")
h2 "$question"
;;
"d")
defaults_ask "$question" $defaultanswer "$commandYes"
;;
"c")
command_ask "$question" $defaultanswer "$commandYes" "$commandNo"
;;
"b")
command_ask "Install $question ?" true "brew install $question" "brew uninstall $question"
;;
"k")
command_ask "Install $question cask?" true "brew install --cask $question" "brew uninstall --cask $question"
;;
"r")
# Run a command without asking
runner "$commandYes" "$question"
;;
esac
done
fi
############################################################################
# TXT PARSING
############################################################################
if [[ $extension == "txt" ]]; then
value=$(<$filename)
runner "$value"
fi