forked from andresgongora/synth-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·440 lines (334 loc) · 13.4 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
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#!/bin/bash
## +-----------------------------------+-----------------------------------+
## | |
## | Copyright (c) 2019-2021, Andres Gongora <[email protected]>. |
## | |
## | This program is free software: you can redistribute it and/or modify |
## | it under the terms of the GNU General Public License as published by |
## | the Free Software Foundation, either version 3 of the License, or |
## | (at your option) any later version. |
## | |
## | This program is distributed in the hope that it will be useful, |
## | but WITHOUT ANY WARRANTY; without even the implied warranty of |
## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
## | GNU General Public License for more details. |
## | |
## | You should have received a copy of the GNU General Public License |
## | along with this program. If not, see <http://www.gnu.org/licenses/>. |
## | |
## +-----------------------------------------------------------------------+
##
## QUICK INSTALLER
##
##==============================================================================
## DEPENDENCIES
##==============================================================================
[ "$(type -t include)" != 'function' ]&&{ include(){ { [ -z "$_IR" ]&&_IR="$PWD"&&cd $(dirname "${BASH_SOURCE[0]}")&&include "$1"&&cd "$_IR"&&unset _IR;}||{ local d=$PWD&&cd "$(dirname "$PWD/$1")"&&. "$(basename "$1")"&&cd "$d";}||{ echo "Include failed $PWD->$1"&&exit 1;};};}
include 'bash-tools/bash-tools/user_io.sh'
include 'bash-tools/bash-tools/shell.sh'
##==============================================================================
## FUNCTIONS
##==============================================================================
##------------------------------------------------------------------------------
##
## INSTALL SCRIPT
## This function installs a generic script to the system. It copies the
## script to INSTALL_DIR, and also adds to it all the dependencies from
## common to make the script completely self contained. Also, this
## function copies all configuration files to CONFIG_DIR
##
## ARGUMENTS
## 1. Name of script. (e.g. "status" or "fancy-bash-prompt")
##
installScript()
{
## ARGUMENTS
local operation=$1
local script_name=$2
## EXTERNAL VARIABLES
if [ -z $INSTALL_DIR ]; then echo "INSTALL_DIR not set"; exit 1; fi
if [ -z $RC_FILE ]; then echo "RC_FILE not set"; exit 1; fi
if [ -z $CONFIG_DIR ]; then echo "CONFIG_DIR not set"; exit 1; fi
## LOCAL VARIABLES
local dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
local script="${INSTALL_DIR}/${script_name}.sh"
local source_script="${dir}/synth-shell/${script_name}.sh"
local config_template_dir="${dir}/config_templates"
local uninstaller="${INSTALL_DIR}/uninstall.sh"
local edit_text_file_script="$dir/bash-tools/bash-tools/edit_text_file.sh"
source "$edit_text_file_script"
## TEXT FRAGMENTS
local hook=$(printf '%s'\
"\n"\
"##-----------------------------------------------------\n"\
"## ${script_name}\n"\
"if [ -f ${script} ] && [ -n \"\$( echo \$- | grep i )\" ]; then\n"\
"\tsource ${script}\n"\
"fi")
local script_header=$(printf '%s'\
"##!/bin/bash\n"\
"\n"\
"## +-----------------------------------+-----------------------------------+\n"\
"## | |\n"\
"## | Copyright (c) 2014-2021, https://github.com/andresgongora/synth-shell |\n"\
"## | Visit the above URL for details of license and authorship. |\n"\
"## | |\n"\
"## | This program is free software: you can redistribute it and/or modify |\n"\
"## | it under the terms of the GNU General Public License as published by |\n"\
"## | the Free Software Foundation, either version 3 of the License, or |\n"\
"## | (at your option) any later version. |\n"\
"## | |\n"\
"## | This program is distributed in the hope that it will be useful, |\n"\
"## | but WITHOUT ANY WARRANTY; without even the implied warranty of |\n"\
"## | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |\n"\
"## | GNU General Public License for more details. |\n"\
"## | |\n"\
"## | You should have received a copy of the GNU General Public License |\n"\
"## | along with this program. If not, see <http://www.gnu.org/licenses/>. |\n"\
"## | |\n"\
"## +-----------------------------------------------------------------------+\n"\
"##\n"\
"##\n"\
"## =======================\n"\
"## WARNING!!\n"\
"## DO NOT EDIT THIS FILE!!\n"\
"## =======================\n"\
"##\n"\
"## This file was generated by an installation script.\n"\
"## If you edit this file, it might be overwritten without warning\n"\
"## and you will lose all your changes.\n"\
"##\n"\
"## Visit for instructions and more information:\n"\
"## https://github.com/andresgongora/synth-shell/\n"\
"##\n\n\n")
## INSTALL/UNINSTALL
case "$operation" in
uninstall)
## REMOVE HOOK AND SCRIPT
printInfo "Removed $script_name hook from $RC_FILE"
editTextFile "$RC_FILE" delete "$hook"
if [ -f $script ]; then rm $script; fi
;;
install)
## CHECK THAT INSTALL DIR EXISTS
if [ ! -d $INSTALL_DIR ]; then
printInfo "Creating directory $INSTALL_DIR"
mkdir -p $INSTALL_DIR
fi
## CREATE EMPTY SCRIPT FILE
printInfo "Creating file $script"
if [ -f $script ]; then
rm $script
fi
touch "$script" || exit 1
chmod 755 "$script"
echo -e "${script_header}" >> ${script}
## WARNING!! UGLY PATCH, WORK IN PROGRESS
if [ "$script_name" == "synth-shell-greeter" ]; then
printInfo "Installing as $script"
"${dir}/synth-shell/synth-shell-greeter/setup.sh" "$script" "$CONFIG_DIR"
elif [ "$script_name" == "synth-shell-prompt" ]; then
printInfo "Installing as $script"
"${dir}/synth-shell/synth-shell-prompt/setup.sh" "$script" "$CONFIG_DIR"
else
## ADD CONTENT TO SCRIPT FILE
## - Add common scripts TODO: Make this configurable
## - Add actual script
## - Remove common functions from environment
cat "${dir}/bash-tools/bash-tools/load_config.sh" |\
sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$script"
cat "${dir}/bash-tools/bash-tools/color.sh" |\
sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$script"
cat "${dir}/bash-tools/bash-tools/shorten_path.sh" |\
sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$script"
cat "${dir}/bash-tools/bash-tools/print_utils.sh" |\
sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$script"
cat "$source_script" |\
sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$script"
#echo "unset loadConfigFile" >> "$script"
#echo "unset getFormatCode" >> "$script"
## COPY CONFIGURATION FILES
## - Check if script has config file at all. If so:
## - Create system config folder if there is none
## - Check if there is already some configuration in place
## - If none, copy current configuration
## - If there is, but different, copy with .new extension
## - If example folder exists
## - Copy all examples files (overwrite old examples)
local sys_conf_file="${CONFIG_DIR}/${script_name}.config"
local conf_example_dir="${config_template_dir}/${script_name}.config.examples"
local conf_template="${config_template_dir}/${script_name}.config"
if [ -f $conf_template ]; then
printInfo "Adding config files to $CONFIG_DIR"
if [ ! -d $CONFIG_DIR ]; then
mkdir -p $CONFIG_DIR
fi
if [ ! -f "$sys_conf_file" ]; then
cp -u "${conf_template}" "${sys_conf_file}"
#elif ( ! cmp -s "$conf_template" "$sys_conf_file" ); then
# cp -u "${conf_template}" "${sys_conf_file}.new"
# printWarn "Old configuration file detected"
# printInfo "New file written to ${sys_conf_file}.new"
fi
if [ -d "$conf_example_dir" ]; then
printInfo "Adding example config files to ${CONFIG_DIR}"
cp -ur "$conf_example_dir" "${CONFIG_DIR}/"
fi
fi
## ADD HOOK TO /etc/bash.bashrc
printInfo "Adding $script_name hook to $RC_FILE"
editTextFile "$RC_FILE" append "$hook"
fi
## ADD QUICK-UNINSTALLER
## TODO: FIX
#printInfo "Adding quick uninstaller as $uninstaller"
#editTextFile "$uninstaller" append "$script_header"
#cat "$edit_text_file_script" |\
# sed 's/^#.*$//g;s/[ \t][ \t]*#.*$//g;/^[ \t]*$/d' >> "$uninstaller"
#editTextFile "$uninstaller" append "rm -rf ${CONFIG_DIR}"
#echo "hook=\"$hook\"" >> "$uninstaller"
#echo "editTextFile \"$RC_FILE\" delete \"\$hook\"" >> "$uninstaller"
#echo "unset hook" >> "$uninstaller"
#chmod +x "$uninstaller"
printSuccess "Script $script_name succesfully installed"
## EXTRA NOTES DEPENDING ON SCRIPT
local optional_packages=""
if [ $script_name == "status" ]; then
local optional_packages="lm_sensors"
elif [ $script_name == "fancy-bash-prompt" ]; then
local optional_packages="powerline-fonts"
fi
if [ -n "$optional_packages" ]; then
printInfo "Consider installing the following packages as well."
printInfo "The exact name might change between distributions:"
printText "$optional_packages"
fi
## Print final separator
echo ""
;;
*)
echo $"Usage: $0 {install|uninstall}"
exit 1
;;
esac
}
##------------------------------------------------------------------------------
##
installAll()
{
for script in $SCRIPTS; do
local action=$(promptUser "Install ${script}? (Y/n)" "" "yYnN" "y")
case "$action" in
""|y|Y ) installScript install "${script}"
;;
*) echo ""
esac
done
}
##------------------------------------------------------------------------------
##
uninstallAll()
{
## CHECK IF QUICK-UNINSTALL FILE EXISTS
local uninstaller="${INSTALL_DIR}/uninstall.sh"
if [ -f "$uninstaller" ]; then
## RUN QUICK-UNINSTALLER
"$uninstaller"
else
for script in $SCRIPTS; do
installScript uninstall "$script"
done
fi
}
##==============================================================================
## MAIN
##==============================================================================
##------------------------------------------------------------------------------
##
installerSystem()
{
local option=$1
local INSTALL_DIR="/usr/local/bin"
local CONFIG_DIR="/etc/synth-shell"
local RC_FILE="/etc/bash.bashrc"
if [ $(id -u) -ne 0 ]; then
printError "Please run as root"
exit
fi
printInfo "Running systemwide"
case "$option" in
uninstall) printInfo "Uninstalling synth-shell"
uninstallAll
printSuccess "synth-shell was uninstalled"
;;
""|install) printInfo "Installing synth-shell"
installAll
printSuccess "synth-shell was installed"
;;
*) echo "Usage: $0 {install|uninstall}" & exit 1
esac
}
##------------------------------------------------------------------------------
##
installerUser()
{
local option=$1
local INSTALL_DIR="${HOME}/.config/synth-shell"
local CONFIG_DIR="${HOME}/.config/synth-shell"
local user_shell=$(getShellName)
printInfo "Running for user $USER"
case "$user_shell" in
bash) local RC_FILE="${HOME}/.bashrc" ;;
zsh) local RC_FILE="${HOME}/.zshrc" ;;
*) local RC_FILE="${HOME}/.bashrc"
printInfo "Could not determine user shell. I will install the scripts into $RC_FILE"
esac
case "$option" in
uninstall) printInfo "Uninstalling synth-shell"
uninstallAll
printSuccess "synth-shell was uninstalled"
;;
""|install) printInfo "Installing synth-shell"
installAll
printSuccess "synth-shell was installed"
;;
*) echo "Usage: $0 {install|uninstall}" & exit 1
esac
}
##------------------------------------------------------------------------------
##
## PROMPT USER FOR INSTALLATION OPTIONS
##
## USER INSTALL ONLY: Will all code to user's home dir
## and add hooks to its own bashrc file.
## SYSTEM WIDE INSTALL: Will add code to system and hooks to
## /etc/bash.bashrc file.
##
installer()
{
local SCRIPTS="
synth-shell-greeter
synth-shell-prompt
better-ls
alias
better-history
"
if [ "$#" -eq 0 ]; then
printHeader "Installation wizard for synth-shell"
local action=$(promptUser "Would you like to install or uninstall synth-shell?" "[i] install / [u] uninstall. Default i" "iIuU" "i")
case "$action" in
""|i|I ) local install_option="install" ;;
u|U ) local install_option="uninstall" ;;
*) printError "Invalid option"; exit 1
esac
local action=$(promptUser "Would you like to install it for your current user only (recommended),\n\tor system wide (requires elevated privileges)?" "[u] current user only / [s] system wide install. Default u" "uUsS" "u")
case "$action" in
""|u|U ) installerUser $install_option ;;
s|S ) sudo bash -c "bash $0 $install_option" ;;
*) printError "Invalid option"; exit 1
esac
else
installerSystem $1
fi
}
installer "$@"