#!/bin/sh

# This program prints to stdout the canonical platform name in the
# format <vendor>,<machine> without any newlines.

# This program *must* work correctly in the following contexts:
#
# - from ONIE context
# - from Cumulus Linux initramfs context (before we know the platform)
# - from regular Cumulus Linux context

model=

arch="$(uname -m)"
if [ "$arch" = "ppc" ] ; then
    # Check the device-tree.  This is the standard approach on
    # legacy PowerPC platforms.
    if [ -r /proc/device-tree/model ] ; then
        # This implies the device tree "model" must be in sync between
        # ONIE and Cumulus Linux.
        model=$(sed -e 's/ //' /proc/device-tree/model)
    fi
else
    # On x86 and arm use the platform string from ONIE.
    #
    # In ONIE context use the output of "onie-sysinfo -p".
    #
    # In Cumulus Linux context the kernel command line variable
    # "cl_platform" contains the ONIE platform string.  While running
    # in ONIE context the Cumulus installer used the output of
    # "onie-sysinfo -p" to set this variable.

    if [ -r /proc/cmdline ] ; then
        # Parse kernel command line options
        for x in $(cat /proc/cmdline); do
            case "$x" in
                cl_platform=*)
                    cl_platform=${x#cl_platform=}
                    break
                    ;;
            esac
        done
    fi

    # Handle SimX platform here which is a derivative of VX
    if [ "$cl_platform" = "cumulus_vx" ] ; then
        for x in $(cat /sys/class/dmi/id/product_name); do
            case "$x" in
                MSN2700)
                    cl_platform="mlnx_simx_msn2700"
                    break
                    ;;
                MSN3420)
                    cl_platform="mlnx_simx_msn3420"
                    break
                    ;;
                MSN3700)
                    cl_platform="mlnx_simx_msn3700"
                    break
                    ;;
                MSN4600-VS2F)
                    cl_platform="mlnx_simx_msn4600"
                    break
                    ;;
                MSN4600-CS2F)
                    cl_platform="mlnx_simx_msn4600c"
                    break
                    ;;
                MSN4700)
                    cl_platform="mlnx_simx_msn4700"
                    break
                    ;;
                MSN4800)
                    cl_platform="mlnx_simx_msn4800"
                    break
                    ;;
                MSN5700-XXXX)
                    cl_platform="mlnx_simx_msn5700"
                    break
                    ;;
            esac
        done
    fi

    if [ -z "$cl_platform" ] ; then
        # In ONIE context
        if [ -x /bin/onie-sysinfo ] ; then
            # get cl_platform from ONIE
	    cl_platform=$(onie-sysinfo -p)
        else
            echo "ERROR:  Unable to determine platform for ARCH: $(uname -m)" > /dev/stderr
            exit 1
        fi
    fi
    # cl_platform has the format: <arch>-<vendor>_<machine>-<revision>
    # Keep the <vendor>_<machine> substring.
    tmp_str=${cl_platform#*-}
    tmp_str=${tmp_str%-*}
    # <vendor> does not contain "_", but <machine> might.
    vendor=${tmp_str%%_*}
    machine=${tmp_str#*_}
    model="${vendor},${machine}"
fi

echo -n "$model"

# Local Variables:
# mode: shell-script
# eval: (sh-set-shell "/bin/sh" t nil)
# End:
