#!/usr/bin/env bash set -euo pipefail REPO="Ooooze/batctl" BINARY="batctl" INSTALL_DIR="/usr/bin" SYSTEMD_DIR="/etc/systemd/system" RESUME_SERVICE="batctl-resume.service" RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' CYAN='\033[0;36m' BOLD='\033[1m' NC='\033[0m' info() { echo -e "${CYAN}[info]${NC} $*"; } ok() { echo -e "${GREEN}[ok]${NC} $*"; } warn() { echo -e "${YELLOW}[warn]${NC} $*"; } error() { echo -e "${RED}[error]${NC} $*" >&2; } die() { error "$@"; exit 1; } usage() { cat < "${SYSTEMD_DIR}/batctl.service" <<'SERVICE' [Unit] Description=Apply battery charge thresholds (batctl) After=multi-user.target [Service] Type=oneshot ExecStart=/usr/bin/batctl apply RemainAfterExit=yes [Install] WantedBy=multi-user.target SERVICE ok "Service installed: ${SYSTEMD_DIR}/batctl.service" info "Installing resume service..." cat > "${SYSTEMD_DIR}/${RESUME_SERVICE}" <<'RESUME' [Unit] Description=Restore battery charge thresholds after resume (batctl) After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target [Service] Type=oneshot ExecStart=/usr/bin/batctl apply [Install] WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target RESUME ok "Resume service installed: ${SYSTEMD_DIR}/${RESUME_SERVICE}" if command -v systemctl &>/dev/null; then systemctl daemon-reload ok "systemd daemon reloaded" fi } do_uninstall() { check_root info "Uninstalling batctl..." if command -v systemctl &>/dev/null; then systemctl disable batctl.service 2>/dev/null || true systemctl stop batctl.service 2>/dev/null || true systemctl disable "${RESUME_SERVICE}" 2>/dev/null || true fi local files=( "${INSTALL_DIR}/${BINARY}" "${SYSTEMD_DIR}/batctl.service" "${SYSTEMD_DIR}/${RESUME_SERVICE}" "/etc/batctl.conf" ) for f in "${files[@]}"; do if [[ -f "$f" ]]; then rm -f "$f" ok "Removed: $f" fi done if command -v systemctl &>/dev/null; then systemctl daemon-reload 2>/dev/null || true fi ok "batctl has been uninstalled" } do_install() { local version="$1" local skip_systemd="$2" check_root detect_os local arch arch=$(detect_arch) if [[ -z "$version" ]]; then version=$(get_latest_version) fi echo "" echo -e "${BOLD} ⚡ batctl installer${NC}" echo -e " Version: ${CYAN}${version}${NC} Arch: ${CYAN}${arch}${NC}" echo "" download_and_install "$version" "$arch" if [[ "$skip_systemd" == "false" ]]; then install_systemd else warn "Skipping systemd services installation (--no-systemd)" fi echo "" ok "${BOLD}batctl v${version} installed successfully!${NC}" echo "" echo -e " Get started:" echo -e " ${CYAN}sudo batctl${NC} # Launch TUI" echo -e " ${CYAN}batctl status${NC} # Show battery info" echo -e " ${CYAN}sudo batctl set --stop 80${NC} # Set charge limit" echo -e " ${CYAN}sudo batctl persist enable${NC} # Survive reboots" echo "" } main() { local version="" local uninstall=false local skip_systemd=false while [[ $# -gt 0 ]]; do case "$1" in --uninstall) uninstall=true; shift ;; --no-systemd) skip_systemd=true; shift ;; --version) version="$2"; shift 2 ;; --help|-h) usage ;; *) die "Unknown option: $1. Use --help for usage." ;; esac done if [[ "$uninstall" == "true" ]]; then do_uninstall else do_install "$version" "$skip_systemd" fi } main "$@"