| 1 | #!/bin/sh |
| 2 | # This script exits with status 0 when the computer is on AC power (or no AC |
| 3 | # power supply found), 1 otherwise (i.e. running on battery). |
| 4 | set -u |
| 5 | |
| 6 | verbose=false |
| 7 | [ "${1:-}" = '-v' ] && verbose=true |
| 8 | |
| 9 | # If we do not have any power supplies, assume we are on AC. |
| 10 | rc=0 |
| 11 | |
| 12 | # Iterate through power supplies sysfs knows about. |
| 13 | for ps in /sys/class/power_supply/*; do |
| 14 | # Ignore batteries. |
| 15 | [ -r $ps/type ] && [ "$(cat $ps/type)" = 'Battery' ] && continue |
| 16 | |
| 17 | # Ignore when no online info (we return 0 when no AC power found). |
| 18 | [ -r $ps/online ] || continue |
| 19 | |
| 20 | # We know we have an AC adaptor, our default return changes to failed. |
| 21 | rc=1 |
| 22 | |
| 23 | if [ "$(cat $ps/online)" -eq 1 ]; then |
| 24 | rc=0 |
| 25 | break |
| 26 | fi |
| 27 | done |
| 28 | |
| 29 | $verbose && echo $rc |
| 30 | exit $rc |