lazy/update.sh
Konano b93d9ab879
feat(zsh): load user config after zshrc
Load .zsh_user once before the first prompt and remove legacy direct source lines during updates. Disable Clash proxy environment variables in the default user configuration.
2026-09-08 12:03:26 +08:00

89 lines
2.9 KiB
Bash
Executable File

#!/usr/bin/env bash
scriptdir=$(dirname $(realpath $0))
# ============================================================
update_file() {
local A="$1"
local B="$2"
if [ -f "$A" ] && [ -f "$B" ] && [ "$(cat "$A")" != "$(cat "$B")" ]; then
cp "$A" "$B"
echo "File $B has been updated."
fi
}
update_file_sudo() {
local A="$1"
local B="$2"
if [ -f "$A" ] && [ -f "$B" ] && [ "$(cat "$A")" != "$(cat "$B")" ]; then
sudo cp "$A" "$B"
echo "File $B has been updated."
fi
}
update_authorized_keys() {
[ ! -f ~/.ssh/authorized_keys ] && return
local public_keys=$(cat $scriptdir/files/authorized_keys)
while IFS= read -r public_key; do
[[ $public_key == \#* ]] && continue
if ! grep -q "$public_key" ~/.ssh/authorized_keys; then
echo "$public_key" >> ~/.ssh/authorized_keys
echo "Added public key to authorized_keys: $public_key"
fi
done <<< "$public_keys"
}
remove_legacy_zsh_user_source() {
local zshrc="$HOME/.zshrc"
local init_file="$HOME/.oh-my-zsh/custom/00-init.zsh"
[ -f "$zshrc" ] || return
grep -q '^add-zsh-hook precmd __zsh_load_user_config$' "$init_file" 2>/dev/null || return
local temp_file
temp_file=$(mktemp "${zshrc}.XXXXXX") || return 1
awk '
{
line = $0
sub(/^[[:space:]]+/, "", line)
sub(/[[:space:]]+$/, "", line)
if (line != "source ~/.zsh_user" && line != ". ~/.zsh_user") print
}
' "$zshrc" > "$temp_file"
if cmp -s "$zshrc" "$temp_file"; then
rm "$temp_file"
return
fi
cp "$temp_file" "$zshrc"
rm "$temp_file"
echo "Legacy source ~/.zsh_user line has been removed from $zshrc."
}
# ============================================================
update_file $scriptdir/files/.nanorc ~/.nanorc
update_file $scriptdir/files/.vimrc ~/.vimrc
update_file $scriptdir/files/.tmux.conf ~/.tmux.conf
update_file $scriptdir/files/gpg.conf ~/.gnupg/gpg.conf
update_file $scriptdir/files/zsh/00-init.zsh ~/.oh-my-zsh/custom/00-init.zsh
update_file $scriptdir/files/zsh/10-theme.zsh ~/.oh-my-zsh/custom/10-theme.zsh
update_file $scriptdir/files/zsh/20-function.zsh ~/.oh-my-zsh/custom/20-function.zsh
update_file $scriptdir/files/zsh/50-env.zsh ~/.oh-my-zsh/custom/50-env.zsh
update_file $scriptdir/files/zsh/51-alias.zsh ~/.oh-my-zsh/custom/51-alias.zsh
update_file $scriptdir/files/zsh/80-proxy.zsh ~/.oh-my-zsh/custom/80-proxy.zsh
update_file $scriptdir/files/zsh/90-other.zsh ~/.oh-my-zsh/custom/90-other.zsh
update_file $scriptdir/files/zsh/ys-simple.zsh-theme ~/.oh-my-zsh/custom/themes/ys-simple.zsh-theme
remove_legacy_zsh_user_source
update_authorized_keys
# sudo_id=$(sudo id -u 2>/dev/null)
# if [ $? -eq 0 ] && [ "$sudo_id" -eq 0 ]; then
# update_file_sudo $scriptdir/files/proxychains4.conf /etc/proxychains4.conf
# fi