#! /bin/sh
set -e

# grub-mkconfig helper script.
# Copyright (C) 2006,2007,2008,2009,2010  Free Software Foundation, Inc.
#
# GRUB 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.
#
# GRUB 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 GRUB.  If not, see <http://www.gnu.org/licenses/>.

prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@
. ${libdir}/grub/grub-mkconfig_lib

export TEXTDOMAIN=@PACKAGE@
export TEXTDOMAINDIR=@localedir@

if [ "x${GRUB_DISTRIBUTOR}" = "x" ] ; then
  OS="NetBSD"
else
  OS="${GRUB_DISTRIBUTOR} NetBSD"
fi

netbsd_load_fs_module ()
{
  loader="$1"	# "knetbsd" or "multiboot"
  kernel="$2"	# absolute path to the kernel file

  case $(zcat -f "${kernel}" | file -bL - | cut -d , -f 2 | tr -d ' ') in
    Intel80386)
      karch="i386"
      ;;
    x86-64)
      karch="amd64"
      ;;
    *)
      return
      ;;
  esac

  case $(${grub_probe} --target=fs -d ${GRUB_DEVICE}) in
    ext2)
      kmod="ext2fs"
      ;;
    fat)
      kmod="msdosfs"
      ;;
    ntfs)
      kmod="ntfs"
      ;;
    ufs*)
      kmod="ffs"
      ;;
    *)
      return
      ;;
  esac

  kversion=$(zcat -f "${kernel}" | strings | sed -n -e '/^@(#)NetBSD/ { s/^@(#)NetBSD \([0-9\.]*\) .*$/\1/g ; p ; q ; }')
  kmodule="/stand/${karch}/${kversion}/modules/${kmod}/${kmod}.kmod"

  if test -z "$karch" -o -z "$kversion" -o ! -f "${kmodule}"; then
    return
  fi

  kmodule_rel=$(make_system_path_relative_to_its_root "$kmodule") || return
  prepare_grub_to_access_device $(${grub_probe} -t device "${kmodule}") | sed -e 's,^,	,'
  case "${loader}" in
    knetbsd)
      printf "\tknetbsd_module_elf %s\n" "${kmodule_rel}"
      ;;
    multiboot)
      printf "\tmodule %s\n" "${kmodule_rel}"
      ;;
  esac
}

netbsd_entry ()
{
  loader="$1"	# "knetbsd" or "multiboot"
  kernel="$2"	# absolute path to the kernel file
  recovery="$3"	# is this is a recovery entry?
  args="$4"	# extra arguments appended to loader command

  kroot_device="$(echo ${GRUB_DEVICE} | sed -e 's,^/dev/r,,')"
  if ${recovery} ; then
    title="$(gettext_quoted "%s, with kernel %s (via %s, recovery mode)")"
  else
    title="$(gettext_quoted "%s, with kernel %s (via %s)")"
  fi

  printf "menuentry \"${title}\" {\n" \
    "${OS}" "$(echo ${kernel} | sed -e 's,^.*/,,')" "${loader}"
  printf "%s\n" "${prepare_boot_cache}"
  case "${loader}" in
    knetbsd)
      printf "\tknetbsd %s -r %s %s\n" \
        "${kernel}" "${kroot_device}" "${GRUB_CMDLINE_NETBSD} ${args}"
      ;;
    multiboot)
      printf "\tmultiboot %s %s root=%s %s\n" \
        "${kernel}" "${kernel}" "${kroot_device}" "${GRUB_CMDLINE_NETBSD} ${args}"
      ;;
  esac

  netbsd_load_fs_module "${loader}" "${kernel}"

  printf "}\n"
}

prepare_boot_cache="$(prepare_grub_to_access_device ${GRUB_DEVICE} | sed -e 's,^,	,')"

# We look for NetBSD kernels in / but not in subdirectories.  We simply
# pick all statically linked ELF executable files (or links) in / with a
# name that starts with `netbsd'.
pattern="^ELF[^,]*executable.*statically linked"
for k in $(ls -t /netbsd*) ; do
  if ! grub_file_is_not_garbage "$k" ; then
    continue
  fi
  if ! (zcat -f "$k" | file -bL - | grep -q "${pattern}") 2>/dev/null ; then
    continue
  fi

  echo "Found NetBSD kernel: $k" >&2
  netbsd_entry "knetbsd"   "$k" false "${GRUB_CMDLINE_NETBSD_DEFAULT}"
  netbsd_entry "multiboot" "$k" false "${GRUB_CMDLINE_NETBSD_DEFAULT}"
  if [ "x${GRUB_DISABLE_RECOVERY}" != "xtrue" ]; then
    netbsd_entry "knetbsd"   "$k" true "-s"
    netbsd_entry "multiboot" "$k" true "-s"
  fi
done
