-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
uninstall.bash
executable file
·228 lines (185 loc) · 7.04 KB
/
uninstall.bash
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
#!/usr/bin/env bash
# shellcheck disable=SC1117,SC1090
# Script: uninstall.bash
# Purpose: Uninstall HomeSetup
# Created: Dec 21, 2018
# Author: <B>H</B>ugo <B>S</B>aporetti <B>J</B>unior
# Mailto: homesetup@gmail.com
# Site: https://github.com/yorevs/homesetup
# License: Please refer to <https://opensource.org/licenses/MIT>
#
# Copyright (c) 2024, HomeSetup team
# This script name.
APP_NAME="${0##*/}"
# Help message to be displayed by the script.
USAGE="
usage: $APP_NAME
"
# Define the USER and HOME
USER="${SUDO_USER:-${USER}}"
[[ -z "${SUDO_USER}" ]] && HOME=${HOME:-~/}
[[ -n "${SUDO_USER}" ]] && HOME=$(getent passwd "${SUDO_USER}" | cut -d: -f6)
SHELL="${SHELL:/bin/bash}"
# Import pre-defined Bash Colors.
[[ -f "${HOME}/.bash_colors" ]] && source "${HOME}/.bash_colors"
UNSETS=(
'quit' 'usage' 'uninstall_dotfiles' 'uninstall_dotfiles'
)
# HomeSetup installation prefix file
HHS_PREFIX_FILE="${HOME}/.hhs-prefix"
# Define the user HomeSetup installation prefix
HHS_PREFIX="$([[ -f "${HHS_PREFIX_FILE}" ]] && grep . "${HHS_PREFIX_FILE}")"
# Define the HomeSetup directory
HHS_HOME=${HHS_PREFIX:-${HOME}/HomeSetup}
# Define the HomeSetup files (.hhs) location
HHS_DIR="${HOME}/.hhs"
# HSPyLib python modules to uninstall
PYTHON_MODULES=('hspylib' 'hspylib-clitt' 'hspylib-setman' 'hspylib-vault' 'hspylib-firebase')
# Shell type
SHELL_TYPE="${SHELL##*/}"
# Dotfiles source location
DOTFILES_DIR="${HHS_HOME}/dotfiles/${SHELL_TYPE}"
# Backup prefix
ORIG_PREFIX="orig"
# Flag indicating HHS_DIR removal
REMOVE_HHS_DIR=
# Flag indicating HSPyLib removal
REMOVE_HSPYLIB=
# Flag indicating HomeSetup project removal
REMOVE_PRJ_DIR=
# .dotfiles we will handle
ALL_DOTFILES=()
# Uninstallation type
UNINSTALL_TYPE='normal'
# Find all dotfiles used by HomeSetup according to the current shell type.
IFS=''
while read -r dotfile; do
ALL_DOTFILES+=("${dotfile}")
done < <(find "${DOTFILES_DIR}" -maxdepth 1 -name "*.${SHELL_TYPE}" -exec basename {} \;)
IFS="${OLDIFS}"
# Purpose: Quit the program and exhibits an exit message if specified
# @param $1 [Req] : The exit return code. 0 = SUCCESS, 1 = FAILURE, * = ERROR ${RED}.
# @param $2 [Opt] : The exit message to be displayed.
quit() {
# Unset all declared functions
unset -f "${UNSETS[*]}"
exit_code=${1:-0}
shift
[[ ${exit_code} -ne 0 && ${#} -ge 1 ]] && echo -en "${RED}${APP_NAME}: " 1>&2
[[ ${#} -ge 1 ]] && echo -e "${*} ${NC}" 1>&2
[[ ${#} -gt 0 ]] && echo ''
exit "${exit_code}"
}
# Usage message
# @param $1 [Req] : The exit return code. 0 = SUCCESS, 1 = FAILURE
usage() {
quit "$1" "$USAGE"
}
check_installation() {
if [[ -n "${HHS_HOME}" && -d "${HHS_HOME}" ]]; then
echo -e "${ORANGE}"
[[ -z ${QUIET} ]] && read -rn 1 -p \
"Delete HomeSetup project (${HHS_HOME}) dir y/[n] ? " REMOVE_PRJ_DIR
[[ -n "${REMOVE_PRJ_DIR}" ]] && echo ''
[[ -z ${QUIET} ]] && read -rn 1 -p \
"Delete HomeSetup config (${HHS_DIR}) files y/[n] ? " REMOVE_HHS_DIR
[[ -n "${REMOVE_HHS_DIR}" ]] && echo ''
[[ -z ${QUIET} ]] && read -rn 1 -p \
"Uninstall HomeSetup python packages(${PYTHON_MODULES[*]}) y/[n] ? " REMOVE_HSPYLIB
[[ -n "${REMOVE_HSPYLIB}" ]] && echo ''
[[ "${REMOVE_PRJ_DIR}" =~ ^[yY]$ ]] && UNINSTALL_TYPE+=' +hhs-prj'
[[ "${REMOVE_HHS_DIR}" =~ ^[yY]$ ]] && UNINSTALL_TYPE+=' +hhs-dir'
[[ "${REMOVE_HSPYLIB}" =~ ^[yY]$ ]] && UNINSTALL_TYPE+=' +lib-dir'
echo -e "${NC}"
echo -e "${WHITE}### ${GREEN}HomeSetup© ${WHITE}Removal Settings ###${NC}"
echo -e "${BLUE}"
echo -e " Uninstall Type: ${UNINSTALL_TYPE}"
echo -e " Install Dir: ${HHS_HOME}"
echo -e " Install Prefix: ${HHS_PREFIX}"
echo -e " Dotfiles: ${ALL_DOTFILES[*]}"
echo -e "${NC}"
echo -e "${YELLOW}"
read -rn 1 -p "HomeSetup will be completely removed and all backups restored. Continue y/[n] ?" ANS
echo -e "${NC}"
[[ -n "$ANS" ]] && echo ''
if [[ "$ANS" =~ ^[yY]$ ]]; then
uninstall_dotfiles
else
quit 1 "Uninstallation cancelled !"
fi
else
quit 2 "Installation files were not found or removed !"
fi
}
# Remove dotfiles
uninstall_dotfiles() {
cd "${HOME}" || cd ..
# Remove installed HomeSetup dotfiles
echo -e "\n${BLUE}Removing installed HomeSetup dotfiles ...${NC}"
for next in ${ALL_DOTFILES[*]}; do
dotfile="${HOME}/.${next//\.${SHELL_TYPE}/}"
[[ -f "${dotfile}" ]] && \rm -fv "${dotfile:?}"
done
# Removing HomeSetup folders
[[ -n "${REMOVE_PRJ_DIR}" && -d "${HHS_HOME}" ]] && \rm -rfv "${HHS_HOME:?}"
[[ -L "${HHS_DIR}/bin" ]] && \rm -rfv "${HHS_DIR:?}/bin"
# Remove HomeSetup .hhs folder
[[ -n "${REMOVE_HHS_DIR}" ]] && echo ''
if [[ "${REMOVE_HHS_DIR}" =~ ^[yY]$ ]]; then
[[ -d "${HHS_DIR}" ]] && \rm -rfv "${HHS_DIR:?}" &> /dev/null
fi
# Restore original dotfiles
if [[ -d "${HHS_DIR}" ]]; then
echo -e "\n${BLUE}Restoring original dotfiles ...${NC}"
BACKUPS=("$(find "${HHS_DIR}" -iname "*.${ORIG_PREFIX}")")
for next in ${BACKUPS[*]}; do
[[ -f "${next}" ]] && \cp -v "${next}" "${HOME}/$(basename "${next%.*}")"
done
fi
# Uninstall HomeSetup python library
if [[ "${REMOVE_HSPYLIB}" =~ ^[yY]$ ]]; then
PIP=$(command -v pip3 2>/dev/null)
# HsPyLib-Vault
echo -e "\n${BLUE}Removing HomeSetup Vault${NC}"
${PIP} uninstall -y hspylib-vault &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup vault !\n${NC}"
# HsPyLib-Firebase
echo -e "\n${BLUE}Removing HomeSetup Firebase${NC}"
${PIP} uninstall -y hspylib-firebase &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup firebase !\n${NC}"
# HsPyLib-Clitt
echo -e "\n${BLUE}Removing HomeSetup Clitt${NC}"
${PIP} uninstall -y hspylib-clitt &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup clitt !\n${NC}"
# HsPyLib-Datasource
echo -e "\n${BLUE}Removing HomeSetup Datasource${NC}"
${PIP} uninstall -y hspylib-datasource &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup datasource !\n${NC}"
# HsPyLib-AskAi
echo -e "\n${BLUE}Removing HomeSetup HsPyLib-AskAi${NC}"
${PIP} uninstall -y hspylib-askai &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup HsPyLib-AskAi !\n${NC}"
# HsPyLib-Core
echo -e "\n${BLUE}Removing HomeSetup HsPyLib-Core${NC}"
${PIP} uninstall -y hspylib &> /dev/null \
|| echo -e "${RED}# Unable to uninstall HomeSetup HsPyLib-Core !\n${NC}"
fi
# Remove Starship. Locate and delete the binary
command -v 'starship' &>/dev/null && \rm -fv "$(command -v 'starship')"
# Restoring prompts
export PS1='\[\h:\W \u \$ '
export PS2="$PS1"
# Removing HomeSetup folder
[[ -d "${HHS_HOME}" ]] && quit 2 "Failed to uninstall HomeSetup !"
# Unset aliases and envs
echo "Unsetting aliases and variables ..."
unalias -a
unset "${!HHS_@}"
echo 'HomeSetup has been successfully uninstalled !'
echo ''
echo '* Your old PS1 (prompt) and aliases will be restored next time you open the terminal.'
echo "* Your temporary PS1 => '$PS1'"
echo ''
}
check_installation
echo '!!! HomeSetup will be gone after you open a new terminal !!!'