#!/bin/bash
# Filename:      grml2hd-bootparams
# Purpose:       prompt for bootparams which should be use for the grml system
# 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.
################################################################################

LANG=C
LC_ALL=C
PN=$(basename "$0")
TMPFILE=$(mktemp)
CMDLINE="$(cat /proc/cmdline 2>/dev/null)"

# same for strings
stringinstring(){
  case "$2" in *$1*) return 0;; esac
  return 1
}

# Reread boot command line; echo last parameter's argument or return false.
getbootparam(){
  stringinstring " $1=" "$CMDLINE" || return 1
  result="${CMDLINE##*$1=}"
  result="${result%%[ 	]*}"
  echo "$result"
  return 0
}

# Check boot commandline for specified option
checkbootparam(){
  stringinstring " $1" "$CMDLINE"
  return "$?"
}

if checkbootparam "resume" ; then
  RESUME_OPTS="=$(getbootparam resume)"
fi

# helper functions
get_value()
{
  grep -q $* /proc/cmdline && return 0 || return 1
}

get_value "noapic"             && NOAPIC="on"     || NOAPIC="off"
get_value "acpi=force"         && ACPI_FORCE="on" || ACPI_FORCE="off"
get_value "nolapic"            && NOLAPIC="on"    || NOLAPIC="off"
get_value "lapic"              && LAPIC="on"      || LAPIC="off"
get_value "acpi=off"           && ACPI_OFF="on"   || ACPI_OFF="off"
get_value "acpi=noirq"         && ACPI_NOIRQ="on" || ACPI_NOIRQ="off"
get_value "noirqbalance"       && NOIRQ="on"      || NOIRQ="off"
get_value "nopcmcia"           && NOPCMIA="on"    || NOPCMIA="off"
get_value "nodma"              && NODMA="on"      || NODMA="off"
get_value "pci=noacpi"         && PCINOACPI="on"  || PCINOACPI="off"
get_value "pci=biosirq"        && PCIBIOS="on"    || PCIBIOS="off"
get_value "pci=assign-busses"  && PCIASSIGN="on"  || PCIASSIGN="off"
get_value "irqpoll"            && IRQPOLL="on"    || IRQPOLL="off"
get_value "resume"             && RESUME="on"     || RESUME="off"

# main program
interface()
{
  dialog --cr-wrap --clear --title "$PN" --checklist "Do you want to use any special boot parameters?

This script checks for the boot parameters you used for booting
the Grml system. If there were any important options found
they will be activated in this menu.

If you do not know what to do at this stage just continue.

Set the boot parameters for kernel via bootmanager (lilo/grub):
" 0 0 0 \
  noapic 'disable buggy APIC interrupt routing' $NOAPIC \
  acpi=force 'force usage of ACPI' $ACPI_FORCE \
  nolapic 'disable buggy APIC interrupt routing' $NOLAPIC \
  lapic 'enable the local APIC even if BIOS disabled it' $LAPIC \
  acpi=off 'completely disable ACPI' $ACPI_OFF \
  acpi=noirq 'disable PCI IRQ routing of ACPI' $ACPI_NOIRQ \
  noirqbalance 'disable kernel irq balancing' $NOIRQ \
  nopcmcia 'avoid startup of PCMCIA services' $NOPCMIA \
  nodma 'disable Direct Memory Access' $NODMA \
  pci=noacpi 'disable ACPI for PCI maps' $PCINOACPI \
  pci=biosirq 'use PCI BIOS calls to get interrupt routing table' $PCIBIOS \
  pci=assign-busses 'assign all PCI bus numbers ourselves' $PCIASSIGN \
  irqpoll 'when an interrupt is not handled search in all handlers' $IRQPOLL \
  resume$RESUME_OPTS 'specify swap partition to resume from' $RESUME \
  2>$TMPFILE
}

# and now run it:
  interface
  cat $TMPFILE | sed 's/"//g' > /tmp/bootappendline
  rm -f $TMPFILE &>/dev/null

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