#! /bin/bash

set -e

if [ ! -f /etc/laptop-mode/laptop-mode.conf -o ! -f /usr/sbin/laptop_mode ] ; then
  echo Laptop mode does not seem to be installed.
  exit 1
fi

#############################################################################

# Maximum time, in seconds, of hard drive spindown time that you are
# confortable with. Worst case, it's possible that you could lose this
# amount of work if your battery fails you while in laptop mode.
MAX_AGE=600

# Automatically disable laptop mode when the number of minutes of battery
# that you have left goes below this threshold.
MINIMUM_BATTERY_MINUTES=7

# Seconds laptop mode has to to wait after the disk goes idle before doing
# a sync.
LM_SECONDS_BEFORE_SYNC=2

# Enable laptop mode always, not just when on battery?
# (This will still disable laptop mode when the battery almost runs out.)
LAPTOP_MODE_ALWAYS_ON=0

# Enable laptop mode when the laptop's lid is closed, even when we're on
# AC power? This only works on ACPI.
LM_WHEN_LID_CLOSED=0

# Read-ahead, in kilobytes. You can spin down the disk while playing MP3/OGG
# by setting the disk readahead to 8MB (READAHEAD=8192). Effectively, the disk
# will read a complete MP3 at once, and will then spin down while the MP3/OGG is
# playing.
READAHEAD=8192

# Shall we remount journaled fs. with appropiate commit interval? (1=yes)
DO_REMOUNTS=1

# And shall we add the "noatime" option to that as well? (1=yes)
DO_REMOUNT_NOATIME=1

# Dirty synchronous ratio.  At this percentage of dirty pages the process
# which
# calls write() does its own writeback
DIRTY_RATIO=60

#
# Allowed dirty background ratio, in percent.  Once DIRTY_RATIO has been
# exceeded, the kernel will wake pdflush which will then reduce the amount
# of dirty memory to dirty_background_ratio.  Set this nice and low, so once
# some writeout has commenced, we do a lot of it.
#
DIRTY_BACKGROUND_RATIO=1

# kernel default dirty buffer age
DEF_AGE=30
DEF_UPDATE=5
DEF_DIRTY_BACKGROUND_RATIO=10
DEF_DIRTY_RATIO=40
DEF_XFS_AGE_BUFFER=15
DEF_XFS_SYNC_INTERVAL=30
DEF_XFS_BUFD_INTERVAL=1

# This must be adjusted manually to the value of HZ in the running kernel
# on 2.4, until the XFS people change their 2.4 external interfaces to work in
# centisecs. This can be automated, but it's a work in progress that still
# needs# some fixes. On 2.6 kernels, XFS uses USER_HZ instead of HZ for
# external interfaces, and that is currently always set to 100. So you don't
# need to change this on 2.6.
XFS_HZ=100

# Should the maximum CPU frequency be adjusted down while on battery?
# Requires CPUFreq to be setup.
# See Documentation/cpu-freq/user-guide.txt for more info
DO_CPU=0

# When on battery what is the maximum CPU speed that the system should
# use? Legal values are "slowest" for the slowest speed that your
# CPU is able to operate at, or a value listed in:
# /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies
# Only applicable if DO_CPU=1.
CPU_MAXFREQ=slowest

# Idle timeout for your hard drive (man hdparm for valid values, -S option)
# Default is 2 hours on AC (AC_HD_WITHOUT_LM=244) and 5 seconds for battery
# (BATT_HD=1) and for AC with laptop mode on (AC_HD_WITH_LM=1).
# Note: AC_HD_WITH_LM is used when AC power is present but laptop mode is enabled
# (i.e. with LAPTOP_MODE_ALWAYS_ON=1), while AC_HD_WITHOUT_LM is used when
# AC power is present and laptop mode is _disabled_. We still support the old
# option AC_HD: if it is set in the config file then it overrides both these
# options.
AC_HD_WITH_LM=1
AC_HD_WITHOUT_LM=244
BATT_HD=1

# Shall we adjust the power management values on the hard drives?
DO_HD_POWERMGMT=1

# Power management for HD (hdparm -B values)
AC_HDPARM_POWERMGMT_WITH_LM=1
AC_HDPARM_POWERMGMT_WITHOUT_LM=255
BATT_HDPARM_POWERMGMT=1

