Last active
May 2, 2023 09:17
-
-
Save WinkelCode/168028a9e20baa4393a60791cdb7e4b9 to your computer and use it in GitHub Desktop.
Unofficial and WIP, for this program: https://github.com/the-weird-aquarian/Battery-Notifier (License: unlicense.org)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -e | |
# --- Preamble --- | |
# username will be pulled from arguments | |
program_name="battery-notify" | |
sourceloc_script="./$program_name.sh" | |
sourceloc_systemd_unit="./$program_name.service" | |
targetloc_prog_directory="/var/lib/battery-notify" | |
targetloc_script="$targetloc_prog_directory/$program_name" | |
targetloc_systemd_unit="/etc/systemd/system/$program_name.service" | |
targetloc_profiled_script="/etc/profile.d/zzz-$program_name-add-to-path.sh" | |
profiled_script=$(cat <<-EOF | |
PATH="$targetloc_prog_directory:\$PATH" | |
EOF | |
) | |
expected_files=( | |
./battery-notify.sh | |
./battery-notify.service | |
) | |
# --- Functions --- | |
install_program() { | |
for file in "${expected_files[@]}"; do | |
if [ ! -f "$file" ]; then | |
echo "Error: Expected file '$file' not found" | |
echo "This script is meant to be run from the root of the $program_name repository" | |
exit 1 | |
fi | |
done | |
install -D --mode=755 "$sourceloc_script" "$targetloc_script" && echo "OK: Installed $program_name script" || { echo "Error: Failed to install $program_name script"; exit 1; } | |
install -D --mode=644 "$sourceloc_systemd_unit" "$targetloc_systemd_unit" && echo "OK: Installed $program_name systemd unit" || { echo "Error: Failed to install $program_name systemd unit"; exit 1; } | |
sed -e "s|User=.*|User=$systemd_unit_username|" -e "s|ExecStart=.*|ExecStart=/bin/bash \"$targetloc_script\"|" -i "$targetloc_systemd_unit" | |
cat "$targetloc_systemd_unit" | grep "User=$systemd_unit_username" &>/dev/null && echo "OK: Updated systemd unit to run as $systemd_unit_username" || { echo "Error: Failed to update systemd unit to run as $systemd_unit_username"; exit 1; } | |
cat "$targetloc_systemd_unit" | grep "ExecStart=/bin/bash \"$targetloc_script\"" &>/dev/null && echo "OK: Updated systemd unit to run $targetloc_script" || { echo "Error: Failed to update systemd unit to run $targetloc_script"; exit 1; } | |
echo "$profiled_script" >"$targetloc_profiled_script" && echo "OK: Created profile.d script" || { echo "Error: Failed to create profile.d script"; exit 1; } | |
source "$targetloc_profiled_script" || { echo "Error: Failed to source '$targetloc_profiled_script'"; exit 1; } | |
systemctl enable --quiet --now "$program_name.service" && echo "OK: Enabled and started $program_name systemd unit" || { echo "Error: Failed to enable and start $program_name systemd unit"; exit 1; } | |
# $program_name -h || { echo "Failed to run '$program_name -h'"; exit 1; } # Messes up because we're we're running as root at this point | |
echo "Done. Restart your shell or run 'source $targetloc_profiled_script' to add $program_name to your PATH variable" | |
exit 0 | |
} | |
uninstall_program() { | |
if systemctl is-active --quiet "$program_name.service" &>/dev/null; then | |
systemctl stop --quiet "$program_name.service" && echo "OK: Stopped $program_name systemd unit" || { echo "Error: Failed to stop $program_name systemd unit"; exit 1; } | |
fi | |
if systemctl is-enabled --quiet "$program_name.service" &>/dev/null; then | |
systemctl disable --quiet "$program_name.service" && echo "OK: Disabled $program_name systemd unit" || { echo "Error: Failed to disable $program_name systemd unit"; exit 1; } | |
fi | |
if [ -f "$targetloc_systemd_unit" ]; then | |
rm -f "$targetloc_systemd_unit" && echo "OK: Removed $program_name systemd unit" || { echo "Error: Failed to remove $program_name systemd unit"; exit 1; } | |
fi | |
if [ -f "$targetloc_profiled_script" ]; then | |
rm -f "$targetloc_profiled_script" && echo "OK: Removed $program_name profile.d script" || { echo "Error: Failed to remove $program_name profile.d script"; exit 1; } | |
fi | |
if [ -f "$targetloc_script" ]; then | |
rm -f "$targetloc_script" && echo "OK: Removed $program_name script" || { echo "Error: Failed to remove $program_name script"; exit 1; } | |
fi | |
if [ -d "$targetloc_prog_directory" ]; then | |
rm -rf "$targetloc_prog_directory" && echo "OK: Removed $program_name program directory" || { echo "Error: Failed to remove $program_name program directory"; exit 1; } | |
fi | |
echo "Done." | |
exit 0 | |
} | |
elevate_privileges() { | |
if [ $EUID -ne 0 ]; then | |
if command -v sudo &>/dev/null; then | |
exec sudo "$0" "$@" | |
else | |
echo "Sudo not installed, please run this script as root manually" | |
exit 1 | |
fi | |
fi | |
} | |
# --- Main --- | |
case "$1" in | |
install) | |
if [ ! "$2" ]; then | |
echo "Error: No username provided" | |
exit 1 | |
fi | |
systemd_unit_username="$2" | |
elevate_privileges "$@" | |
install_program | |
;; | |
uninstall) | |
elevate_privileges "$@" | |
uninstall_program | |
;; | |
*) | |
echo "Usage: $0 [install|uninstall] <username (install only)>" | |
echo "The username will be used to run the systemd unit, and is expected to be a non-root user on the system" | |
echo "This script will need to be run as root, but can elevate itself with sudo, if installed" | |
exit 1 | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Any chance we can fix this or use your steam OS script?