#!/bin/sh
#
# Test if the computer is running on line power
# Exit status:
# - 0 (true)  System is on AC power
# - 1 (false) System is not on AC power
#
# NOTE: Batteries are not good indicators unless we also check their type,
#       as some peripherals have batteries.
#
# Copyright 2010 Red Hat, Inc.
#
# Author: Adam Jackson <ajax@redhat.com>
#
# Based on work from:
#    Richard Hughes <hughsient@gmail.com>
#    Peter Jones <pjones@redhat.com>
#    Stefan Seyfried <seife@suse.de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

adaptors_present=0

for i in /sys/class/power_supply/* ; do
    # check that it exists; the glob might not expand, after all.
    [ -d "$i" ] || continue

    # if it's not an AC adaptor, ignore it
    grep -qi mains "$i"/type || continue

    adaptors_present=1

    # if it's an AC adaptor and it's online, we're done
    [ `cat "$i"/online` -eq 1 ] && exit 0
done

# if there are adaptors, they're offline.
# if there aren't, assume AC.
exit $adaptors_present
