mass mod install.sh

This commit is contained in:
Dict Xiong 2022-11-03 22:45:33 +08:00
parent aa448efb6d
commit d3766b883e

View File

@ -3,87 +3,111 @@
THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) THIS_DIR=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd )
source "$THIS_DIR/tools/common.sh" source "$THIS_DIR/tools/common.sh"
home_slashes=${HOME//\//\\\/} if [[ ! "$DOTFILES" == "${HOME}"* ]]; then
if [[ ! $DOTFILES == ${home_slashes}* ]]; then
fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..." fmt_fatal "\"$DOTFILES\" is not under \"$HOME\". aborting ..."
fi fi
dotfile_home_path=${DOTFILES/${home_slashes}/\~} DOTFILE_TILDE=${DOTFILES/"$HOME"/\~}
dotfile_relative_path=${DOTFILES#${home_slashes}\/}
crontab_job="0 * * * * ${DOTFILES}/update.sh"
insert_if_not_exist() CRON_JOB="0 * * * * ${DOTFILES}/update.sh"
declare -a HOME_FILES_PATH
declare -a HOME_FILES_CONTENT
HOME_FILES_PATH[0]=".zshrc"
HOME_FILES_CONTENT[0]="source ${DOTFILE_TILDE}/.zshrc2"
HOME_FILES_PATH[1]=".tmux.conf"
HOME_FILES_CONTENT[1]="source-file ${DOTFILE_TILDE}/.tmux.conf2"
HOME_FILES_PATH[2]=".vimrc"
HOME_FILES_CONTENT[2]="source ${DOTFILE_TILDE}/.vimrc2"
HOME_FILES_PATH[3]=".gitconfig"
HOME_FILES_CONTENT[3]="[include] path = \"${DOTFILE_TILDE}/.gitconfig2\""
declare -a HOME_SYMLINKS_SRC
declare -a HOME_SYMLINKS_DST
HOME_SYMLINKS_SRC[0]=".ssh/authorized_keys2"
HOME_SYMLINKS_DST[0]=".ssh/authorized_keys2"
install_file_content()
{ {
local filename=$1 for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
local line=$2 local filename="$HOME/${HOME_FILES_PATH[$i]}"
fmt_note "installing \"$line\" into \"$filename\" ..." local content=${HOME_FILES_CONTENT[$i]}
fmt_note "installing \"$content\" into \"$filename\" ..."
mkdir -p $(dirname "$filename") mkdir -p $(dirname "$filename")
if [ ! -f "$filename" ]; then if [ ! -f "$filename" ]; then
touch $filename touch $filename
fi fi
grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename" grep -qxF -- "$content" "$filename" || echo "$content" >> "$filename"
done
} }
delete_if_exist() uninstall_file_content()
{ {
local filename=$1 for ((i=0; i<${#HOME_FILES_PATH[@]}; i++)); do
local line=$2 local filename="$HOME/${HOME_FILES_PATH[$i]}"
fmt_note "removing \"$line\" from \"$filename\" ..." local content=${HOME_FILES_CONTENT[$i]}
fmt_note "removing \"$content\" from \"$filename\" ..."
if [ -f "$filename" ]; then if [ -f "$filename" ]; then
grep -vxF -- "$line" "$filename" | tee "$filename" grep -vxF -- "$content" "$filename" | tee "$filename"
fi fi
done
} }
create_symlink() install_symlink()
{ {
local src=$1 for ((i=0; i<${#HOME_SYMLINKS_SRC[@]}; i++)); do
local dest=$2 local src="$DOTFILES/${HOME_SYMLINKS_SRC[$i]}"
fmt_note "creating symlink \"$dest\" --> \"$src\" ..." local dst="$HOME/${HOME_SYMLINKS_DST[$i]}"
fmt_note "creating symlink \"$dst\" --> \"$src\" ..."
if [ ! -f "$src" ]; then if [ ! -f "$src" ]; then
fmt_error "\"$src\" does not exist! aborting this job ..." fmt_error "\"$src\" does not exist! aborting this job ..."
return 1 continue
fi fi
mkdir -p $(dirname "$dest") mkdir -p $(dirname "$dst")
if [ -f "$dest" ]; then if [ -f "$dst" ]; then
if [ "$(readlink $dest)" -ef "$src" ]; then if [ "$(readlink $dst)" -ef "$src" ]; then
return 0 continue
fi fi
fmt_warning "\"$dest\" already exists! stat output:" fmt_warning "\"$dst\" already exists! stat output:"
echo ---------- echo ----------
stat $dest stat $dst
echo ---------- echo ----------
ask_for_yN "would you like to replace ${dest}?" ask_for_yN "would you like to replace ${dst}?"
if [ $? -eq 1 ]; then if [ $? -eq 1 ]; then
rm $dest rm $dst
else else
fmt_error "\"$dest\" already exists! aborting this job ..." fmt_error "aborting this job ..."
return 1 continue
fi fi
fi fi
ln -s $src $dest ln -s $src $dst
return $? done
} }
delete_link_if_match() uninstall_symlink()
{ {
local src=$1 local src
local dest=$2 for src in "${!HOME_SYMLINKS[@]}"; do
if [ "$(readlink $dest)" -ef "$src" ]; then local dst=${HOME_SYMLINKS[$src]}
fmt_note "removing symlink \"$dest\" ..." src="$DOTFILES/$src"
dst="$HOME/$dst"
if [ "$(readlink $dst)" -ef "$src" ]; then
fmt_note "removing symlink \"$dst\" ..."
echo ---------- echo ----------
stat $dest stat $dst
echo ---------- echo ----------
rm $dest rm $dst
fi fi
done
} }
install_crontab(){ install_crontab(){
fmt_note "installing \"$crontab_job\" to crontab ..." fmt_note "installing \"$CRON_JOB\" to crontab ..."
( crontab -l | grep -vxF "${crontab_job}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab - ( crontab -l | grep -vxF "${CRON_JOB}" | grep -v "no crontab for"; echo "$CRON_JOB" ) | crontab -
} }
uninstall_crontab(){ uninstall_crontab(){
fmt_note "removing \"$crontab_job\" from crontab ..." fmt_note "removing \"$CRON_JOB\" from crontab ..."
( crontab -l | grep -vxF "$crontab_job" ) | crontab - ( crontab -l | grep -vxF "$CRON_JOB" ) | crontab -
} }
install_tmux_tpm(){ install_tmux_tpm(){
@ -132,11 +156,8 @@ uninstall_update(){
install(){ install(){
install_update install_update
install_crontab install_crontab
insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" install_file_content
insert_if_not_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2" install_symlink
insert_if_not_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
insert_if_not_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
create_symlink "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
# those that won't be uninstalled in the future # those that won't be uninstalled in the future
install_tmux_tpm install_tmux_tpm
install_vim_vundle install_vim_vundle
@ -145,16 +166,15 @@ install(){
uninstall(){ uninstall(){
ask_for_yN "do you really want to uninstall?" ask_for_yN "do you really want to uninstall?"
if [[ $? == 1 ]]; then if [[ $? != 1 ]]; then
fmt_error "aborting this job ..."
return
fi
uninstall_update uninstall_update
uninstall_crontab uninstall_crontab
delete_if_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" uninstall_file_content
delete_if_exist "${HOME}/.tmux.conf" "source-file ${dotfile_home_path}/.tmux.conf2" uninstall_symlink
delete_if_exist "${HOME}/.vimrc" "source ${dotfile_home_path}/.vimrc2"
delete_if_exist "${HOME}/.gitconfig" "[include] path = \"${dotfile_home_path}/.gitconfig2\""
delete_link_if_match "${DOTFILES}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2"
fmt_note "done uninstalling!" fmt_note "done uninstalling!"
fi
} }