#!/bin/sh

case $(uname -i) in
    x86_64)
        libpath="/usr/lib64"
    ;;

    *)
        libpath="/usr/lib"
    ;;
esac

TLP=/usr/sbin/tlp
LIB=${libpath}/tlp-pm/tlp-functions
RFLIB=${libpath}/tlp-pm/tlp-rf-func
CFG=/etc/default/tlp

[ -x $TLP ] || exit 0
[ -f $LIB ] || exit 0
[ -f $RFLIB ] || exit 0
[ -f $CFG ] || exit 0

. $LIB
. $RFLIB
read_defaults

echo_debug "run" "+++ init.d $1"
echo_debug "path" "PATH=$PATH"

check_tlp_enabled || exit 0

case $1 in
    start|restart|force-reload)
        echo -n "Loading tp-smapi kernel module..."
        load_tp_modules
        echo "done. "

        echo -n "Disabling radios:"
        for dev in $DEVICES_TO_DISABLE_ON_STARTUP; do
            echo -n " $dev"
            device_off $dev
        done
        echo "."

        echo -n "Setting battery charge thresholds..."
        set_charge_thresholds
        echo "done."

        UPWR=$(which upower)
        if [ -n "$UPWR" ]; then
            echo -n "Starting upowerd..."
            $UPWR -e > /dev/null 2>&1
            echo "done."
            echo_debug "run" "upower_start"
        fi
        ;;

    stop)
        echo -n "Disabling radios:"
        for dev in $DEVICES_TO_DISABLE_ON_SHUTDOWN; do
            echo -n " $dev"
            device_off $dev
        done
        echo "."
        ;;

    status)
        /usr/bin/tlp-stat
        ;;

    *)
        echo "Usage: tlp {start|stop|restart|force-reload|status}" >&2
        exit 3
        ;;
esac

exit 0
