From c7f3a190914f921769b1898443366fa9c36a30b8 Mon Sep 17 00:00:00 2001 From: Dict Xiong Date: Sun, 15 May 2022 12:55:50 +0800 Subject: [PATCH] adding uninstall functions --- .zshrc2 | 9 ++++----- install.sh | 45 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/.zshrc2 b/.zshrc2 index 1102624..5b1a412 100644 --- a/.zshrc2 +++ b/.zshrc2 @@ -64,11 +64,10 @@ antigen apply export DOTFILES=$( cd "$( dirname "${BASH_SOURCE[0]:-${(%):-%x}}" )" && pwd ) dfs() { - if [[ $1 == update ]]; then - (cd "$DOTFILES" && env git pull) - else - echo "unknown command \"$1\". available: update" - fi + case $1 in + update ) (cd "$DOTFILES" && env git pull) ;; + * ) echo "unknown command \"$1\". available: update" ;; + esac } # alias diff --git a/install.sh b/install.sh index 0ab7688..a7109da 100755 --- a/install.sh +++ b/install.sh @@ -121,6 +121,16 @@ insert_if_not_exist() grep -qxF -- "$line" "$filename" || echo "$line" >> "$filename" } +delete_if_exist() +{ + filename=$1 + line=$2 + fmt_note "removing \"$line\" from \"$filename\" ..." + if [ -f "$filename" ]; then + grep -vxF -- "$line" "$filename" >> "$filename" + fi +} + create_symlink() { src=$1 @@ -146,22 +156,47 @@ create_symlink() ln -s $src $dest } +delete_link_if_match() +{ + src=$1 + dest=$2 + if [ "$(readlink $dest)" -ef "$src" ]; then + fmt_note "removing symlink \"$dest\" ..." + echo ---------- + env stat $dest + echo ---------- + rm $dest + fi +} + + install_crontab(){ fmt_note "installing \"$crontab_job\" to crontab ..." - ( crontab -l | grep -v "${crontab_job//\*/\\\*}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab - + ( crontab -l | grep -vxF "${crontab_job}" | grep -v "no crontab for"; echo "$crontab_job" ) | crontab - } uninstall_crontab(){ - fmt_note uninstalling "\"$crontab_job\"" from crontab ... - ( crontab -l | grep -v"$crontab_job" ) | crontab - + fmt_note "removing \"$crontab_job\" from crontab ..." + ( crontab -l | grep -vxF "$crontab_job" ) | crontab - } install(){ install_crontab insert_if_not_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" create_symlink "${dotfile_path}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2" - fmt_note "job done!" + fmt_note "done installing!" +} + +uninstall(){ + uninstall_crontab + delete_if_exist "${HOME}/.zshrc" "source ${dotfile_home_path}/.zshrc2" + delete_link_if_match "${dotfile_path}/.ssh/authorized_keys2" "${HOME}/.ssh/authorized_keys2" + fmt_note "done uninstalling!" } setup_color -install +case $1 in + ^$|-i ) install ;; + -r ) uninstall ;; + * ) echo "unknown command \"$1\". available: -i, -r" ;; +esac