#!/bin/sh
# tlp - run commands depending on power source
#
# Copyright (c) 2017 Thomas Koch <linrunner at gmx.net>
# This software is licensed under the GPL v2 or later.

# --- Constants
readonly LIBDIR="/usr/share/tlp"
readonly LIBS="tlp-functions"

# --- Source libraries
for lib in $LIBS; do
    if [ ! -f $LIBDIR/$lib ]; then
        echo "Error: missing function library \'$LIBDIR/$lib\'." 1>&2
        exit 1
    fi
    . $LIBDIR/$lib
done

# --- MAIN
self=${0##*/}

cmd=$1
if [ -z "$cmd" ]; then
    echo "Usage: $self command [arg(s)]" 1>&2
    exit 1
fi
if ! cmd_exists $cmd; then
    echo "Error: \"$cmd\" not found." 1>&2
    exit 2
fi
shift

case $self in
    run-on-ac)
        if get_power_state; then
            $cmd $@
        fi
        ;;

    run-on-bat)
        if ! get_power_state; then
            $cmd $@
        fi
        ;;

    *)
        echo "Error: unknown mode $self." 1>&2
        exit 1
        ;;
esac
