-
Notifications
You must be signed in to change notification settings - Fork 5
/
install.sh
executable file
·372 lines (340 loc) · 12.6 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
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
# Terminal-config installation script.
# INSTALLATION STEPS:
# 0) Installation wizard.
# 1) Backup existing bash files.
# 2) Copy over the base bash files.
# 3) Copy or compile the bash color codes file.
# 4) Copy or compile the dotfile files.
# 5) Copy or compile the aliases, plugins, completion files.
# 6) Copy or compile a theme.
# 7) Copy or compile custom bash code file.
function usage() {
echo
echo -e "${echo_bold_cyan}TERMINAL-CONFIG${echo_reset_color}"
echo
echo -e "${echo_bold_yellow}ABOUT${echo_reset_color}"
echo -e " This Wizard will guide you through installing terminal"
echo -e " configuration scripts."
echo
echo -e " ${echo_underline_white}NOTE${echo_reset_color}${echo_normal}: Any previously existing scripts that might get"
echo -e " overwritten will be backed up to the .BAK directory."
echo
echo -e "${echo_bold_green}INSTALLATION${echo_reset_color}"
echo -e " run ./install.sh and follow the wizard."
echo
echo -e "${echo_bold_purple}OPTIONAL ARGUMENTS${echo_reset_color}"
echo -e " -c, --compile Compiles all shell scripts into a single file."
echo -e " -e, --email EMAIL Email address used to parse into"
echo -e " .dotfiles suchas .gitconfig."
echo -e " -b, --home HOME Home directory to install these scripts."
echo -e " Default: $HOME."
echo -e " -i, --install INSTALL Installation type:"
echo -e " 1) Full Install."
echo -e " 2) Light Install."
echo -e " 3) Custom Install."
echo -e " -u, --user USER Unix username used to parse into "
echo -e " .dotfiles suchas .gitconfig. Default: $USER."
echo -e " -s, --script SCRIPT Custom script to include."
echo -e " -t, --theme THEME Custom Bash theme to use."
echo -e " -v, --verbose Verbosity"
echo
}
OPTLIND=1
TC_DIR=`pwd`
DIR_DATE=$(date -u '+%b-%d-%Y-at-%H-%M-%S')
opt_verbose=false opt_compile=false opt_home=$HOME opt_user=$USER
# Load the bash colors file.
if [ -f ${TC_DIR}/bash/colors/colors.bash ]; then
source ${TC_DIR}/bash/colors/colors.bash
fi
# Load getopts_long
if [ -f ${TC_DIR}/bash/plugins/getopts_long.plugins.bash ]; then
source ${TC_DIR}/bash/plugins/getopts_long.plugins.bash
fi
while getopts_long b:ce:hi:s:t:u:v opt \
compile no_argument \
email required_argument \
home required_argument \
install required_argument \
script required_argument \
theme required_argument \
user required_argument \
verbose no_argument \
help no_argument "" "$@"
do
case "$opt" in
b|home) opt_home=${OPTLARG-$home};;
c|compile) opt_compile=true;;
e|email) opt_email=${OPTLARG-$email};;
h|help) usage; exit 0;;
i|install) opt_install=${OPTLARG-$install};;
s|script) opt_script=${OPTLARG-$script};;
t|theme) opt_theme=${OPTLARG-$theme};;
u|user) opt_user=${OPTLARG-$name};;
v|verbose) opt_verbose=true;;
:) printf >&2 '%s: %s\n' "${0##*/}" "$OPTLERR"
usage
echo "help"
exit 1;;
esac
done
shift "$(($OPTLIND - 1))"
echo -e "${echo_bold_cyan}+------------------------+${echo_reset_color}"
echo -e "${echo_bold_cyan}+ ${echo_bold_white}Terminal-config Wizard${echo_bold_cyan} +${echo_reset_color}"
echo -e "${echo_bold_cyan}+------------------------+${echo_reset_color}"
# 0) Installation wizard.
echo -e "${echo_bold_yellow}Username [${echo_white}$opt_user${echo_bold_yellow}]:${echo_reset_color}"
echo -en "${echo_green}==> ${echo_white}"
read opt_user
if [ -z "${opt_user}" ]; then
opt_user=$USER
fi
echo -e "${echo_bold_yellow}Email [${echo_white}${opt_user}@localhost${echo_bold_yellow}]:${echo_reset_color}"
echo -en "${echo_green}==> ${echo_white}"
read opt_email
if [ -z "${opt_email}" ]; then
opt_email="${opt_user}@localhost"
fi
if [ -z "${opt_install}" ]; then
while true; do
echo -e "${echo_bold_yellow}Select an installation:${echo_reset_color}"
echo -e "${echo_white}1) Full Install${echo_reset_color}"
echo -e "${echo_white}2) Minimal Install${echo_reset_color}"
echo -e "${echo_white}3) Custom Install${echo_reset_color}"
echo -en "${echo_green}==> ${echo_white}"
read opt_install
case "$opt_install" in
1)
break;;
2)
opt_filelist=( general.aliases git.aliases hg.aliases heroku.aliases \
extract.plugins files.plugins network.plugins \
nginx.plugins history.plugins password.plugins \
ssh.plugins system.plugins .gitinore .gitconfig )
break;;
3)
echo -e "${echo_bold_yellow}Show available scripts [N/y]:${echo_reset_color}"
echo -en "${echo_green}==> ${echo_white}"
read opt_available
echo
case "$opt_available" in
Y|y|yes|Yes|YES)
for directory in "aliases" "plugins" "completions"; do
echo -e "${echo_bold_cyan}$(echo ${directory} | tr [a-z] [A-Z]) ${echo_reset_color}"
echo
for file in `ls ${TC_DIR}/bash/${directory}/*`
do
filename=$(echo ${file##*/})
echo " $(echo ${filename%.*})"
done
echo
done
esac
echo -e "${echo_bold_yellow}Choose scripts to install (space separated):${echo_reset_color}"
echo -e "${echo_white}e.g - ffmpeg.plugins git.aliases ...${echo_reset_color}"
echo -en "${echo_green}==> ${echo_white}"
read -a opt_filelist
break;;
*)
echo -e "${echo_bold_red} Invalid Choice${echo_reset_color}"
echo
esac
done
echo
fi
# 1) Backup existing bash files.
# Create the backup directory, if it doesn not exist.
if [ ! -d "${opt_home}/.BAK/" ]; then
mkdir "${opt_home}/.BAK/"
fi
# Files being backed up are stored in a subdirectory by
# the date and time the scripts were installed.
if [ ! -d "${opt_home}/.BAK/$DIR_DATE" ]; then
mkdir "${opt_home}/.BAK/$DIR_DATE"
fi
if [ "$opt_verbose" == "true" ]; then
verbage="Backing up existing terminal files"
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
for file in "bashrc" "bash_profile" "profile" "bash_custom" "bash_theme"
do
if [ -f ${opt_home}/.${file} ]; then
mv "${opt_home}/.${file}" "${opt_home}/.BAK/${DIR_DATE}/.${file}"
fi
done
# Backup any dotfiles
for file in `ls ${TC_DIR}/bash/dotfiles/*`
do
filename=$(echo ${file##*/})
dotfile=$(echo ${filename%%.*})
if [ -f ${opt_home}/.${dotfile} ]; then
mv "${opt_home}/.${dotfile}" "${opt_home}/.BAK/${DIR_DATE}/.${dotfile}"
fi
done
# If the .bash directory already exists, move it to the backup directory.
if [ -d "${opt_home}/.bash/" ]; then
mv "${opt_home}/.bash" "${opt_home}/.BAK/${DIR_DATE}/.bash"
fi
# 2) Create the base bash files.
if [ "$opt_compile" == "false" ]; then
if [ "$opt_verbose" == "true" ]; then
verbage="Creating the base bash files"
verbage="$verbage\n .bashrc"
verbage="$verbage\n .bash_profile"
verbage="$verbage\n .profile files"
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
for file in "bashrc" "bash_profile" "profile"; do
cp "${TC_DIR}/bash/templates/${file}.template.bash" "${opt_home}/.${file}"
done
else
if [ "$opt_verbose" == "true" ]; then
verbage="Creating the base bash files"
verbage="$verbage\n .bashrc"
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
cp "${TC_DIR}/bash/templates/bashrc.template.bash" "${opt_home}/.bashrc"
fi
# Recreate the .bash directory.
if [ "$opt_compile" == "false" ]; then
mkdir "${opt_home}/.bash/"
fi
# 3) Copy or compile the bash color codes file.
if [ "$opt_verbose" == "true" ]; then
verbage="Compiling the bash colors file"
verbage="$verbage\n colors.bash"
if [ "$opt_compile" == "false" ]; then
verbage="Creating the bash colors file"
verbage="$verbage\n colors.bash"
fi
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
if [ "$opt_compile" == "false" ]; then
mkdir "${opt_home}/.bash/colors"
cp "${TC_DIR}/bash/colors/colors.bash" "${opt_home}/.bash/colors/colors.bash"
else
# Strips out the shabang line
tail +2 "${TC_DIR}/bash/colors/colors.bash" >> "${opt_home}/.bashrc"
fi
# 4) Copy or compile the dotfile files.
if [ "$opt_compile" == "false" ]; then
if [ "$opt_verbose" == "true" ]; then
if [ "$opt_install" == "1" ] || [[ $opt_filelist =~ ".dotfiles" ]]; then
verbage="Creating dotfiles"
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
fi
for file in `ls ${TC_DIR}/bash/dotfiles/*`; do
dotfile=$(echo ${file##*/})
filename=${dotfile%%.*}
if [ "$opt_install" == "1" ]; then
if [ "$opt_verbose" == "true" ]; then
echo -e " .${filename}${echo_reset_color}"
fi
render="sed -e 's/{{ opt_email }}/${opt_email}/g' -e 's/{{ opt_user }}/${opt_user}/g' ${TC_DIR}/bash/dotfiles/${dotfile} > ${opt_home}/.${filename}"
eval $render
else
for script in ${opt_filelist[@]}; do
if [ "${TC_DIR}/bash/dotfiles/${script}.bash" = "${file}" ]; then
# File found, copy it.
if [ "$opt_verbose" == "true" ]; then
echo -e " .${filename}${echo_reset_color}"
fi
render="sed -e 's/{{ opt_email }}/${opt_email}/g' -e 's/{{ opt_user }}/${opt_user}/g' ${TC_DIR}/bash/dotfiles/${dotfile} > ${opt_home}/.${filename}"
eval $render
break
fi
done
fi
done
fi
# 5) Copy or compile the aliases, plugins, completion files.
for directory in "aliases" "plugins" "completions"; do
if [ "$opt_verbose" == "true" ]; then
verbage="Compiling ${directory}"
if [ "$opt_compile" == "false" ]; then
verbage="Creating ${directory}"
fi
if [ "$opt_install" == "1" ] || [[ "${opt_filelist[@]}" =~ ".${directory}" ]]; then
echo -e "${echo_green}==> ${echo_white}${verbage}${echo_reset_color}"
fi
fi
for file in `ls ${TC_DIR}/bash/${directory}/*`; do
# Check if the subdirectory exists, if not create it.
if [ "$opt_compile" == "false" ] && [ ! -d "${opt_home}/.bash/${directory}" ]; then
mkdir "${opt_home}/.bash/${directory}"
fi
filename="$(echo ${file##*/})"
if [ ${#opt_filelist[@]} -gt 0 ]; then
for script in ${opt_filelist[@]}; do
if [ "${script}.bash" == "${filename}" ]; then
if [ "$opt_compile" == "false" ]; then
verbage="${echo_green} ${echo_white}${directory}${echo_white}${filename}"
# File found, copy it.
cp "${file}" "${opt_home}/.bash/${directory}/${filename}"
else
# File found, tail it.
tail +2 "${file}" >> "${opt_home}/.bashrc"
fi
if [ "$opt_verbose" == "true" ]; then
verbage="${filename}"
echo -e "${echo_green} ${echo_white}${verbage}${echo_reset_color}"
fi
break
fi
done
else
if [ "$opt_compile" == "false" ]; then
verbage="${filename}"
# File found, copy it.
cp "${file}" "${opt_home}/.bash/${directory}/${filename}"
else
verbage="${filename}"
# File found, tail it.
tail +2 "${file}" >> "${opt_home}/.bashrc"
fi
if [ "$opt_verbose" == "true" ]; then
echo -e " ${verbage}${echo_reset_color}"
fi
fi
done
done
# 6) Copy or compile a theme.
if [ ! -z "$opt_theme" ]; then
for theme in `find ${TC_DIR}/bash/themes/ -type f ! -iname '*.theme'`; do
filepath="$(echo ${theme%%:*/})"
filename="$(echo ${theme##*/})"
name="$(echo ${filename%.*})"
# Name should match something like default.theme
if [ "$name" == "$opt_theme" ]; then
if [ "$opt_compile" == "false" ]; then
verbage="${echo_green}==> ${echo_white}Creating theme${echo_white}"
cp "${theme}" "${opt_home}/.bash_theme"
else
verbage="${echo_green}==> ${echo_white}Compiling theme${echo_white}"
tail +2 "${theme}" >> "${opt_home}/.bashrc"
fi
if [ "$opt_verbose" == "true" ]; then
echo -e "${echo_cyan}${verbage}${echo_reset_color}"
echo -e " ${name}"
fi
fi
done
fi
# 7) Copy or compile custom bash code file.
if [ ! -z "$opt_script" ] && [ -f "$opt_script" ]; then
if [ "$opt_compile" == "false" ]; then
verbage="${echo_green}==> ${echo_white}Creating custom script"
cp "$opt_script" "${opt_home}/.bash_custom"
else
verbage="${echo_green}==> ${echo_white}Compiling custom script"
tail +2 "${opt_script}" >> "${opt_home}/.bashrc"
fi
if [ "$opt_verbose" == "true" ]; then
echo -e "${echo_cyan}${verbage}${echo_reset_color}"
fi
fi
echo
echo -e "${echo_green}All done, Enjoy!${echo_reset_color}"
echo