# The drives for which to adjust the idle timeout and power management settings.
# Separate them by a space, e.g. HD="/dev/hda /dev/hdb".
HD="/dev/hda /dev/hdb /dev/hdc /dev/hdd"

# Set the spindown timeout on the hard drives?
DO_HD=1

# Enable this if you have a buggy ACPI implementation that doesn't send
# out AC adapter events. This will make laptop mode check the AC state
# on battery state change events as well.
ACPI_WITHOUT_AC_EVENTS=0

# Enable DO_SYSLOG to "split" syslog.conf into AC_SYSLOG_WITHOUT_LM,
# AC_SYSLOG_WITH_LM and BATT_SYSLOG. This is done by replacing
# /etc/syslog.conf by a link to the respective files.

# NOTE: these files are NOT created by default, and if they do not
# exist this feature will not work. You can run the script
# /usr/sbin/lm-syslog-setup to set things up.
DO_SYSLOG=0
AC_SYSLOG_WITH_LM=/etc/syslog-on-ac-with-lm.conf
AC_SYSLOG_WITHOUT_LM=/etc/syslog-on-ac-without-lm.conf
BATT_SYSLOG=/etc/syslog-on-battery.conf

# This is the syslog file that should be symlinked to the other files.
SYSLOG_CONF=/etc/syslog.conf

#############################################################################

. /etc/laptop-mode/laptop-mode.conf

if [ ! -f $SYSLOG_CONF ] ; then
  echo Syslog configuration file \"$SYSLOG_CONF\" not found. 
  echo Please configure the correct path in /etc/laptop-mode/laptop-mode.conf.
  exit 1
fi

echo This script helps you to set up separate syslog.conf files for the
echo various modes a laptop can be in. Switching between the configurations
echo is managed by the Laptop Mode Tools.
echo

if [ "`readlink $SYSLOG_CONF`" != "" ] ; then
  echo $SYSLOG_CONF already seems to be symlinked. Either lm-syslog-setup
  echo has already been called, or some other program manages the syslog.conf
  echo symlink. This script will not continue, for safety reasons.
  exit 1
fi

echo Creating config files:
echo
echo -n AC power, laptop mode off: $AC_SYSLOG_WITHOUT_LM
if [ -f "$AC_SYSLOG_WITHOUT_LM" ] ; then
  echo " (exists)"
else
  if ( cp -a "$SYSLOG_CONF" "$AC_SYSLOG_WITHOUT_LM" ) ; then
    echo " (created)"
  else
    echo " (creation failed, exiting)"
    exit 1
  fi
fi
echo -n AC power, laptop mode on: $AC_SYSLOG_WITH_LM
if [ -f "$AC_SYSLOG_WITH_LM" ] ; then
  echo " (exists)"
else
  if ( cp -a "$SYSLOG_CONF" "$AC_SYSLOG_WITH_LM" ) ; then
    echo " (created)"
  else
    echo " (creation failed, exiting)"
    exit 1
  fi
fi
echo -n Battery power: $BATT_SYSLOG
if [ -f "$BATT_SYSLOG" ] ; then
  echo " (exists)"
else
  if ( cp -a "$SYSLOG_CONF" "$BATT_SYSLOG" ) ; then
    echo " (created)"
  else
    echo " (creation failed, exiting)"
    exit 1
  fi
fi
echo
while [ "$DO_ENABLE" != "y" -a "$DO_ENABLE" != "n" ] ; do
  echo -n "Do you wish to enable the split configuration now? (y/n) "
  read DO_ENABLE
done
if [ "$DO_ENABLE" == "y" ] ; then
  if ( ! sed -i /etc/laptop-mode/laptop-mode.conf -e "s/DO_SYSLOG=0/DO_SYSLOG=1/" || ! grep DO_SYSLOG=1 /etc/laptop-mode/laptop-mode.conf ) ; then
    echo Enabling the split configuration failed.
    exit 1
  else
    echo Enabling the split configuration succeeded.
    echo Restarting laptop mode to activate changes.
    /usr/sbin/laptop_mode auto force
  fi
fi
