#!/bin/sh

# This script is used to setup the OE Build Envrionment
# Normally this is called as '. ./core-init-build-env <builddir>'

# Configuration variables
# ------------------------------------------------------------------------------
# We only set default value, so all configuration variable can be overriden
# if already set when sourcing this script

# On some shell, we can get the path of this script when sources. Otherwise we
# use the current directory as a fallback
if [ -z "$COREOS_ROOT" ]; then
    if [ -n "$BASH_SOURCE" ]; then
        COREOS_ROOT=$(dirname "$BASH_SOURCE")
    elif [ -n "$ZSH_NAME" ]; then
        COREOS_ROOT=$(dirname "$0")
    else
        COREOS_ROOT="$(pwd)"
    fi
fi

# Get a non relative path to the root directory
COREOS_ROOT=$(readlink -f "${COREOS_ROOT}")

# Set the path to bitbake, openembedded-core and the template directory
# All theses values can be overriden by the caller of coreos-init-build-env
BITBAKEDIR="${BITBAKEDIR:-${COREOS_ROOT}/bitbake}"
OEROOT="${OEROOT:-${COREOS_ROOT}/layers/openembedded-core}"
TEMPLATECONF="${TEMPLATECONF:-${COREOS_ROOT}/templates}"

# Sanity checks
# ------------------------------------------------------------------------------

# BITBAKEDIR, OEROOT, TEMPLATECONF and COREOS_ROOT can be overridden by our user
# so let's check that they have valid value

if [ ! -f "${COREOS_ROOT}/coreos-init-build-env" ]; then
    echo "Error: COREOS_ROOT ($COREOS_ROOT) isn't valid" >&2
    echo "If you are using CoreOS directly, try using this script from CoreOS root directory." >&2
    echo "If you are embedding coreos-init-build-env in another script, set COREOS_ROOT correctly there." >&2
    return 1
fi

if [ ! -d "$TEMPLATECONF" ]; then
    echo "Error: TEMPLATECONF (${TEMPLATECONF}) doesn't exist!" >&2
    echo "Please check your TEMPLATECONF configuration." >&2
    return 1
fi

if [ ! -f "${BITBAKEDIR}/bin/bitbake" ]; then
    echo "Error: BITBAKEDIR (${BITBAKEDIR}) isn't valid!" >&2
    echo "Please ensure all git submodule are fetched." >&2
    echo "And check your BITBAKEDIR configuration." >&2
    return 1
fi

if [ ! -f "${OEROOT}/oe-init-build-env" ]; then
    echo "Error: OEROOT (${OEROOT}) isn't valid!" >&2
    echo "Please ensure all git submodule are fetched." >&2
    echo "And check your OEROOT configuration." >&2
    return 1
fi

# Build environmnet setup
# ------------------------------------------------------------------------------

# Call the oe-init-build-env scripts of openembedded-core
. "${OEROOT}/oe-init-build-env" "${1:-$COREOS_ROOT/build}"

# Add the first argument of the function to the path
coreos_path_add() {
    # Make sure our paths are at the beginning of $PATH
    # Remove any existences of $1 from $PATH
    PATH=$(echo "$PATH" | sed -re "s#(^|:)$1(:|$)#\2#g;s#^:##")

    # Add $1 to the PATH
    PATH="$1:$PATH"
    export PATH
}

coreos_path_add "${COREOS_ROOT}/scripts"

# Add support for ##COREOS_LAYERSDIR## inside of bblayer template
coreos-bblayers-envsub COREOS_LAYERSDIR "${COREOS_ROOT}/layers"

# Generate the ${BUILDDIR}/key directory. The scripts doesn't generate anything it
# the directory already exist, so it's safe to call it everytime
coreos-keygen > /dev/null 2> /dev/null
