-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_consistency.sh
executable file
·295 lines (262 loc) · 8.65 KB
/
check_consistency.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
#!/usr/bin/env bash
DIR=$(dirname $(realpath $0))
verbose=false
BOLD="$(printf '\033[1m')"; TAIL="$(printf '\033[0m')"; WHITE="$(printf '\033[37m')"
RED="$(printf '\033[31m')"; GREEN="$(printf '\033[32m')"; YELLOW="$(printf '\033[33m')"; CYAN="$(printf '\033[36m')"
function mesg() { echo -e "${WHITE}$1${TAIL}" ; }
function info() { if [[ $verbose == true ]]; then mesg "${CYAN}$1"; fi ; }
function bold() { mesg "${BOLD}$1" ; }
function success() { bold "${GREEN}$1 ✔" ; }
function warning() { bold "${YELLOW}$1" ; }
function error() { bold "${RED}$1" 1>&2 ; }
declare -a file_list=(
zshrc
vimrc
)
declare -a extra_file_list=(
coc_settings
ptpython_config
lazygit_config
ideavimrc
global_gitconfig
shellcheckrc
kitty_config
vifmrc
tealdeer_config
condarc
yazi_config
yazi_keymap
yazi_theme
)
# shellcheck disable=SC2034
function declare_dirs() {
zshrc_remote=$DIR/.zshrc
zshrc_local=$HOME/.zshrc
vimrc_remote=$DIR/.vimrc
vimrc_local=$HOME/.vimrc
coc_settings_remote=$DIR/.config/nvim/coc-settings.json
coc_settings_local=$HOME/.config/nvim/coc-settings.json
ptpython_config_remote=$DIR/.config/ptpython/config.py
ptpython_config_local=$HOME/.config/ptpython/config.py
lazygit_config_remote=$DIR/.config/lazygit/config.yml
lazygit_config_local=$HOME/.config/lazygit/config.yml
ideavimrc_remote=$DIR/dot_files/.ideavimrc
ideavimrc_local=$HOME/.ideavimrc
global_gitconfig_remote=$DIR/dot_files/.gitconfig
global_gitconfig_local=$HOME/.gitconfig
shellcheckrc_remote=$DIR/dot_files/.shellcheckrc
shellcheckrc_local=$HOME/.shellcheckrc
kitty_config_remote=$DIR/.config/kitty/kitty.conf
kitty_config_local=$HOME/.config/kitty/kitty.conf
vifmrc_remote=$DIR/.config/vifm/vifmrc
vifmrc_local=$HOME/.config/vifm/vifmrc
tealdeer_config_remote=$DIR/.config/tealdeer/config.toml
tealdeer_config_local=$HOME/.config/tealdeer/config.toml
condarc_remote=$DIR/dot_files/.condarc
condarc_local=$HOME/.condarc
yazi_config_remote=$DIR/.config/yazi/yazi.toml
yazi_config_local=$HOME/.config/yazi/yazi.toml
yazi_keymap_remote=$DIR/.config/yazi/keymap.toml
yazi_keymap_local=$HOME/.config/yazi/keymap.toml
yazi_theme_remote=$DIR/.config/yazi/theme.toml
yazi_theme_local=$HOME/.config/yazi/theme.toml
case $SYSTEM in
Darwin)
ptpython_config_local="$HOME/Library/Application Support/ptpython/config.py"
lazygit_config_local="$HOME/Library/Application Support/lazygit/config.yml"
;;
Android)
shortcut_sshd_remote=$DIR/android/.shortcuts/sshd
shortcut_sshd_local=$HOME/.shortcuts/sshd
;;
WSL*)
wsl_conf_remote=$DIR/windows/wsl/wsl.conf
wsl_conf_local=/etc/wsl.conf
;;
Codespace)
global_gitconfig_remote=$DIR/misc/codespace/.gitconfig
;;
esac
local -a exclude_file_list
local -a addon_file_list
case $SYSTEM in
Android)
exclude_file_list+=( ideavimrc kitty_config condarc );
addon_file_list+=( shortcut_sshd )
;;
WSL*)
exclude_file_list+=( ideavimrc )
addon_file_list+=( wsl_conf )
;;
Codespace)
exclude_file_list+=( ideavimrc kitty_config )
;;
esac
if [[ ${#exclude_file_list[@]} != 0 ]]; then
for exclude_file in ${exclude_file_list[@]}; do
file_list=( ${file_list[@]/$exclude_file} )
done
info "Skipped ${exclude_file_list[*]} on current system: ${GREEN}$SYSTEM ✔"
fi
if [[ ${#addon_file_list[@]} != 0 ]]; then
for addon_file in ${addon_file_list[@]}; do
file_list+=( $addon_file )
done
info "Added ${addon_file_list[*]} on current system: ${GREEN}$SYSTEM ✔"
fi
}
function usage() {
bold "Usage:"
mesg " check_consistency.sh [options]"
mesg
bold "Options:"
mesg " -a, --all Check all files"
mesg " -h, --help Display help"
mesg " -v, --verbose Show detailed information"
}
function cmd_parser
{
while [ "$#" -gt 0 ]; do
case "$1" in
a|-a|--all) file_list+=( ${extra_file_list[@]} ) ;;
h|-h|--help) usage; exit 0 ;;
v|-v|--verbose) verbose=true ;;
*) error "Error: Invalid option '$1'"; usage; exit 1 ;;
esac
shift
done
}
function detect_system() {
local _uname_a=$(uname -a)
if [[ $_uname_a =~ Microsoft ]]; then SYSTEM="WSL1"
elif [[ $_uname_a =~ WSL2 ]]; then SYSTEM="WSL2"
elif [[ -n $CODESPACE_NAME ]]; then SYSTEM="Codespace"
elif [[ $OSTYPE =~ ^darwin ]]; then SYSTEM="Darwin"
elif [[ $OSTYPE =~ android ]]; then SYSTEM="Android"
elif [[ $OSTYPE =~ ^linux ]]; then SYSTEM="Linux"
else SYSTEM="Unknown"
fi
}
function has_command() { command -v "$1" > /dev/null ; }
function has_dir() { [[ -d "$1" ]] ; }
function has_file() { [[ -f "$1" ]] ; }
function file_same() { diff "$1" "$2" > /dev/null ; }
function ensure_dir() { has_dir "$1" || mkdir -p "$1" ; }
function owned_by_root() { [[ $(stat -c %U "$1") == "root" ]] ; }
function check_editor
{
if has_command nvim; then
diff_command="nvim -i NONE -d"
elif has_command vim; then
diff_command="vimdiff"
else
error "Error: vim or nvim not found."
exit 1
fi
}
function check_all_files
{
for file in ${file_list[@]}; do
local file_remote=$(eval echo \$${file}_remote)
local file_local=$(eval echo \$${file}_local)
if ! has_file "$file_local"; then
return 1
fi
if ! file_same "$file_remote" "$file_local"; then
return 1
fi
done
return
}
function confirm() {
local user_input
read -N1 -p "$1 [Y/n] " user_input </dev/tty
[[ "$user_input" == $'\n' ]] && user_input=y || echo
[[ "$user_input" =~ [yY] ]] && return 0 || return 1
}
function copy_file() {
local src=$1
local dest=$2
local dest_dir=$(dirname "$dest")
ensure_dir $dest_dir
if owned_by_root $dest_dir; then
if ! sudo cp "$src" "$dest"; then
return 1
fi
else
if ! cp "$src" "$dest"; then
return 1
fi
fi
return
}
function run_edit
{
if check_all_files; then
success "All files are the same. Nothing to do."
return
fi
for file in ${file_list[@]}; do
local file_remote=$(eval echo \$${file}_remote)
local file_local=$(eval echo \$${file}_local)
if ! has_file $file_local; then
if confirm "$file not found, create a copy to \`$file_local\` ?"; then
if ! copy_file "$file_remote" "$file_local"; then
error "Error: Failed to copy \`$file_remote\` to \`$file_local\`."
continue
fi
success "Copied \`$file_remote\` to \`$file_local\`."
fi
continue
fi
if file_same "$file_remote" "$file_local"; then
[[ $verbose == true ]] && success "$file has already been synchronized."
else
if confirm "$file unsynchronized. Edit with $diff_command ?"; then
if owned_by_root $file_local; then
diff_command="sudo -E env PATH=$PATH $diff_command"
fi
$diff_command "$file_remote" "$file_local"
if file_same "$file_remote" "$file_local"; then
success "$file is now synchronized."
else
info "$file is still unsynchronized."
info "-- Use \`$diff_command \"$file_remote\" \"$file_local\"\` later,"
info "-- or try to rerun this script."
fi
fi
fi
done
if check_all_files; then
success "All files are same now."
fi
}
function link_init_nvim() {
local init_nvim=$HOME/.config/nvim/init.vim
local vimrc=$HOME/.vimrc
info "Linking init.vim ..."
if ! has_file $vimrc; then
info "$vimrc not found, please try to rerun this script."
return 1
fi
if has_file $init_nvim; then
if [[ $(realpath $init_nvim) == $(realpath $vimrc) ]]; then
info "Neovim init.nvim has already linked."
return
else
warning "Warning: $init_nvim exists, but not linked to $vimrc."
return 1
fi
else
ensure_dir $HOME/.config/nvim
ln -s $vimrc $init_nvim
success "Linked $init_nvim to $vimrc."
fi
}
# main
cmd_parser "$@"
detect_system
declare_dirs
check_editor
run_edit
link_init_nvim