#! /bin/sh
set -e

# Generate kboot.conf by inspecting /boot contents.
# Copyright (C) 2011  Antonio Ospite <ospite@studenti.unina.it>
#
# Inspired by kboot-mkconfig
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This progra, is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software.  If not, see <http://www.gnu.org/licenses/>.

PACKAGE_NAME=kboot-mkconfig
PACKAGE_VERSION=0.1

prefix=/usr
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
bindir=${exec_prefix}/bin
libdir=${exec_prefix}/lib
sysconfdir=/etc
host_os=linux-gnu
datarootdir=${prefix}/share
datadir=${datarootdir}
pkgdatadir=${datadir}/kboot
kboot_conf=""

self=`basename $0`

# Usage: usage
# Print the usage.
usage () {
    cat <<EOF
Usage: $self [OPTION]
Generate a kboot config file

  -o, --output=FILE       output generated config to FILE [default=stdout]
  -h, --help              print this message and exit
  -v, --version           print the version information and exit

Report bugs to <bug-kboot@gnu.org>.
EOF
}

argument () {
  opt=$1
  shift

  if test $# -eq 0; then
      echo "$0: option requires an argument -- '$opt'" 1>&2
      exit 1
  fi
  echo $1
}

# Check the arguments.
while test $# -gt 0
do
    option=$1
    shift

    case "$option" in
    -h | --help)
	usage
	exit 0 ;;
    -v | --version)
	echo "$self (${PACKAGE_NAME}) ${PACKAGE_VERSION}"
	exit 0 ;;
    -o | --output)
	kboot_conf=`argument $option "$@"`; shift;;
    --output=*)
	kboot_conf=`echo "$option" | sed 's/--output=//'`
	;;
    -*)
	echo "Unrecognized option \`$option'" 1>&2
	usage
	exit 1
	;;
    # Explicitly ignore non-option arguments, for compatibility.
    esac
done

. ${libdir}/kboot/kboot-mkconfig_lib

if [ "x$EUID" = "x" ] ; then
  EUID=`id -u`
fi

if [ "$EUID" != 0 ] ; then
  root=f
  case "`uname 2>/dev/null`" in
    CYGWIN*)
      # Cygwin: Assume root if member of admin group
      for g in `id -G 2>/dev/null` ; do
	case $g in
	  0|544) root=t ;;
	esac
      done ;;
  esac
  if [ $root != t ] ; then
    echo "$self: You must run this as root" >&2
    exit 1
  fi
fi


if test -f ${sysconfdir}/default/kboot ; then
  . ${sysconfdir}/default/kboot
fi

# Default values
if [ "x${KBOOT_TIMEOUT}" = "x" ] ; then KBOOT_TIMEOUT=5 ; fi

if [ "x${KBOOT_DISTRIBUTOR}" = "x" ] ; then
  OS=GNU/Linux
else
  OS="${KBOOT_DISTRIBUTOR} GNU/Linux"
fi

# These are optional, user-defined variables.
export KBOOT_TIMEOUT \
  KBOOT_DISTRIBUTOR \
  KBOOT_CMDLINE_LINUX \
  KBOOT_CMDLINE_LINUX_DEFAULT \
  KBOOT_DISABLE_RECOVERY

if test "x${kboot_conf}" != "x"; then
  rm -f ${kboot_conf}.new
  exec > ${kboot_conf}.new
fi
echo "Generating kboot.conf ..." >&2

cat << EOF
#
# DO NOT EDIT THIS FILE
#
# It is automatically generated by $self
# with settings from ${sysconfdir}/default/kboot
#

timeout=${KBOOT_TIMEOUT}

EOF

echo "### BEGIN list of kernels ###"
list_kernels
echo "### END list of kernels ###"

if test "x${kboot_conf}" != "x" ; then
   # none of the children aborted with error, install the new kboot.cfg
   mv -f ${kboot_conf}.new ${kboot_conf}
fi

echo "done" >&2