#!/bin/bash
# Filename:      grml-autoconfig
# Purpose:       configuration interface for grml-autoconfig
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika(at)grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
################################################################################

if [ $(id -u) != 0 ] ; then
  echo "Error: please run this script with uid 0 (root)." ; exit 1
fi

LANG=C
LC_ALL=C
PN="$(basename $0)"
TMPFILE="$(mktemp)"

AUTOCONFIG=/etc/grml/autoconfig
[ -r $AUTOCONFIG ] || exit 1

. $AUTOCONFIG

# helper functions
activate_value()
{
  check_entry $1
  sed -i "s/$1.*/$1'yes'/" ${CONFIG_AUTOCONFIG_LOCAL}
}

deactivate_value()
{
  check_entry $1
  sed -i "s/$1.*/$1'no'/" ${CONFIG_AUTOCONFIG_LOCAL}
}

check_setting()
{
  grep -q $* $TMPFILE && return 0 || return 1
}

check_entry()
{
  if ! grep -q ${1} ${CONFIG_AUTOCONFIG_LOCAL} 2>/dev/null ; then
    grep $1 ${AUTOCONFIG} >> ${CONFIG_AUTOCONFIG_LOCAL}
  fi
}

is_set()
{
    [ $1 = 'yes' ] && return 0 || return 1
}
check_current_state()
{
  is_set $CONFIG_FSTAB      && FSTABSTATUS=ON    || FSTABSTATUS=OFF
  is_set $CONFIG_CPU        && CPUSTATUS=ON      || CPUSTATUS=OFF
  is_set $CONFIG_ACPI_APM   && ACPI_APMSTATUS=ON || ACPI_APMSTATUS=OFF
  is_set $CONFIG_SYSLOG     && SYSLOGSTATUS=ON   || SYSLOGSTATUS=OFF
  is_set $CONFIG_GPM        && GPMSTATUS=ON      || GPMSTATUS=OFF
}

# main program
interface()
{
  dialog --cr-wrap --clear --cancel-label "Exit" --title "$PN" --checklist "grml-autoconfig is the framework which includes hardware
detection, activation of system services and this is the
interface to activate or deactivate some features.

If you do not know what to do at this stage just leave it untouched,
the defaults represent the recommended values.

All the configuration happens in the file /etc/grml/autoconfig.local -
you can edit the file manually as well.

Please do not confuse these settings with plain Debian configuration.
For example disabling dhcp here will NOT deactivate any configured network
settings in /etc/network/interfaces, it just configures grml-autoconfig
related settings instead.
" 0 0 0 \
fstab "update /etc/fstab entries (check for devices)" $FSTABSTATUS \
cpufreq "activate cpu frequency scaling" $CPUSTATUS \
acpi_apm "load ACPI/APM modules" $ACPI_APMSTATUS \
syslog "start syslog-ng" $SYSLOGSTATUS \
gpm "start GPM (mouse on console)" $GPMSTATUS \
  2>$TMPFILE
}

set_values()
{
  check_setting fstab     && activate_value CONFIG_FSTAB=    || deactivate_value CONFIG_FSTAB=
  check_setting cpufreq   && activate_value CONFIG_CPU=      || deactivate_value CONFIG_CPU=
  check_setting acpi_apm  && activate_value CONFIG_ACPI_APM= || deactivate_value CONFIG_ACPI_APM=
  check_setting syslog    && activate_value CONFIG_SYSLOG=   || deactivate_value CONFIG_SYSLOG=
  check_setting gpm       && activate_value CONFIG_GPM=      || deactivate_value CONFIG_GPM=
}

# and now run it:
  check_current_state
  interface
  retval=$?
  case $retval in
   0)
    set_values && dialog --stdout --title "${PN}" --msgbox "Adjusting values via grml-autoconfig was successful!" 5 60 || \
    dialog --stdout --title "${PN}" --msgbox "There was an error adjusting values via grml-autoconfig. Sorry." 5 60
    rm -f $TMPFILE &>/dev/null
    ;;
   1)
    echo "Exit pressed."
    ;;
   255)
    echo "ESC pressed."
    exit 1
    ;;
  esac

## END OF FILE #################################################################
