-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanager
executable file
·266 lines (250 loc) · 6.89 KB
/
manager
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
#!/bin/bash
# manager - copy files to their proper locations
shopt -s dotglob
BACKUP_DIR="$HOME/.config_backup/$(date +%Y%m%d%H%M%S)"
AVAILABLE_CONFIGS=(bash git i3 tmux xresources ranger kitty fish dunst all lazygit picom)
declare -A COPY_OPTIONS=(
# bash
["bash_.bash_profile"]="$HOME/.bash_profile"
["bash_.bashrc"]="$HOME/.bashrc"
["bash_.bash_aliases"]="$HOME/.bash_aliases"
["bash_.bash_functions"]="$HOME/.bash_functions"
["bash_.bash_env"]="$HOME/.bash_env"
# git
["git_.gitconfig"]="$HOME/.gitconfig"
# i3
["i3_config"]="$HOME/.config/i3/config"
["i3_i3status"]="$HOME/.config/i3status"
# tmux
["tmux_.tmux.conf"]="$HOME/.tmux.conf"
# xresources
["xresources_.Xresources"]="$HOME/.Xresources"
# ranger
["ranger_commands_full.py"]="$HOME/.config/ranger/commands_full.py"
["ranger_scope.sh"]="$HOME/.config/ranger/scope.sh"
["ranger_commands.py"]="$HOME/.config/ranger/commands.py"
["ranger_rifle.conf"]="$HOME/.config/ranger/rifle.conf"
["ranger_rc.conf"]="$HOME/.config/ranger/rc.conf"
# kitty
["kitty_kitty.conf"]="$HOME/.config/kitty/kitty.conf"
["kitty_kitty-themes"]="$HOME/.config/kitty/kitty-themes"
["kitty_theme.conf"]="$HOME/.config/kitty/theme.conf"
# fish
["fish_conf.d"]="$HOME/.config/fish/conf.d"
["fish_functions"]="$HOME/.config/fish/functions"
["fish_fish_variables"]="$HOME/.config/fish/fish_variables"
["fish_completions"]="$HOME/.config/fish/completions"
["fish_config.fish"]="$HOME/.config/fish/config.fish"
# dunst
["dunst_dunstrc"]="$HOME/.config/dunst/dunstrc"
# lazygit
["lazygit_config.yml"]="$HOME/.config/lazygit/config.yml"
# picom
["picom_picom.conf"]="$HOME/.config/picom/picom.conf"
)
# Prints help
usage() {
help_text='USAGE: ./manager -h|[-d][-v] -c config|[-v]-i config
\n-h - help - print help
\n-d - dangerous - without backup of existing files
\n-c - config file name - install given configuration
\n-i - import - import your current configuration to this repository
\n-v - verbose
\nExit codes:
\n1 - cannot install configuration
\n2 - invalid config name
\n3 - connot create backup dir
\n4 - invalid options
\n5 - cannot import configuration
\n\nList of available configs:'
echo -e $help_text
for config in ${AVAILABLE_CONFIGS[@]}
do
echo -e "${config}:"
if [[ $config = 'all' ]]; then
echo -e '\tall configuration files'
else
for file in ${config}/*
do
file=$(basename $file)
key="${config}_${file}"
location=${COPY_OPTIONS[${key}]}
if [[ $location = "" ]]; then
echo -e "\t$file -> [Missing configuration]"
else
echo -e "\t$file -> $location"
fi
done
fi
done
}
# Makes backup of existing files
#
# params:
# 1 - config name
backup() {
echo 'Making backup...'
local config=$1
local backup_dir="${BACKUP_DIR}/${config}"
# create backup dir with timestamp
mkdir -p $verbose_option $backup_dir
if [[ $? != 0 ]]; then
echo 'Cannot create backup dir. Exiting...'
exit 2
fi
# iterate over all files in given config dir
for file in ${config}/*
do
file=$(basename $file)
local key="${config}_${file}"
local target_to_backup=${COPY_OPTIONS[${key}]}
if [[ -d $target_to_backup ]]; then # directory
cp -r $verbose_option $target_to_backup $backup_dir
elif [[ -f $target_to_backup ]]; then # file
cp $verbose_option $target_to_backup $backup_dir
else
echo "File $file not found"
fi
done
echo 'Done'
}
# Makes installation
#
# params:
# 1 - config name
install() {
echo 'Installing...'
local config=$1
# iterate over all files in given config dir
for file in ${config}/*
do
file=$(basename $file)
local key="${config}_${file}"
local target_to_replace=${COPY_OPTIONS[${key}]}
local replace_with="${config}/${file}"
if [[ -d $replace_with ]] && [[ $target_to_replace != "" ]]; then # directory
rm -r $target_to_replace
cp -r $verbose_option $replace_with $target_to_replace
elif [[ -f $replace_with ]] && [[ $target_to_replace != "" ]]; then # file
rm $target_to_replace
cp $verbose_option $replace_with $target_to_replace
else
echo "File $file not found"
fi
if [[ $? != 0 ]]; then
echo 'Cannot install configuration. Exiting...'
exit 1
fi
done
echo 'Done'
}
# Makes import
#
# params:
# 1 - config name
import() {
echo 'Importing...'
local config=$1
# iterate over all files in given config dir
for key in "${!COPY_OPTIONS[@]}"
do
if [[ $key =~ ^${config}_ ]]; then
local replace_with=${COPY_OPTIONS[${key}]}
local target_to_replace="${config}/${file}"
[[ ! -d $config ]] && mkdir -p $verbose_option $config
if [[ -d $replace_with ]]; then # directory
cp -r $verbose_option $replace_with $target_to_replace
elif [[ -f $replace_with ]]; then # file
cp $verbose_option $replace_with $target_to_replace
else
echo "File $file not found"
fi
if [[ $? != 0 ]]; then
echo 'Cannot import configuration. Exiting...'
exit 5
fi
fi
done
echo 'Done'
}
# defaults
option_provided=false
safe_install=true
install=false
verbose_option=""
import=false
# parse options
while getopts ':hdc:vi:' OPTION; do
case $OPTION in
h)
usage
exit
;;
d)
safe_install=false
;;
c)
install=true
config=$OPTARG
option_provided=true
;;
v)
verbose_option="-v"
;;
i)
option_provided=true
import=true
config=$OPTARG
;;
:)
echo 'Invalid option' >&2
;;
*)
echo 'Invalid option' >&2
;;
esac
done
# check if arguments were given
if [[ $# = 0 ]] || [[ $option_provided = false ]]; then
usage
exit 4
fi
if [[ $install = true ]] && [[ $import = true ]]; then
echo 'Choose import or install action'
exit 4
fi
# check if the correct config name was given
if [[ $install = true ]] || [[ $import = true ]]; then
# check if $config is in $AVAILABLE_CONFIGS
valid_config_name=false
for config_name in "${AVAILABLE_CONFIGS[@]}"
do
if [[ $config_name = "$config" ]]; then
valid_config_name=true
break
fi
done
# exit if $config is not in $AVAILABLE_CONFIGS
if [[ $valid_config_name = false ]]; then
echo 'Invalid config name'
exit 2
fi
fi
# perform task
if [[ $config = 'all' ]]; then
for config_name in "${AVAILABLE_CONFIGS[@]}"
do
# omit "all" from $AVAILABLE_CONFIGS
if [[ $config_name = 'all' ]]; then
continue
else
[[ $safe_install = true ]] && [[ $install = true ]] && backup $config_name
[[ $install = true ]] && install $config_name
[[ $import = true ]] && import $config_name
fi
done
else # perform task for only one configuration
[[ $safe_install = true ]] && [[ $install = true ]] && backup $config
[[ $install = true ]] && install $config
[[ $import = true ]] && import $config
fi