set timeout=10

set env_file="/grubenv"
set oem_env_file="/grub_oem_env"
set custom_file="/grubcustom/custom.cfg"

if [ -f "${oem_env_file}" ]; then
  load_env -f "${oem_env_file}"
fi

# ${oem_label} is expected to be defined in ${oem_env_file}
search --no-floppy --set oem_blk --label "${oem_label}"

if [ -f "(${oem_blk})${env_file}" ]; then
  load_env -f "(${oem_blk})${env_file}"
fi

# Reload again ${oem_env_file} to prevent overwrites from ${env_file}
if [ -f "${oem_env_file}" ]; then
  load_env -f "${oem_env_file}"
fi

# Save default
if [ "${next_entry}" ]; then
  set default="${next_entry}"
  set selected_entry="${next_entry}"
  set next_entry=
  save_env -f "(${oem_blk})${env_file}" next_entry
else
  set default="${saved_entry}"
fi

## Display a default menu entry if set
if [ "${default_menu_entry}" ]; then
  set display_name="${default_menu_entry}"
else
  set display_name="Elemental"
fi

## Set a default fallback if set
if [ "${default_fallback}" ]; then
  set fallback="${default_fallback}"
else
  set fallback="0 recovery"
fi

## Include custom file if any
if [ -f "${custom_file}" ]; then
  source "${custom_file}"
fi

insmod all_video
insmod gfxterm
insmod loopback
insmod squash4

## Sets a loopback device volume for a given image
function set_loopdevice {
  set volume="loop"
  if [ -e (${volume}) ]; then 
    loopback -d ${volume}
  fi
  loopback ${volume} ${1}
}

## Sources bootargs from the current volume
function source_bootargs {
  source (${volume})/etc/cos/bootargs.cfg
  source (${volume})/etc/elemental/bootargs.cfg
}

## Defines the volume and image to boot from for active or passive boots
function set_volume {
  if [ "${snapshotter}" == "btrfs" ]; then
    set btrfs_relative_path="y"
    set volume="${root}"
    if [ -n "${1}" ]; then
      set img="@/.snapshots/${1}/snapshot"
      btrfs-mount-subvol ($root) / ${img}
    fi
  elif [ -z "${1}" ]; then
    set_loopdevice /.snapshots/active
  else
    set img="/.snapshots/${1}/snapshot.img"
    set_loopdevice ${img}
  fi
}

menuentry "${display_name}" --id active {
  set mode=active
  search --no-floppy --label --set=root ${state_label}
  set_volume
  source_bootargs
  linux (${volume})${kernel} ${kernelcmd} ${extra_cmdline} ${extra_active_cmdline}
  initrd (${volume})${initramfs}
}

for passive_snap in ${passive_snaps}; do
  menuentry "${display_name} (snapshot ${passive_snap})" --id passive${passive_snap} ${passive_snap} {
    set mode=passive
    search --no-floppy --label --set=root ${state_label}
    set_volume ${2}
    source_bootargs
    linux (${volume})${kernel} ${kernelcmd} ${extra_cmdline} ${extra_passive_cmdline}
    initrd (${volume})${initramfs}
  }
done

menuentry "${display_name} recovery" --id recovery {
  set mode=recovery
  search --no-floppy --label --set=root ${recovery_label}

  # Check the presence of the image and fallback to legacy path if not present
  set img=/boot/recovery.img
  if [ -f "${img}" ]; then
    source (${root})/boot/bootargs.cfg
    linux (${root})${kernel} ${kernelcmd} ${extra_cmdline} ${extra_recovery_cmdline}
    initrd (${root})${initramfs}
  else
    # Boot using legacy recovery system, everything is included in the loopback image.
    set img=/cOS/recovery.img
    set_loopdevice ${img}
    source_bootargs
    linux (${volume})${kernel} ${kernelcmd} ${extra_cmdline} ${extra_recovery_cmdline}
    initrd (${volume})${initramfs}
  fi
}
