Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not overwrite backup bashrc #284

Merged
merged 2 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function bash_stats() {
}

function uninstall_oh_my_bash() {
env OSH=$OSH sh $OSH/tools/uninstall.sh
source "$OSH"/tools/uninstall.sh
}

function upgrade_oh_my_bash() {
Expand Down
5 changes: 3 additions & 2 deletions tools/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,9 @@ _omb_install_main() {

printf "${BLUE}Looking for an existing bash config...${NORMAL}\n"
if [[ -f ~/.bashrc || -h ~/.bashrc ]]; then
printf "${YELLOW}Found ~/.bashrc.${NORMAL} ${GREEN}Backing up to ~/.bashrc.pre-oh-my-bash${NORMAL}\n"
_omb_install_run mv ~/.bashrc ~/.bashrc.pre-oh-my-bash
local bashrc_backup=~/.bashrc.omb-backup-$(date +%Y%m%d%H%M%S)
printf "${YELLOW}Found ~/.bashrc.${NORMAL} ${GREEN}Backing up to $bashrc_backup${NORMAL}\n"
_omb_install_run mv ~/.bashrc "$bashrc_backup"
fi

printf "${BLUE}Using the Oh My Bash template file and adding it to ~/.bashrc${NORMAL}\n"
Expand Down
82 changes: 61 additions & 21 deletions tools/uninstall.sh
Original file line number Diff line number Diff line change
@@ -1,37 +1,77 @@
#!/usr/bin/env bash

read -r -p "Are you sure you want to remove Oh My Bash? [y/N] " confirmation
if [ "$confirmation" != y ] && [ "$confirmation" != Y ]; then
echo "Uninstall cancelled"
exit
_omb_uninstall_contains_omb() {
grep -qE '(source|\.)[[:space:]]+.*[/[:space:]]oh-my-bash\.sh' "$1" 2>/dev/null
}

# Find the latest bashrc that do not source oh-my-bash.sh
_omb_uninstall_find_bashrc_original() {
_omb_uninstall_bashrc_original=
printf '%s\n' "Looking for original bash config..."
IFS='
'
for _omb_uninstall_file in $(printf '%s\n' ~/.bashrc.omb-backup-?????????????? | sort -r) ~/.bashrc.pre-oh-my-bash; do
[ -f "$_omb_uninstall_file" ] || [ -h "$_omb_uninstall_file" ] || continue
_omb_uninstall_contains_omb "$_omb_uninstall_file" && continue
_omb_uninstall_bashrc_original=$_omb_uninstall_file
break
done
unset _omb_uninstall_file
IFS='
'
if [ -n "$_omb_uninstall_bashrc_original" ]; then
printf '%s\n' "-> Found at '$_omb_uninstall_bashrc_original'."
else
printf '%s\n' "-> Not found."
fi
}

read -r -p "Are you sure you want to remove Oh My Bash? [y/N] " _omb_uninstall_confirmation
if [ "$_omb_uninstall_confirmation" != y ] && [ "$_omb_uninstall_confirmation" != Y ]; then
printf '%s\n' "Uninstall cancelled"
unset _omb_uninstall_confirmation
return 0 2>/dev/null || exit 0
fi
unset _omb_uninstall_confirmation

echo "Removing ~/.oh-my-bash"
if [ -d ~/.oh-my-bash ]; then
printf '%s\n' "Removing ~/.oh-my-bash"
rm -rf ~/.oh-my-bash
fi

echo "Looking for original bash config..."
if [ -f ~/.bashrc.pre-oh-my-bash ] || [ -h ~/.bashrc.pre-oh-my-bash ]; then
echo "Found ~/.bashrc.pre-oh-my-bash -- Restoring to ~/.bashrc";
_omb_uninstall_bashrc_original=
_omb_uninstall_find_bashrc_original

if [ -f ~/.bashrc ] || [ -h ~/.bashrc ]; then
bashrc_SAVE=".bashrc.omb-uninstalled-$(date +%Y%m%d%H%M%S)";
echo "Found ~/.bashrc -- Renaming to ~/${bashrc_SAVE}";
mv ~/.bashrc ~/"${bashrc_SAVE}";
if ! _omb_uninstall_contains_omb ~/.bashrc; then
printf '%s\n' "uninstall: Oh-my-bash does not seem to be installed in .bashrc." >&2
if [ -n "$_omb_uninstall_bashrc_original" ]; then
printf '%s\n' "uninstall: The original config was found at '$_omb_uninstall_bashrc_original'." >&2
fi
printf '%s\n' "uninstall: Canceled." >&2
unset _omb_uninstall_bashrc_original
return 1 2>/dev/null || exit 1
fi

mv ~/.bashrc.pre-oh-my-bash ~/.bashrc;
exec bash; source ~/.bashrc
_omb_uninstall_bashrc_uninstalled=
if [ -e ~/.bashrc ] || [ -h ~/.bashrc ]; then
_omb_uninstall_bashrc_uninstalled=".bashrc.omb-uninstalled-$(date +%Y%m%d%H%M%S)";
printf '%s\n' "Found ~/.bashrc -- Renaming to ~/${_omb_uninstall_bashrc_uninstalled}";
mv ~/.bashrc ~/"${_omb_uninstall_bashrc_uninstalled}";
fi

echo "Your original bash config was restored. Please restart your session."
if [ -n "$_omb_uninstall_bashrc_original" ]; then
printf '%s\n' "Found $_omb_uninstall_bashrc_original -- Restoring to ~/.bashrc";
mv "$_omb_uninstall_bashrc_original" ~/.bashrc;
printf '%s\n' "Your original bash config was restored. Please restart your session."
else
if hash chsh >/dev/null 2>&1; then
echo "Switching back to bash"
chsh -s /bin/bash
else
echo "You can edit /etc/passwd to switch your default shell back to bash"
fi
sed '/oh-my-bash\.sh/s/^/: #/' ~/"${_omb_uninstall_bashrc_uninstalled:-.bashrc}" >| ~/.bashrc.omb-temp && \
mv ~/.bashrc.omb-temp ~/.bashrc
fi

unset _omb_uninstall_bashrc_original
unset _omb_uninstall_bashrc_uninstalled

echo "Thanks for trying out Oh My Bash. It has been uninstalled."
case $- in
*i*) exec bash; source ~/.bashrc ;;
esac