-
Notifications
You must be signed in to change notification settings - Fork 0
/
battery-check.sh
executable file
·382 lines (322 loc) · 9.41 KB
/
battery-check.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/bin/sh
pw_IconDir="/usr/local/share/icons/Adwaita"
pw_ExtraDir="/scalable/status"
# The name of the program, needs to be reachable in $PATH
pw_SoundEnabled="True"
pw_CustomMusicProgram="paplay" # Required if active
pw_SoundLocation="Charging.wav" # Required if active
# Your program needs arguments? Parse them below.
pw_ArgumentsBeforeFile="" #Optional
pw_ArgumentsAfterFile="" # Optional
pw_LowBatteryPercentage=20
pw_CriticalLowBatteryPercentage=10
pw_CheckStatus() {
case $pw_DebugMode in
"True")
case $1 in
"BatteryLevel")
pw_BatteryLevel="$(cat /tmp/pw_BatteryLevelDebug)";;
"BatteryStatus")
if [ "$pw_ACStatus" -eq 1 ]; then
pw_BatteryStatus=3
elif [ "$pw_BatteryLevel" -gt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=0
elif [ "$pw_BatteryLevel" -lt "$pw_CriticalLowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=2
elif [ "$pw_BatteryLevel" -lt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=1
fi;;
"ACStatus")
pw_ACStatus="$(cat /tmp/pw_ACStatusDebug)";;
*)
pw_BatteryLevel="$(cat /tmp/pw_BatteryLevelDebug)"
pw_ACStatus="$(cat /tmp/pw_ACStatusDebug)"
if [ "$pw_ACStatus" -eq 1 ]; then
pw_BatteryStatus=3
elif [ "$pw_BatteryLevel" -gt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=0
elif [ "$pw_BatteryLevel" -lt "$pw_CriticalLowBatteryPercentage" ]; then
pw_BatteryStatus=2
elif [ "$pw_BatteryLevel" -lt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=1
fi;;
esac;;
*)
case $1 in
"BatteryLevel")
if [ "$(uname)" = "FreeBSD" ]; then
pw_BatteryLevel="$(apm -l)"
elif [ "$(uname)" = "Linux" ]; then
pw_BatteryLevel="$(cat /sys/class/power_supply/BAT1/capacity)"
fi;;
"BatteryStatus")
# Battery Status
# 0 = High
# 1 = Low
# 2 = Critical
# 3 = Charging
if [ "$pw_ACStatus" -eq 1 ]; then
pw_BatteryStatus=3
elif [ "$pw_BatteryLevel" -gt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=0
elif [ "$pw_BatteryLevel" -lt "$pw_CriticalLowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=2
elif [ "$pw_BatteryLevel" -lt "$pw_LowBatteryPercentage" ] && [ "$pw_ACStatus" != 1 ]; then
pw_BatteryStatus=1
fi;;
"ACStatus")
# AC Status
# 0 - Off-Line
# 1 - On-Line
# 2 - Backup Power
if [ "$(uname)" = "FreeBSD" ]; then
pw_ACStatus="$(apm -a)"
elif [ "$(uname)" = "Linux" ]; then
if [ "$(cat /sys/class/power_supply/BAT1/status)" = "Charging" ]; then
pw_ACStatus=1
else
pw_ACStatus=0
fi
fi;;
*)
pw_CheckStatus ACStatus
pw_CheckStatus BatteryLevel
pw_CheckStatus BatteryStatus
esac
esac
}
pw_ChangeIcon() {
if [ "$pw_BatteryStatus" -eq 3 ] || [ "$pw_ACStatus" -eq 1 ]; then
local pw_Suffix="-charging"
else
local pw_Suffix=""
fi
if [ "$pw_ACStatus" -eq 1 ] && [ "$pw_BatteryStatus" -eq 3 ] && [ "$pw_BatteryLevel" -eq 100 ]; then
local pw_PercentageNumberIcon="100-charged"
else
local pw_BatteryLevel2=$(($pw_BatteryLevel / 10 * 10))
local pw_PercentageNumberIcon="$pw_BatteryLevel2$pw_Suffix"
fi
pw_NotifIcon="$pw_IconDir$pw_ExtraDir/battery-level-$pw_PercentageNumberIcon-symbolic.svg"
}
pw_SummonNotif() {
# Arguments
local pw_NotifTitle="$1"
local pw_NotifSubtitle="$2"
local pw_NotifUrgency="$3"
# Notify routines
if [ -z "$pw_NotifTitle" ] && [ -z "$pw_NotifSubtitle" ]; then
echo "Missing arguments"
else
notify-send -r 27078 "$pw_NotifTitle" "$pw_NotifSubtitle" -i "$pw_NotifIcon" -u "$pw_NotifUrgency" &
fi
}
pw_ForbiddenPrograms="echo rm touch cp mv mkdir printf rmdir sudo doas su"
pw_OneWordCheck() {
if [ -z "$pw_CustomMusicProgram" ] || [ -z "$pw_SoundLocation" ]; then
echo "[Error] One or more required variables are empty."
exit 1
else
local pw_Counter=0
for pw_CommandIteration1 in $pw_CustomMusicProgram; do
if [ $pw_Counter != 0 ]; then
echo "[Error] Command is longer than one word."
exit 1
fi
local pw_Counter=$(($pw_Counter + 1))
done
for pw_CommandIteration2 in $pw_ForbiddenPrograms; do
if [ "$pw_CustomMusicProgram" = "$pw_CommandIteration2" ]; then
echo "[Error] ""'"$pw_CommandIteration2"'"" is not allowed."
exit 1
fi
done
fi
for pw_Word in $pw_ForbiddenPrograms; do
echo "$pw_ArgumentsAfterFile" | grep -q "$pw_Word"
if [ $? -eq 0 ]; then
echo "[Error] ""'"$pw_ArgumentsAfterFile"'"" is not allowed."
exit 1
fi
done
for pw_Word in $pw_ForbiddenPrograms; do
echo "$pw_ArgumentsBeforeFile" | grep -q "$pw_Word"
if [ $? -eq 0 ]; then
echo "[Error] ""'"$ArgumentsBeforeFile"'"" is not allowed."
exit 1
fi
done
}
if [ $pw_SoundEnabled = "True" ]; then
pw_OneWordCheck
else
pw_CustomMusicProgram=""
unset pw_CustomMusicProgram
pw_SoundLocation=""
unset pw_SoundLocation
pw_ArgumentsBeforeFile=""
unset pw_ArgumentsBeforeFile
pw_ArgumentsAfterFile=""
unset pw_ArgumentsAfterFile
fi
pw_Sound() {
if [ "$pw_SoundEnabled" = "True" ]; then
eval "$pw_CustomMusicProgram" "$pw_ArgumentsBeforeFile" "'""$pw_SoundLocation""'" "$pw_ArgumentsAfterFile" &
fi
}
pw_DischargeNotif() {
pw_ChangeIcon
if [ "$pw_ACStatus" = 1 ]; then
if [ "$(cat /tmp/pw_BatteryDaemon)" = "X11" ]; then
if [ "$pw_BatteryLevel" = 100 ]; then
pw_SummonNotif "Charged - $pw_BatteryLevel%" "" "normal"
else
pw_SummonNotif "Charging - $pw_BatteryLevel%" "" "normal"
fi
else
if [ "$pw_BatteryLevel" = 100 ]; then
pw_SummonNotif "[Charged] $pw_BatteryLevel%"
else
echo "[Charging] $pw_BatteryLevel%"
fi
fi
else
local pw_Counter=0
while true; do
pw_CheckStatus ACStatus
pw_CheckStatus BatteryStatus
if [ "$pw_Counter" -eq 10 ]; then
return 1
elif [ "$pw_BatteryStatus" != 3 ] && [ "$pw_ACStatus" -eq 0 ]; then
break
fi
sleep 0.5s
local pw_Counter=$(($pw_Counter + 1))
done
pw_CheckStatus BatteryLevel
if [ "$(cat /tmp/pw_BatteryDaemon)" = "X11" ]; then
case $pw_BatteryStatus in
0)
pw_SummonNotif "Discharging - $pw_BatteryLevel%" "" "low" &;;
1)
pw_SummonNotif "Low Battery - $pw_BatteryLevel%" "Charge your laptop." "normal" &;;
2)
pw_SummonNotif "Critical Low Battery - $pw_BatteryLevel%" "Charge your laptop or it will shut down soon." "critical" &;;
esac
else
case $pw_BatteryStatus in
0)
echo "[Discharging] $pw_BatteryLevel%";;
1)
echo "[Low Battery] $pw_BatteryLevel% - Charge your laptop.";;
2)
echo "[Critical Low Battery] - $pw_BatteryLevel% - Charge your laptop or it will shut down soon.";;
esac
fi
fi
return
}
pw_QuitDaemon() {
rm /tmp/pw_BatteryDaemon
exit 0
}
pw_CheckStatus
pw_CurrentBatteryStatus="$pw_BatteryStatus"
if [ "$pw_CurrentBatteryStatus" = 3 ] || [ "$pw_ACStatus" = 1 ]; then
pw_Charging="True"
elif [ "$pw_CurrentBatteryStatus" != 3 ] || [ "$pw_ACStatus" != 1 ]; then
pw_Charging="False"
fi
if [ "$pw_BatteryLevel" = 100 ]; then
pw_BatteryFull="True"
else
pw_BatteryFull="False"
fi
pw_MainModule() {
pw_CheckStatus
if [ "$pw_CLIBehaviorLockin" != "True" ] ; then
if [ "$pw_DebugMode" != "True" ] && [ -z "$DISPLAY" ]; then
echo "CLI" > /tmp/pw_BatteryDaemon
elif [ "$pw_DebugMode" != "True" ] && [ -n "$DISPLAY" ]; then
echo "X11" > /tmp/pw_BatteryDaemon
fi
fi
if [ "$pw_ACStatus" -eq 0 ] && [ "$pw_Charging" = "True" ]; then
pw_ChangeIcon
if pw_DischargeNotif; then
pw_Charging="False"
fi
elif [ "$pw_ACStatus" -eq 1 ] && [ "$pw_Charging" = "False" ]; then
pw_ChangeIcon
if [ $pw_SoundEnabled = "True" ]; then
pw_Sound
fi
pw_DischargeNotif
pw_Charging="True"
fi
sleep 0.1s
if [ "$pw_BatteryLevel" -eq 100 ] && [ "$pw_BatteryFull" = "False" ]; then
pw_ChangeIcon
pw_DischargeNotif
pw_BatteryFull="True"
elif [ "$pw_BatteryLevel" != 100 ]; then
pw_BatteryFull="False"
fi
if [ "$pw_BatteryStatus" != "$pw_CurrentBatteryStatus" ] && [ "$pw_Charging" = "False" ]; then
pw_DischargeNotif
pw_CurrentBatteryStatus="$pw_BatteryStatus"
fi
}
case $1 in
"help")
echo "./battery-check.sh [arg1] [arg2]]"
echo "help - Shows the help information"
echo "daemon [subargs: cli] - Runs this script as a cron job/daemon"
exit 0;;
"daemon")
if [ -e /tmp/pw_BatteryDaemon ]; then
echo "[Error] You already have a battery daemon running."
exit 1
fi
if [ "$2" = "cli" ]; then
pw_CLIBehaviorLockin="True"
pw_DebugMode="False"
elif [ "$2" = "debug" ]; then
echo "Debug mode activated."
echo 95 > /tmp/pw_BatteryLevelDebug
echo 0 > /tmp/pw_ACStatusDebug
pw_DebugMode="True"
pw_CLIBehaviorLockin="False"
else
pw_DebugMode="False"
pw_CLIBehaviorLockin="False"
fi
if [ "$pw_CLIBehaviorLockin" = "False" ] && [ ! -z $DISPLAY ]; then
echo "X11" > /tmp/pw_BatteryDaemon
elif [ $pw_CLIBehaviorLockin = "True" ] || [ -z $DISPLAY ]; then
echo "CLI" > /tmp/pw_BatteryDaemon
fi
trap pw_QuitDaemon INT
trap pw_QuitDaemon TERM
trap pw_QuitDaemon ABRT
trap pw_QuitDaemon QUIT
trap pw_QuitDaemon HUP
while true; do
pw_MainModule
sleep 0.5s
done;;
*)
if [ ! -e /tmp/pw_BatteryDaemon ]; then
if [ "$1" != "cli" ] && [ ! -z $DISPLAY ]; then
echo "X11" > /tmp/pw_BatteryDaemon
pw_ChangeIcon
else
echo "CLI" > /tmp/pw_BatteryDaemon
fi
pw_DischargeNotif
rm /tmp/pw_BatteryDaemon
else
pw_DischargeNotif
fi
;;
esac