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

# --- Source libraries

for lib in /usr/share/tlp/tlp-func-base; do
    # shellcheck disable=SC1090
    . $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_mode; then
            $cmd "$@"
        fi
        ;;

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

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