#!/bin/bash
#
# Find a native sysroot to use - either from an in-tree OE build or
# from a toolchain installation. It then ensures the variable
# $OECORE_NATIVE_SYSROOT is set to the sysroot's base directory, and sets
# $PSEUDO to the path of the pseudo binary.
#
# This script is intended to be run within other scripts by source'ing
# it, e.g:
#
# SYSROOT_SETUP_SCRIPT=`which oe-find-native-sysroot`
# . $SYSROOT_SETUP_SCRIPT <recipe>
#
# This script will terminate execution of your calling program unless
# you set a variable $SKIP_STRICT_SYSROOT_CHECK to a non-empty string
# beforehand.
#
# Copyright (c) 2010 Linux Foundation
#
# SPDX-License-Identifier: GPL-2.0-only
#

if [ "$1" = '--help' -o "$1" = '-h' -o $# -ne 1 ] ; then
    echo 'Usage: oe-find-native-sysroot <recipe> [-h|--help]'
    echo ''
    echo 'OpenEmbedded find-native-sysroot - helper script to set'
    echo 'environment variables OECORE_NATIVE_SYSROOT and PSEUDO'
    echo 'to the path of the native sysroot directory and pseudo'
    echo 'executable binary'
    echo ''
    echo 'options:'
    echo '  recipe              its STAGING_DIR_NATIVE is used as native sysroot'
    echo '  -h, --help          show this help message and exit'
    echo ''
    exit 2
fi

# Global vars
BITBAKE_E=""
set_oe_native_sysroot(){
    echo "Running bitbake -e $1"
    BITBAKE_E="`bitbake -e $1`"
    OECORE_NATIVE_SYSROOT=`echo "$BITBAKE_E" | grep ^STAGING_DIR_NATIVE= | cut -d '"' -f2`

    if [ "x$OECORE_NATIVE_SYSROOT" = "x" ]; then
        # This indicates that there was an error running bitbake -e that
        # the user needs to be informed of
        echo "There was an error running bitbake to determine STAGING_DIR_NATIVE"
        echo "Here is the output from bitbake -e $1"
        echo $BITBAKE_E
        exit 1
    fi
}

if [ "x$OECORE_NATIVE_SYSROOT" = "x" ]; then
    BITBAKE=`which bitbake 2> /dev/null`
    if [ "x$BITBAKE" != "x" ]; then
        if [ "$UID" = "0" ]; then
            # Root cannot run bitbake unless sanity checking is disabled
            if [ ! -d "./conf" ]; then
                echo "Error: root cannot run bitbake by default, and I cannot find a ./conf directory to be able to disable sanity checking"
                exit 1
            fi
            touch conf/sanity.conf
            set_oe_native_sysroot $1
            rm -f conf/sanity.conf
        else
            set_oe_native_sysroot $1
        fi
    else
        echo "Error: Unable to locate bitbake command."
        echo "Did you forget to source the build environment setup script?"

        if [ -z "$SKIP_STRICT_SYSROOT_CHECK" ]; then
            exit 1
        fi
    fi
fi

if [ ! -e "$OECORE_NATIVE_SYSROOT/" ]; then
    echo "Error: $OECORE_NATIVE_SYSROOT doesn't exist."

    if [ "x$OECORE_DISTRO_VERSION" = "x" ]; then
        if [[ $1 =~ .*native.* ]]; then
            echo "Have you run 'bitbake $1 -caddto_recipe_sysroot'?"
        else
            echo "Have you run 'bitbake $1 '?"
        fi
    else
        echo "This shouldn't happen - something is wrong with your toolchain installation"
    fi

    if [ -z "$SKIP_STRICT_SYSROOT_CHECK" ]; then
        exit 1
    fi
fi

# Set up pseudo command
pseudo="$OECORE_NATIVE_SYSROOT/usr/bin/pseudo"
if [ -e "$pseudo" ]; then
    echo "PSEUDO=$pseudo"
    PSEUDO="$pseudo"
fi