#!/bin/bash

# NOTE: this file is similar to the sbx-install/50_ribbon file but does differ
# slightly in order to handle the ISO image

. "$pkgdatadir/grub-mkconfig_lib"

# we have a separate boot partition from the root partition we
# are in.  We need to make sure grub finds/uses that partition.
# Rather than hardcode the name, determine it on the fly.
rbbnBootDev=`mount | grep "/boot" | awk '{print $1}'`


# provide a function to generate custom menu items, similar to how grub
# generates the default items when it probes for kernel versions.
sonus_linux_entry() {
  # whether or not this is the iso entry
  iso_entry=$1

  # the full kernel version
  kernel_version="$2"

  # location of the root device
  root_device="$3"

  # extra command line args
  kernel_args="$4"

  # title
  title="$5"

  # menu id
  id="$5"

  # image names
  if [ $iso_entry -eq 0 ]; then
      kernel_name="vmlinuz-${kernel_version}"
      initrd_name="initrd.img-${kernel_version}"
  else
      kernel_name="iso_vmlinuz"
      initrd_name="iso_initrd.gz"
  fi

  # set up menu ID and load user message
  if [ -n "$kernel_version" ]; then
    menu_id="${id}"
    load_msg="${kernel_version}"
    kernel_args="${kernel_args} ${GRUB_CMDLINE_LINUX} ${GRUB_CMDLINE_LINUX_DEFAULT}"
  else
    menu_id="Ribbon ISO"
    load_msg="Installer"
  fi

  cat <<EOF
menuentry '$title' --class debian --class gnu-linux --class gnu --class os $menuentry_id_option '${id}' {
	load_video
	insmod gzio

$(prepare_grub_to_access_device ${rbbnBootDev} | grub_add_tab)

	echo 'Loading Linux ${load_msg} ...'
	linux /${kernel_name} root=${root_device} ${kernel_args}
	echo 'Loading initial ramdisk ...'
	initrd /${initrd_name}
}

EOF
}


# kernel arguments for running the installer for connexip ISO install
isoArgs="locale=en_US preseed/file=/cdrom/simple-cdd/default.preseed debian-installer/locale=en_US keymap=us iso-scan/copy_iso_to_ram=true simple-cdd/profiles=connexip quiet"

# add menu entries
# note: the first 2 entries get updated by setupSystem depending on what is on
# the system being ISOd. if the 50_ribbon_sda1 file doesn't exist, the entries
# get deleted leaving just the ISO option.
sonus_linux_entry 0 CURR_KERNEL CURR_DEV CURR_OPTS CURR_TITLE "single-user"
sonus_linux_entry 0 CURR_KERNEL CURR_DEV CURR_OPTS CURR_TITLE "sonus-curr-ver"
sonus_linux_entry 1 "" "" "$isoArgs" "Install ISO" "isosbx-install"
