-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbash-config.sh
More file actions
executable file
·361 lines (321 loc) · 11.6 KB
/
Copy pathbash-config.sh
File metadata and controls
executable file
·361 lines (321 loc) · 11.6 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
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
#!/bin/bash
################################################################################
################################################################################
# Name: bash-config.sh
# Usage:
# Description: Sets up bash options
# Created: 2018-09-10
# Copyright 2014, Victor Mendonca - http://victormendonca.com
# - https://github.com/victorbrca
# License: Released under the terms of the GNU GPL license v3
################################################################################
################################################################################
<<COMMENT_USAGE
bash-config enable -t [theme]
bash-config enable -p [plugin]
bash-config disable -t [theme]
bash-config disable -p [plugin]
bash-config list
bash-config install
bash-config aliases {plugin|alias}
COMMENT_USAGE
#-------------------------------------------------------------------------------
# Sets variables
#-------------------------------------------------------------------------------
bash_config_dir="${HOME}/.bash-config"
bash_config_plugins_folder="${bash_config_dir}/plugins"
bash_config_themes_folder="${bash_config_dir}/themes"
# Bash Colors
if [ -f "${bash_config_dir}/lib/bash-colors" ] ; then
. "${bash_config_dir}/lib/bash-colors"
fi
# Sets up usage
usage="${BWhite}bash-config${Color_Off} [install|upgrade|list|enable|disable|aliases] \
{-p [plugin1,plugin2]|-t [theme1,theme2]}"
#-------------------------------------------------------------------------------
# Functions
#-------------------------------------------------------------------------------
_enable ()
{
local OPT file_type file_to_enable files_to_enable dest_file source_file
OPTERR=0
while getopts "t:p:" OPT ; do
case $OPT in
t)
file_type="themes"
files_to_enable="$OPTARG"
;;
p)
file_type="plugins"
files_to_enable="$OPTARG"
;;
\?)
echo "Wrong option -${OPT}"
echo -e "$usage"
exit 1
;;
:)
echo "Missing argument"
echo -e "$usage"
exit 1
;;
esac
done
old_ifs="$IFS"
IFS=,
for file_to_enable in $files_to_enable ; do
dest_file="${bash_config_dir}/${file_type}/enabled/${file_to_enable}.${file_type/s/}.bash"
source_file="${bash_config_dir}/${file_type}/${file_to_enable}.${file_type/s/}.bash"
if [ ! -e "$source_file" ] ; then
echo "The ${file_type/s/} \"$file_to_enable\" doesn't exist"
# exit 1
elif [ -e "$dest_file" ] ; then
echo "The ${file_type/s/} \"$file_to_enable\" is already enabled, skipping"
else
# Disable current theme
if [ "$file_type" = "themes" ] ; then
current_enabled_theme="$(find ${bash_config_dir}/${file_type}/enabled -type l)"
if [ -L "$current_enabled_theme" ] ; then
echo "Disabling the current theme."
unlink "$current_enabled_theme"
fi
fi
ln -sf "$source_file" "$dest_file"
echo "Enabled the ${file_type/s/} \"$file_to_enable\". Please reload your shell."
fi
done
IFS="$old_ifs"
# sleep 1.5
# _list_plugins_and_themes
}
_disable ()
{
local OPT file_type file_to_disable files_to_disable dest_file source_file
OPTERR=0
while getopts "t:p:" OPT ; do
case $OPT in
t)
file_type="themes"
files_to_disable="$OPTARG"
;;
p)
file_type="plugins"
files_to_disable="$OPTARG"
;;
\?)
echo "Wrong option -${OPT}"
echo -e "$usage"
exit 1
;;
:)
echo "Missing argument"
echo -e "$usage"
exit 1
;;
esac
done
old_ifs="$IFS"
IFS=,
for file_to_disable in $files_to_disable ; do
dest_file="${bash_config_dir}/${file_type}/enabled/${file_to_disable}.${file_type/s/}.bash"
if [ ! -e "$dest_file" ] ; then
echo "The ${file_type/s/} \"$file_to_disable\" doesn't seem to be enabled"
# exit 1
else
unlink "$dest_file"
echo "Disabled the ${file_type/s/} \"$file_to_disable\""
fi
done
IFS="$old_ifs"
# sleep 1.5
# _list_plugins_and_themes
}
_list_plugins_and_themes ()
{
echo -e "\n${UWhite}bash-config Plugins and Themes${Color_Off}\n"
echo "Plugins"
plugins_enabled="$(find ${bash_config_plugins_folder}/enabled -name '*.plugin.bash' 2> /dev/null | sed 's/.*\///')"
for type in $(find ${bash_config_plugins_folder} -type f -name '*.plugin.bash' | sort) ; do
description=$(grep '## about' $type | awk -F":" '{print $2}')
type=$(echo $type | sed 's/.*\///')
if [[ $(echo "$plugins_enabled" | grep -E -q "^${type}" ; echo $?) -eq 0 ]] ; then
printf '%-30s %s\n' "[x] ${type%%.plugin.bash}" "$description"
else
printf '%-30s %s\n' "[ ] ${type%%.plugin.bash}" "$description"
fi
unset type description
done
echo -e "\nThemes"
theme_enabled="$(find ${bash_config_themes_folder}/enabled -maxdepth 1 -name '*.theme.bash' 2> /dev/null | sed 's/.*\///')"
for type in $(find ${bash_config_themes_folder} -maxdepth 1 -type f -name '*.theme.bash' | sort) ; do
description=$(grep '## about' $type | awk -F":" '{print $2}')
type="$(echo $type | sed 's/.*\///')"
if [[ $(echo "$theme_enabled" | grep -E -q "^$type" ; echo $?) -eq 0 ]] ; then
printf '%-30s %s\n' "[x] ${type%%.theme.bash}" "$description"
else
printf '%-30s %s\n' "[ ] ${type%%.theme.bash}" "$description"
fi
unset type description
done
echo
}
_install_bash_config ()
{
clear
echo -e 'Warning!! Running the install will overwrite your config with files from the current dir. \nIf this is an upgrade, run "bash-config upgrade".\n'
read -p "Hit \"Enter\" to continue or \"Ctrl+c\" to quit: "
if [[ ! -d lib && ( ! -d plugins && ! -d themes) && ( ! -f bash-config.sh && ! -f bash-config.conf ) ]] ; then
echo "Please run this script from the downloaded directory."
exit 1
fi
echo -e "Creating bash-config dir... \c"
mkdir -p "$bash_config_dir" && echo "ok" || \
{ echo "failed" ; exit 1 ; }
echo -e "Copying files... \c"
cp -a lib plugins themes bash-config.conf "${bash_config_dir}/." && echo ok || \
{ echo "failed" ; exit 1 ; }
echo -e "Creating the plugins folder... \c"
mkdir -p "${bash_config_dir}/plugins/enabled" && echo ok || \
{ echo "failed" ; exit 1 ; }
echo -e "Creating the themes folder... \c"
mkdir -p "${bash_config_dir}/themes/enabled" && echo ok || \
{ echo "failed" ; exit 1 ; }
echo -e "Enabling config file... \c"
grep -q '. ${HOME}/.bash-config/bash-config.conf' "${HOME}/.bashrc"
if [[ $? -eq 0 ]] ; then
echo "already installed"
else
echo -e '\n# Load bash-config\n. ${HOME}/.bash-config/bash-config.conf' >> \
"${HOME}/.bashrc" && echo "ok" || { echo "failed" ; exit 1 ; }
fi
echo -e "Copying bash-config.sh to ${HOME}/bin... \c"
mkdir -p "${HOME}/bin"
cp bash-config.sh "${HOME}/bin/." && echo "ok" || { echo "failed" ; exit 1 ; }
}
_upgrade ()
{
local file_type
if [[ ! -d lib && ( ! -d plugins && ! -d themes) && ( ! -f bash-config.sh && ! -f bash-config.conf ) ]] ; then
echo "Please run this script from the downloaded directory."
exit 1
elif [ ! -d "$bash_config_dir" ] ; then
echo "I can't find a bash-config directory. If you are trying to install, use the \"install\" option"
exit 1
fi
mkdir -p /tmp/bash-plugins
echo -e "Backing up enabled plugins... \c"
find "${bash_config_plugins_folder}/enabled" -type l | xargs ls -l | awk '{print $9 "\t" $11}' > \
/tmp/bash-plugins/enabled.plugins && echo "ok" || { echo "failed" ; exit 1 ; }
echo -e "Backing up enabled themes... \c"
find "${bash_config_themes_folder}/enabled" -type l | xargs ls -l | awk '{print $9 "\t" $11}' > \
/tmp/bash-plugins/enabled.themes && echo "ok" || { echo "failed" ; exit 1 ; }
echo -e "Copying files... \c"
cp -a lib plugins themes "${bash_config_dir}/." && echo ok || \
{ echo "failed" ; exit 1 ; }
echo -e "Checking config file... \c"
if ! command diff "${bash_config_dir}/bash-config.conf" bash-config.conf &> /dev/null ; then
cp bash-config.conf "${bash_config_dir}/bash-config.conf.new"
echo "A new config file was copied to ${bash_config_dir}/bash-config.conf.new"
else
echo "ok"
fi
echo -e "Copying new bash-config.sh to ${HOME}/bin... \c"
mkdir -p "${HOME}/bin"
cp bash-config.sh "${HOME}/bin/." && echo "ok" || { echo "failed" ; exit 1 ; }
echo -e "\n**Restoring plugins"
cd "${bash_config_plugins_folder}/enabled"
cat /tmp/bash-plugins/enabled.plugins | while read line ; do
plugin_name=$(echo $line | awk '{print $1}' | sed 's/.*\///')
# plugin_name=${plugin_name%%.plugin.bash}
ln -sf "$(echo $line | awk '{print $2}')" "$(echo $line | awk '{print $1}')" \
&& echo "Enabled ${plugin_name%%.plugin.bash}" \
|| echo "Could not enable ${plugin_name%%.plugin.bash}"
done
# Removing default theme
cd "${bash_config_themes_folder}/enabled"
for file in $(find . -type l) ; do
unlink $file
done
# Restoring
echo -e "\n**Restoring themes"
cat /tmp/bash-plugins/enabled.themes | while read line ; do
plugin_name=$(echo $line | awk '{print $1}')
ln -sf "$(echo $line | awk '{print $2}')" "$(echo $line | awk '{print $1}')" \
&& echo "Enabled ${plugin_name%%.theme.bash}" \
|| echo "Could not enable ${plugin_name%%.theme.bash}"
done
}
_display_aliases ()
{
if [[ $# -eq 1 ]] ; then
# Display all aliases for a plugin
if [ -e ${bash_config_plugins_folder}/enabled/${1}.plugin.bash ] ; then
#echo "Aliases for plugin ${1}"
echo -e "\n${UWhite}Aliases for plugin ${1}${Color_Off}"
grep -h '# help:' ${bash_config_plugins_folder}/enabled/${1}.plugin.bash \
| awk -F":" '{printf "%-15s %s\n" , $2 , substr($0, index($0,$3))}'
else
# Search for aliases on all plugins
echo -e "\n${UWhite}Searching for aliases with \"$1\" on all plugins${Color_Off}"
grep -h '# help:' ${bash_config_plugins_folder}/enabled/*.plugin.bash 2> /dev/null \
| grep -h "help:${1}" | awk -F":" '{printf "%-15s %s\n" , $2 , substr($0, index($0,$3))}'
fi
elif [[ $# -eq 2 ]] ; then
# Search for aliases in a plugin
echo -e "\n${UWhite}Searching for aliases with \"$2\" on plugin \"$1\"${Color_Off}"
if [ -e ${bash_config_plugins_folder}/enabled/${1}.plugin.bash ] ; then
grep -h '# help:' ${bash_config_plugins_folder}/enabled/${1}.plugin.bash \
| grep "help:${2}" | awk -F":" '{printf "%-15s %s\n" , $2 , substr($0, index($0,$3))}'
else
echo "Could not find alias \"$2\" on plugin \"$1\""
exit 1
fi
else
# Shows all aliases
echo -e "\n${UWhite}Displaying all aliases${Color_Off}"
grep -h '# help:' ${bash_config_plugins_folder}/enabled/*.plugin.bash 2> /dev/null \
| awk -F":" '{printf "%-15s %s\n" , $2 , substr($0, index($0,$3))}'
fi
}
#-------------------------------------------------------------------------------
# Starts script
#-------------------------------------------------------------------------------
if [[ $# -eq 0 ]] ; then
echo -e "\n$usage\n"
elif [[ $# -eq 1 ]] ; then
case "$1" in
list|plugins)
_list_plugins_and_themes
;;
install)
_install_bash_config
;;
upgrade)
_upgrade
;;
aliases) _display_aliases ;;
-h)
echo -e "$usage"
;;
*)
echo "Unknow option"
echo -e "$usage"
exit 1
;;
esac
elif [[ $# -gt 1 ]] ; then
case "$1" in
enable) _enable "$2" "$3" ;;
disable) _disable "$2" "$3" ;;
aliases) shift ; _display_aliases $* ;;
*)
echo "Wrong option"
echo -e "$usage"
exit 0
;;
esac
else
echo "Wrong parameter usage"
echo -e "$usage"
exit 1
fi