mirror of
https://github.com/zoriya/noctalia-shell.git
synced 2026-06-03 02:51:46 +00:00
Battery: refactor installation script, return different error codes
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -e
|
||||
|
||||
SUCCESS=0
|
||||
FAILURE=1
|
||||
MISSING_FILES=2
|
||||
UNSUPPORTED=3
|
||||
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
|
||||
print_error() {
|
||||
@@ -14,7 +18,7 @@ print_info() {
|
||||
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
print_error "This script must be run with root privileges"
|
||||
exit 1
|
||||
exit $FAILURE
|
||||
fi
|
||||
|
||||
print_info "Installing Battery Manager..."
|
||||
@@ -28,19 +32,42 @@ fi
|
||||
|
||||
if [ -z "$ACTUAL_USER" ]; then
|
||||
print_error "Could not determine the actual user"
|
||||
exit 1
|
||||
exit $FAILURE
|
||||
fi
|
||||
|
||||
print_info "Installing for user: $ACTUAL_USER"
|
||||
echo
|
||||
|
||||
if [ -f "$SCRIPT_DIR/battery-paths.conf" ]; then
|
||||
print_info "Paths configuration loaded from $SCRIPT_DIR/battery-paths.conf"
|
||||
else
|
||||
print_error "battery-paths.conf not found in $SCRIPT_DIR"
|
||||
exit 1
|
||||
print_info "Checking required files..."
|
||||
|
||||
MISSING_FILES_LIST=()
|
||||
|
||||
if [ ! -f "$SCRIPT_DIR/battery-paths.conf" ]; then
|
||||
MISSING_FILES_LIST+=("battery-paths.conf")
|
||||
fi
|
||||
|
||||
if [ ! -f "$SCRIPT_DIR/battery-manager.sh" ]; then
|
||||
MISSING_FILES_LIST+=("battery-manager.sh")
|
||||
fi
|
||||
|
||||
if [ ! -f "$SCRIPT_DIR/battery-manager.policy" ]; then
|
||||
MISSING_FILES_LIST+=("battery-manager.policy")
|
||||
fi
|
||||
|
||||
if [ ! -f "$SCRIPT_DIR/battery-manager.rules" ]; then
|
||||
MISSING_FILES_LIST+=("battery-manager.rules")
|
||||
fi
|
||||
|
||||
if [ ${#MISSING_FILES_LIST[@]} -gt 0 ]; then
|
||||
print_error "Missing required files in $SCRIPT_DIR:"
|
||||
for file in "${MISSING_FILES_LIST[@]}"; do
|
||||
print_error " - $file"
|
||||
done
|
||||
exit $MISSING_FILES
|
||||
fi
|
||||
|
||||
print_info "All required files found"
|
||||
|
||||
print_info "Checking battery paths..."
|
||||
BATTERY_PATHS=($(grep -v '^#' "$SCRIPT_DIR/battery-paths.conf" | grep -v '^$'))
|
||||
EXISTING_PATHS=()
|
||||
@@ -53,7 +80,7 @@ done
|
||||
|
||||
if [ ${#EXISTING_PATHS[@]} -eq 0 ]; then
|
||||
print_error "None of the battery control files exist. Please check your hardware compatibility."
|
||||
exit 1
|
||||
exit $UNSUPPORTED
|
||||
fi
|
||||
|
||||
print_info "Found ${#EXISTING_PATHS[@]} compatible battery control file(s)"
|
||||
@@ -61,29 +88,23 @@ print_info "Found ${#EXISTING_PATHS[@]} compatible battery control file(s)"
|
||||
print_info "Installing battery manager script..."
|
||||
BATTERY_MANAGER_PATH="/usr/bin/battery-manager-$ACTUAL_USER"
|
||||
|
||||
if [ -f "$SCRIPT_DIR/battery-manager.sh" ]; then
|
||||
SHEBANG=$(head -n 1 "$SCRIPT_DIR/battery-manager.sh")
|
||||
SHEBANG=$(head -n 1 "$SCRIPT_DIR/battery-manager.sh")
|
||||
echo "$SHEBANG" > "$BATTERY_MANAGER_PATH"
|
||||
echo "" >> "$BATTERY_MANAGER_PATH"
|
||||
|
||||
echo "$SHEBANG" > "$BATTERY_MANAGER_PATH"
|
||||
echo "" >> "$BATTERY_MANAGER_PATH"
|
||||
echo "BATTERY_PATHS=(" >> "$BATTERY_MANAGER_PATH"
|
||||
for path in "${EXISTING_PATHS[@]}"; do
|
||||
echo " \"$path\"" >> "$BATTERY_MANAGER_PATH"
|
||||
done
|
||||
echo ")" >> "$BATTERY_MANAGER_PATH"
|
||||
|
||||
echo "BATTERY_PATHS=(" >> "$BATTERY_MANAGER_PATH"
|
||||
for path in "${EXISTING_PATHS[@]}"; do
|
||||
echo " \"$path\"" >> "$BATTERY_MANAGER_PATH"
|
||||
done
|
||||
echo ")" >> "$BATTERY_MANAGER_PATH"
|
||||
echo "" >> "$BATTERY_MANAGER_PATH"
|
||||
|
||||
echo "" >> "$BATTERY_MANAGER_PATH"
|
||||
tail -n +2 "$SCRIPT_DIR/battery-manager.sh" >> "$BATTERY_MANAGER_PATH"
|
||||
|
||||
tail -n +2 "$SCRIPT_DIR/battery-manager.sh" >> "$BATTERY_MANAGER_PATH"
|
||||
|
||||
chmod +x "$BATTERY_MANAGER_PATH"
|
||||
print_info "Battery manager script created from $SCRIPT_DIR/battery-manager.sh with compatible paths"
|
||||
else
|
||||
print_error "battery-manager.sh not found in $SCRIPT_DIR"
|
||||
exit 1
|
||||
fi
|
||||
chmod +x "$BATTERY_MANAGER_PATH"
|
||||
|
||||
print_info "Battery manager script created from $SCRIPT_DIR/battery-manager.sh with compatible paths"
|
||||
print_info "Script installed at $BATTERY_MANAGER_PATH"
|
||||
|
||||
print_info "Creating log file..."
|
||||
@@ -95,30 +116,19 @@ print_info "Creating polkit policy..."
|
||||
|
||||
POLICY_FILE="/usr/share/polkit-1/actions/com.local.battery-manager.$ACTUAL_USER.policy"
|
||||
|
||||
if [ -f "$SCRIPT_DIR/battery-manager.policy" ]; then
|
||||
sed -e "s/ACTUAL_USER_PLACEHOLDER/$ACTUAL_USER/g" \
|
||||
"$SCRIPT_DIR/battery-manager.policy" > "$POLICY_FILE"
|
||||
print_info "Polkit policy copied from $SCRIPT_DIR/battery-manager.policy"
|
||||
else
|
||||
print_error "battery-manager.policy not found in $SCRIPT_DIR"
|
||||
exit 1
|
||||
fi
|
||||
sed -e "s/ACTUAL_USER_PLACEHOLDER/$ACTUAL_USER/g" \
|
||||
"$SCRIPT_DIR/battery-manager.policy" > "$POLICY_FILE"
|
||||
|
||||
print_info "Polkit policy copied from $SCRIPT_DIR/battery-manager.policy"
|
||||
print_info "Polkit policy created at $POLICY_FILE"
|
||||
|
||||
print_info "Creating polkit rule..."
|
||||
|
||||
RULES_FILE="/etc/polkit-1/rules.d/50-battery-manager-$ACTUAL_USER.rules"
|
||||
|
||||
if [ -f "$SCRIPT_DIR/battery-manager.rules" ]; then
|
||||
sed "s/ACTUAL_USER_PLACEHOLDER/$ACTUAL_USER/g" \
|
||||
"$SCRIPT_DIR/battery-manager.rules" > "$RULES_FILE"
|
||||
print_info "Polkit rule copied from $SCRIPT_DIR/battery-manager.rules"
|
||||
else
|
||||
print_error "battery-manager.rules not found in $SCRIPT_DIR"
|
||||
exit 1
|
||||
fi
|
||||
sed "s/ACTUAL_USER_PLACEHOLDER/$ACTUAL_USER/g" \
|
||||
"$SCRIPT_DIR/battery-manager.rules" > "$RULES_FILE"
|
||||
|
||||
print_info "Polkit rule copied from $SCRIPT_DIR/battery-manager.rules"
|
||||
print_info "Polkit rule created for user: $ACTUAL_USER at $RULES_FILE"
|
||||
|
||||
print_info "Restarting polkit..."
|
||||
|
||||
Reference in New Issue
Block a user