#!/bin/bash

################################################################################
# Copyright 2026 ModalAI Inc.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# 4. The Software is licensed to be used solely in conjunction with devices
#    provided by ModalAI Inc.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
################################################################################

# qcs6490 doesn't have the voxl_platform_module yet, so detect differently
if cat /sys/devices/soc0/machine | grep QCS6490 -q; then
    echo "M0197"
    exit 0
fi

# This is the new hardware ID method based on a kernel module introduced in
# system image 1.7.4 as part of an effort to use the same rootfs across all
# hardware platforms
if [ -f "/sys/module/voxl_platform_mod/parameters/machine" ]; then

    machine=$(cat "/sys/module/voxl_platform_mod/parameters/machine")
    if [[ $machine == 1 ]]; then
        echo "M0054" ## VOXL2, M0054, M0154
        exit 0
    elif [[ $machine == 2 ]]; then
        echo "M0104" ## VOXL2 Mini, M0104, M0204
        exit 0
    elif [[ $machine == 3 ]]; then
        echo "M0205" ## VOXL2 VRX, M0205
        exit 0
    elif [[ $machine == 0 ]]; then
        echo "M0052" ## RB5
        exit 0
    else
        echo "ERROR unknown machine type: $machine"
        exit 1
    fi
fi


# old method pulling from the file system
if [ -f "/etc/version" ]; then

    ver_file=$(cat "/etc/version")
    if [[ $ver_file == *"M0054"* ]]; then
        echo "M0054"
        exit 0
    elif [[ $ver_file == *"M0104"* ]]; then
        echo "M0104"
        exit 0
    elif [[ $ver_file == *"M0052"* ]]; then
        echo "M0052"
        exit 0
    else
        echo "ERROR unknown platform type: $ver_file"
        exit 1
    fi
fi

# old old method, should never get to this point.
FIRMWARE_META="/firmware/verinfo/ver_info.txt"
PLATFORM_META=".ModalAI_Metabuild_Info.Platform"

if [ -f $FIRMWARE_META ]; then
    PLATFORM_TYPE=$(jq -r $PLATFORM_META $FIRMWARE_META)
    echo $PLATFORM_TYPE
    exit 0
fi

echo "failed to detect hardware platform"
exit -1

