forked from sreejithag/battery-charging-limiter-linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlimitrc.sh
71 lines (56 loc) · 1.68 KB
/
limitrc.sh
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
#!/usr/bin/env bash
limit="$1"
throw_err() {
if [ $# -eq 0 ]; then
msg="Unknown error occurred"
else
msg="$1"
fi
echo "$msg :("
exit 1
}
set_limit() {
echo "$limit" > /sys/class/power_supply/BAT?/charge_control_end_threshold || throw_err "Could not create init script"
echo "Max battery capacity is set to be limiting to $limit% $(tput setaf 2)✓ $(tput sgr0)"
}
create_init() {
cd /tmp || throw_err "Could not cd into /tmp"
echo "#!/sbin/openrc-run" > batlimit || throw_err "Could not create init script"
{
echo ""
echo "name=\$RC_SVCNAME"
echo "description=\"limit battery charging\""
echo "command=\"echo $limit > /sys/class/power_supply/BAT?/charge_control_end_threshold\""
echo "pidfile=\"/run/\${RC_SVCNAME}.pid\""
} >> batlimit
echo "init script creation complete $(tput setaf 2)✓ $(tput sgr0)"
cp batlimit /etc/init.d/
rc-update add batlimit default || throw_err "Could not add service to runlevel default"
echo "Openrc init script \"batlimit\" added to runlevel default $(tput setaf 2)✓ $(tput sgr0)"
}
check_val() {
if ! echo "$limit" | grep -E -q '^[0-9]+$'; then
echo "Enter a numeric max limit"
elif [ "$limit" -gt 100 ] || [ "$limit" -le 0 ]; then
echo "Please enter a valid max limit between [1-100]"
else
return 0
fi
return 1
}
check_root() {
echo "checking permissions..."
if [ "$EUID" -ne 0 ]; then
echo "You must run this script as root"
return 1
fi
echo "root $(tput setaf 2)✓ $(tput sgr0)"
return 0
}
if check_val && check_root; then
if set_limit
then create_init
fi
else
exit 1
fi