#!/bin/sh

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

# Display the ONIE version information if available

# 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"
    exit 1
}

if [ -x "$onie_tool_dir/onie-version" ] ; then
    # First try to use the onie-version command that comes with the
    # ONIE tools package from the ONIE distribution.
    $onie_tool_dir/onie-version
else
    # Perhaps older version of ONIE.  Try to find the info manually.
    onie_grub_dir="${onie_boot_mnt}/onie/grub"
    grub_machine_cfg="${onie_grub_dir}/grub-machine.cfg"

    if [ -r "$grub_machine_cfg" ] ; then
        . "$grub_machine_cfg"
        grep = $grub_machine_cfg | sed -e 's/onie_//' -e 's/=.*$//' | while read var ; do
            eval val='$'onie_$var
            printf "%-20s: %s\n" "ONIE ${var}" "$val"
        done
    else
        echo "Warning: ONIE version information not available" > /dev/stderr
    fi
fi

cl_umount_onie_tools
