#! /usr/bin/sh

# Settings from /etc/sysconfig/filename are available as environment vars
# with the name 'SYS__FILENAME__KEY' (filename converted to upper case).
#
# Not all files are parsed, current list is:
#   bootloader, language
#

# usage: get-option OPTION
#
# Read boot option.
#
# OPTION is either of the form 'key=value' or 'key="value"' or just 'key'.
#
# The option value is written into the file passed via 'PBL_RESULT' environment var.
# Either 'option=value', or 'option', or nothing is returned.

if [ -e /etc/default/grub ] ; then
  . /etc/default/grub
  if [ -z "$GRUB_CMDLINE_LINUX_DEFAULT" ] ; then
    echo "Warning: GRUB_CMDLINE_LINUX_DEFAULT is empty"
  fi
else
  echo "/etc/default/grub missing"
  exit 1
fi

cmd="$GRUB_CMDLINE_LINUX_DEFAULT"

opt="$1"

echo "CMDLINE: $cmd"

ifs_orig="$IFS" IFS=

nl=$'\n'
q=0
n=
while read -r -n 1 foo ; do
  if [ "$q" = 0 ] ; then
    [ "$foo" = '"' ] && q=1
  else
    [ "$foo" = '"' ] && q=0
  fi
  [ "$foo" = " " -a "$q" = 0 ] && foo="$nl"
  n="$n$foo"
done <<XXX
$cmd
XXX

IFS="$nl"

matched=
for i in $n ; do
  match="${i#"$opt"}"
  if [ "$match" != "$i" -a \( -z "$match" -o "${match:0:1}" = "=" \) ] ; then
    matched="$i"
  fi
done

IFS="$ifs_orig"

if [ -n "$matched" ] ; then
  echo "$matched" >> "$PBL_RESULT"
fi

exit 0
