Fix/battery threshold setup (#136)

* refactor(battery-threshold): change setup to run script in terminal via sudo

* chore(battery-threshold): update dependencies

* docs(battery-threshold): update threshold.txt location description

* chore(battery-threshold): bump version
This commit is contained in:
Damian D'Souza
2026-07-28 21:12:15 -04:00
committed by GitHub
parent bd7d88dfee
commit 253668025d
4 changed files with 72 additions and 35 deletions
+54 -9
View File
@@ -8,22 +8,64 @@
# It creates a group 'battery_ctl' and adds the target user to this group.
#
# Usage:
# $ sudo ./setup_rules.sh # uses SUDO_USER (with sudo)
# $ ./setup_rules.sh username # use provided username (if ran as root)
# $ ./setup_rules.sh [username] [--non-interactive|-y]
# ------------------------------
set -e
# Check if running as root
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root (use sudo)"
exit 1
SCRIPT_PATH="$(readlink -f "$0" 2>/dev/null || echo "$0")"
TARGET_USER=""
SKIP_PROMPT=false
for arg in "$@"; do
case "$arg" in
-y | --non-interactive)
SKIP_PROMPT=true
;;
-*)
;;
*)
if [ -z "$TARGET_USER" ]; then
TARGET_USER="$arg"
fi
;;
esac
done
TARGET_USER="${TARGET_USER:-${SUDO_USER:-$USER}}"
if [ "$SKIP_PROMPT" = false ]; then
echo "===================================================="
echo " Battery Threshold Udev Setup"
echo "===================================================="
echo "Script location: $SCRIPT_PATH"
echo "Target user: $TARGET_USER"
echo ""
echo "Please examine the script location and contents above before proceeding."
echo ""
read -rp "Do you want to proceed with setup? (y/N): " CONFIRM
case "$CONFIRM" in
[yY][eE][sS] | [yY])
;;
*)
echo "Setup cancelled by user."
read -rp "Press Enter to exit..."
exit 1
;;
esac
fi
# Determine target user
TARGET_USER=${SUDO_USER:-$1}
# Escalate privileges via sudo only after user confirmation
if [ "$EUID" -ne 0 ]; then
echo ""
echo "Escalating privileges via sudo..."
exec sudo bash "$SCRIPT_PATH" "$TARGET_USER" --non-interactive
fi
if [ -z "$TARGET_USER" ]; then
echo "Error: No target user specified." >&2
read -rp "Press Enter to exit..."
exit 1
fi
@@ -50,5 +92,8 @@ echo "Reloading rules..."
udevadm control --reload-rules && udevadm trigger
echo "You may need a reboot for the plugin's write access to take effect"
echo ""
echo "You may need a reboot for the plugin's write access to take effect."
echo "Done!"
echo ""
read -rp "Press Enter to exit..."