Skip to content

Commit 0c4aaa1

Browse files
AhmedYasserrrAhmedYasserrr
andauthored
Fix: handle legacy files in NO_SUDO mode (#133)
* Fix: handle legacy files in NO_SUDO mode * Fix: handle legacy files in NO_SUDO mode * Revert last two commits * Fixed a bug where which 'fw-fanctrl' failed with set -e, causing the script to abort. Disabled set -e temporarily for this check and re-enabled it after execution * Enhance uninstall and install functions to handle errors from pre-uninstall and post-install scripts, providing user guidance for permission issues. * Refactor removal logic to use a save remove function. --------- Co-authored-by: AhmedYasserrr <ahmdyasrj@gamil.com>
1 parent f04bae4 commit 0c4aaa1

1 file changed

Lines changed: 37 additions & 16 deletions

File tree

install.sh

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,31 +133,48 @@ function sanitizePath() {
133133

134134
function build() {
135135
echo "building package"
136-
rm -rf "dist/" 2> "/dev/null" || true
136+
remove_target "dist/"
137137
python -m build -s
138138
find . -type d -name "*.egg-info" -exec rm -rf {} + 2> "/dev/null" || true
139139
}
140140

141+
# safe remove function
142+
function remove_target() {
143+
local target="$1"
144+
if [ -e "$target" ] || [ -L "$target" ]; then
145+
if ! rm -rf "$target" 2> "/dev/null"; then
146+
echo "Failed to remove: $target"
147+
echo "Please run:"
148+
echo " sudo ./install.sh --remove"
149+
exit 1
150+
fi
151+
fi
152+
}
153+
141154
# remove remaining legacy files
142155
function uninstall_legacy() {
143156
echo "removing legacy files"
144-
rm "/usr/local/bin/fw-fanctrl" 2> "/dev/null" || true
145-
rm "/usr/local/bin/ectool" 2> "/dev/null" || true
146-
rm "/usr/local/bin/fanctrl.py" 2> "/dev/null" || true
147-
rm "/etc/systemd/system/fw-fanctrl.service" 2> "/dev/null" || true
148-
rm "$DEST_DIR$PREFIX_DIR/bin/fw-fanctrl" 2> "/dev/null" || true
157+
remove_target "/usr/local/bin/fw-fanctrl"
158+
remove_target "/usr/local/bin/ectool"
159+
remove_target "/usr/local/bin/fanctrl.py"
160+
remove_target "/etc/systemd/system/fw-fanctrl.service"
161+
remove_target "$DEST_DIR$PREFIX_DIR/bin/fw-fanctrl"
149162
}
150163

151164
function uninstall() {
152165
if [ "$SHOULD_PRE_UNINSTALL" = true ]; then
153-
./pre-uninstall.sh "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"
166+
if ! ./pre-uninstall.sh "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"; then
167+
echo "Failed to run ./pre-uninstall.sh. Run the script with root permissions,"
168+
echo "or skip this step by using the --no-pre-uninstall option."
169+
exit 1
170+
fi
154171
fi
155172
# remove program services based on the services present in the './services' folder
156173
echo "removing services"
157174
for SERVICE in $SERVICES ; do
158175
SERVICE=$(sanitizePath "$SERVICE")
159176
# be EXTRA CAREFUL about the validity of the paths (dont wanna delete something important, right?... O_O)
160-
rm -rf "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION"
177+
remove_target "$DEST_DIR$PREFIX_DIR/lib/systemd/system/$SERVICE$SERVICE_EXTENSION"
161178
done
162179

163180
# remove program services sub-configurations based on the sub-configurations present in the './services' folder
@@ -169,7 +186,7 @@ function uninstall() {
169186
for SUBCONFIG in $SUBCONFIGS ; do
170187
SUBCONFIG=$(sanitizePath "$SUBCONFIG")
171188
echo "removing '$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG'"
172-
rm -rf "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG" 2> "/dev/null" || true
189+
remove_target "$DEST_DIR$PREFIX_DIR/lib/systemd/$SERVICE/$SUBCONFIG"
173190
done
174191
done
175192

@@ -184,23 +201,23 @@ function uninstall() {
184201

185202
ectool autofanctrl 2> "/dev/null" || true # restore default fan manager
186203
if [ "$SHOULD_INSTALL_ECTOOL" = true ]; then
187-
rm "$DEST_DIR$PREFIX_DIR/bin/ectool" 2> "/dev/null" || true
204+
remove_target "$DEST_DIR$PREFIX_DIR/bin/ectool"
188205
fi
189-
rm -rf "$DEST_DIR$SYSCONF_DIR/fw-fanctrl" 2> "/dev/null" || true
190-
rm -rf "/run/fw-fanctrl" 2> "/dev/null" || true
206+
remove_target "$DEST_DIR$SYSCONF_DIR/fw-fanctrl"
207+
remove_target "/run/fw-fanctrl"
191208

192209
uninstall_legacy
193210
}
194211

195212
function install() {
196213
uninstall_legacy
197214

198-
rm -rf "$TEMP_FOLDER"
215+
remove_target "$TEMP_FOLDER"
199216
mkdir -p "$DEST_DIR$PREFIX_DIR/bin"
200217
if [ "$SHOULD_INSTALL_ECTOOL" = true ]; then
201218
mkdir "$TEMP_FOLDER"
202219
installEctool "$TEMP_FOLDER" || (echo "an error occurred when installing ectool." && echo "please check your internet connection or consider installing it manually and using --no-ectool on the installation script." && exit 1)
203-
rm -rf "$TEMP_FOLDER"
220+
remove_target "$TEMP_FOLDER"
204221
fi
205222
mkdir -p "$DEST_DIR$SYSCONF_DIR/fw-fanctrl"
206223

@@ -215,7 +232,7 @@ function install() {
215232
pipx install --global --force dist/*.tar.gz
216233
fi
217234
which 'fw-fanctrl' 2> "/dev/null" || true
218-
rm -rf "dist/" 2> "/dev/null" || true
235+
remove_target "dist/"
219236
fi
220237

221238
cp -pn "./src/fw_fanctrl/_resources/config.json" "$DEST_DIR$SYSCONF_DIR/fw-fanctrl" 2> "/dev/null" || true
@@ -263,7 +280,11 @@ function install() {
263280
done
264281
done
265282
if [ "$SHOULD_POST_INSTALL" = true ]; then
266-
./post-install.sh --dest-dir "$DEST_DIR" --sysconf-dir "$SYSCONF_DIR" "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"
283+
if ! ./post-install.sh --dest-dir "$DEST_DIR" --sysconf-dir "$SYSCONF_DIR" "$([ "$NO_SUDO" = true ] && echo "--no-sudo")"; then
284+
echo "Failed to run ./post-install.sh. Run the script with root permissions,"
285+
echo "or skip this step by using the --no-post-install option."
286+
exit 1
287+
fi
267288
fi
268289
}
269290

0 commit comments

Comments
 (0)