#!/bin/sh
#
# chkconfig: - 20 90
# description: Starts and stops "laptop-mode" - tweaks system behavior
#              to extend battery life.
#
# config:  /etc/laptop-mode/laptop-mode.conf

test -f /usr/sbin/laptop_mode || exit 0

. /lib/lsb/init-functions

# Enable laptop mode when the system is booted when running on battery.

case $1 in
  start)
    log_action_begin_msg "Enabling laptop mode"
    touch /var/run/laptop-mode-enabled        
    RESULT=$(/usr/sbin/laptop_mode auto init)
    log_action_end_msg 0 "$RESULT"
    ;;

  restart|reload|force-reload)
    set -e
    log_action_begin_msg "Stopping laptop mode"
    
    # Full restart: first stop laptop mode completely (to restore default mount options etc.)
    rm -f /var/run/laptop-mode-enabled
    RESULT=$(/usr/sbin/laptop_mode stop init)
    log_action_end_msg 0 "$RESULT"
    
    # Now remove files containing stored status, re-enable, and start it up again.
    rm -f /var/run/laptop-mode-*
    touch /var/run/laptop-mode-enabled
    log_action_begin_msg "Starting laptop mode"
    RESULT=$(/usr/sbin/laptop_mode auto init force)
    log_action_end_msg 0 "$RESULT"
    ;;

  stop)
    rm -f /var/run/laptop-mode-enabled
    log_action_begin_msg "Disabling laptop mode"
    RESULT=$(/usr/sbin/laptop_mode stop init)
    if [ $? -ne 0 ]; then
    	log_action_end_msg 1 "$RESULT"
	exit 1
    fi
    log_action_end_msg 0
    ;;

  status)
    echo "Laptop mode status:"
    echo
    /usr/sbin/laptop_mode status
    ;;
    
  *)
    echo "Usage: $0 {stop|start|restart|reload|force-reload|status}" >&2
    exit 2
    ;;
esac
exit 0
