#!/bin/sh
# vim:noexpandtab
# Simple powersave script
#
# Copyright 2006 Red Hat, Inc.
#
# Based on work from:
#    Bill Nottingham <notting@redhat.com>
#    Peter Jones <pjones@redhat.com>
#    David Zeuthen <davidz@redhat.com>
#    Richard Hughes <richard@hughsie.com>
#
# 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
#

export STASHNAME=pm-powersave
. "@PM-UTILS-LIBDIR@/pm-functions"

remove_powersave_lock() {
    release_lock "${STASHNAME}.lock"
}

help() {
    cat <<EOF
$0: Valid options are:
false or ac = turn powersaving features off.
true or battery = turn powersaving features on.
help = get detailed help.

EOF
if [ "$1" = "--help" ]; then
    cat <<EOF
The rest of this help message displays the variables
that can be used to tune the powersave hooks.  
You can change these variables using pm-utils config files.
(see the pm-utils README for more information)

EOF
else
    echo "You can get more detailed information by running pm-powersave --help"
fi
}

lock_and_load() {
# take the powersave lock.
# ensure it gets released no matter how we exit.
    try_lock "${STASHNAME}.lock" || exit 1
    trap remove_powersave_lock 0
    mkdir -p "${STORAGEDIR}"
    rm -f "${INHIBIT}"
    
    load_hook_blacklist
    
    init_logfile "${PM_LOGFILE}"
}

case $1 in
    true|battery) lock_and_load && run_hooks power true;;
    false|ac) lock_and_load && run_hooks power false;;
    --help) help && run_hooks power help;;
    '') lock_and_load; on_ac_power && run_hooks power false || run_hooks power true;;
    *) help && exit 1;;
esac
