#!/bin/sh

################################################################################
#
# Copyright (C) 2014 Cumulus Networks Inc. All rights reserved
#
################################################################################

# Access ONIE's grub-env and extract the ONIE boot mode

# This operation requires root privileges
if [ $(id -u) -ne 0 ] ; then
    echo "ERROR: This command requires root privileges"
    exit 1
fi

. /usr/lib/cumulus/onie/onie-tools-common

cl_mount_onie_tools || {
    echo "ERROR: Mounting ONIE tools failed" >&2
    exit 1
}

if [ -x "$onie_tool_dir/onie-boot-mode" ] ; then
    output="$($onie_tool_dir/onie-boot-mode $@)" || {
        echo "Unable to call ONIE onie-boot-mode" >&2
        exit 1
    }
else
    echo "ERROR: onie-boot-mode utility is unavailable." >&2
    echo "The ONIE installation is incomplete." >&2
    exit 1
fi

cl_umount_onie_tools

# Santitize onie-boot-mode output.  It would be better if that command
# were not so chatty.
output=${output#"Default boot: ONIE mode: "}

echo -n "$output"
