forked from Ooooze/batctl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·236 lines (193 loc) · 5.95 KB
/
install.sh
File metadata and controls
executable file
·236 lines (193 loc) · 5.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/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 <<EOF
${BOLD}batctl installer${NC}
Usage: $0 [OPTIONS]
Options:
--uninstall Remove batctl and all associated files
--no-systemd Skip systemd services installation
--version VER Install a specific version (e.g. 2025.06.1)
--help Show this help
Install:
curl -fsSL https://raw.githubusercontent.com/${REPO}/master/install.sh | sudo bash
Uninstall:
curl -fsSL https://raw.githubusercontent.com/${REPO}/master/install.sh | sudo bash -s -- --uninstall
EOF
exit 0
}
check_root() {
if [[ $EUID -ne 0 ]]; then
die "This script must be run as root. Use: sudo bash $0"
fi
}
detect_arch() {
local arch
arch=$(uname -m)
case "$arch" in
x86_64|amd64) echo "x86_64" ;;
aarch64|arm64) echo "aarch64" ;;
*) die "Unsupported architecture: $arch" ;;
esac
}
detect_os() {
local os
os=$(uname -s)
case "$os" in
Linux) ;;
*) die "batctl only supports Linux (detected: $os)" ;;
esac
}
get_latest_version() {
local version
version=$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" \
| grep '"tag_name"' \
| sed -E 's/.*"tag_name":\s*"v?([^"]+)".*/\1/')
if [[ -z "$version" ]]; then
die "Failed to determine latest version. Check your internet connection."
fi
echo "$version"
}
download_and_install() {
local version="$1"
local arch="$2"
local tarball="batctl-${version}-linux-${arch}.tar.gz"
local url="https://github.com/${REPO}/releases/download/v${version}/${tarball}"
local tmpdir
tmpdir=$(mktemp -d)
trap 'rm -rf "$tmpdir"' EXIT
info "Downloading batctl v${version} for linux/${arch}..."
if ! curl -fsSL -o "${tmpdir}/${tarball}" "$url"; then
die "Download failed. Version v${version} may not have a binary for ${arch}.\n Check: https://github.com/${REPO}/releases"
fi
info "Extracting..."
tar xzf "${tmpdir}/${tarball}" -C "$tmpdir"
if [[ ! -f "${tmpdir}/${BINARY}" ]]; then
die "Archive does not contain '${BINARY}' binary"
fi
info "Installing binary to ${INSTALL_DIR}/${BINARY}..."
install -Dm755 "${tmpdir}/${BINARY}" "${INSTALL_DIR}/${BINARY}"
ok "Binary installed: ${INSTALL_DIR}/${BINARY}"
}
install_systemd() {
info "Installing systemd service..."
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 "$@"