###########################################################################
# Copyright (C) 2015-2021 IoT.bzh Company
#
# Author: Clément Bénier <clement.benier@iot.bzh>
# Author: Romain Forlot <romain.forlot@iot.bzh>
#
# $RP_BEGIN_LICENSE$
# Commercial License Usage
#  Licensees holding valid commercial IoT.bzh licenses may use this file in
#  accordance with the commercial license agreement provided with the
#  Software or, alternatively, in accordance with the terms contained in
#  a written agreement between you and The IoT.bzh Company. For licensing terms
#  and conditions see https://www.iot.bzh/terms-conditions. For further
#  information use the contact form at https://www.iot.bzh/contact.
#
# GNU General Public License Usage
#  Alternatively, this file may be used under the terms of the GNU General
#  Public license version 3. This license is as published by the Free Software
#  Foundation and appearing in the file LICENSE.GPLv3 included in the packaging
#  of this file. Please review the following information to ensure the GNU
#  General Public License requirements will be met
#  https://www.gnu.org/licenses/gpl-3.0.html.
# $RP_END_LICENSE$
###########################################################################

#---------- cross profile options Start ---------
#rpm values
_usr=$(rpm --target @CROSS_TARGET_CPU@ -E %{_usr})
_libdir=$(rpm --target @CROSS_TARGET_CPU@ -E %{_libdir})
_includedir=$(rpm --target @CROSS_TARGET_CPU@ -E %{_includedir})
_unitdir=$(rpm --target @CROSS_TARGET_CPU@ -E %{_unitdir})
_bindir=$(rpm --target @CROSS_TARGET_CPU@ -E %{_bindir})

export VENDOR="@VENDOR@"
export TARGET="@TARGET@"
export CROSS="@CROSS@"
export CROSS_ARCH="@CROSS_ARCH@"
export CROSS_LIB="@CROSS_LIB@"
# Default Fedora Compiler sysroot configured.
# No need to specify '--sysroot' for them :)
export CROSS_ROOT="@CROSS_ROOT@"
export LIBSTDC_VERSION=$(rpm -q --qf '%{VERSION}' libstdc++-devel | cut -d'.' -f1)

export GCCVERSION=$(${TARGET}-gcc -dumpversion)

# autotools partz
export host_alias=@TARGET@
export target_alias=@TARGET@
export build_alias=@BUILD@
export with_systemdsystemunitdir=${_unitdir}
export with_systemdunitdir=${_unitdir}

export PATH=/usr/bin/cross-wrappers:$PATH
export BUILD_CC="@BUILD_CC@"
export BUILD_CXX="@BUILD_CXX@"
export BUILD_CPP="@BUILD_CPP@"
export BUILD_AS="@BUILD_AS@"
export BUILD_LD="@BUILD_LD@"
export BUILD_AR="@BUILD_AR@"
export BUILD_NM="@BUILD_NM@"
export BUILD_OBJCOPY="@BUILD_OBJCOPY@"
export BUILD_OBJDUMP="@BUILD_OBJDUMP@"
export BUILD_RANLIB="@BUILD_RANLIB@"
export BUILD_STRIP="@BUILD_STRIP@"
export TARGET_CC="@CROSS_CC@"
export TARGET_CXX="@CROSS_CXX@"
# Keep <target>-cpp as the default preprocessor compiler because it is the most
# used among packages. Better to fix the wrong one that get the error:
# 'too many input files' by set this env var inside the spec file to point to
# '<target>-gcc -E'
export TARGET_CPP="@CROSS_CPP@"
export TARGET_AS="@CROSS_AS@"
export TARGET_LD="@CROSS_LD@"
export TARGET_AR="@CROSS_AR@"
export TARGET_NM="@CROSS_NM@"
export TARGET_OBJCOPY="@CROSS_OBJCOPY@"
export TARGET_OBJDUMP="@CROSS_OBJDUMP@"
export TARGET_RANLIB="@CROSS_RANLIB@"
export TARGET_STRIP="@CROSS_STRIP@"
export CC_FOR_BUILD="@CROSS_CC@"
export CXX_FOR_BUILD="@CROSS_CXX@"
export CPP_FOR_BUILD="@CROSS_CPP@"
export CC="@CROSS_CC@"
export CXX="@CROSS_CXX@"
# Keep <target>-cpp as the default preprocessor compiler because it is the most
# used among packages. Better to fix the wrong one that get the error:
# 'too many input files' by set this env var inside the spec file to point to
# '<target>-gcc -E'
export CPP="@CROSS_CPP@"
export AS="@CROSS_AS@ "
export LD="@CROSS_LD@ "
export AR="@CROSS_AR@"
export NM="@CROSS_NM@"
export OBJCOPY="@CROSS_OBJCOPY@"
export OBJDUMP="@CROSS_OBJDUMP@"
export RANLIB="@CROSS_RANLIB@"
export STRIP="@CROSS_STRIP@"

# Not sure those are really needed often have to empty them to get the build
# processing correctly because of the '-specs=redhat-hardened-cc1' or
# '-specs=redhat-hardened-ld' that sometimes are disbled but thoses env var
# re-enable them so it fails the build.
export CONFIGURE_FLAGS="@CONFIGURE_FLAGS@"
export CPLUS_INCLUDE_PATH="@CPLUS_INCLUDE_PATH@"
export CFLAGS="@CFLAGS@"
export CXXFLAGS="@CXXFLAGS@"
export FFLAGS="@FFLAGS@"
export FCFLAGS="@FCFLAGS@"
export LDFLAGS="@LDFLAGS@"
export CPPFLAGS="@CPPFLAGS@"
export KCFLAGS="@KCFLAGS@"
export F9X="@F9X@"

#QEMU
export QEMU_LD_PREFIX=${CROSS_ROOT}

# PYTHON
export PYTHONHOME=${_usr}
export PYTHON=$(rpm --target @CROSS_TARGET_CPU@ -E %{__python3})

# SWIG
export SWIG_FEATURES="-I${CROSS_ROOT}/usr/include/python3.6m"

# Wrappers functions to get some compile option generically
function pg_config() {
    if [ "$1" == "--libdir" ]; then
        echo "${CROSS_ROOT}${_libdir}"
    elif [ "$1" == "--pkglibdir" ]; then
        echo "${CROSS_ROOT}${_libdir}/pgsql"
    elif [ "$1" == "--includedir" ]; then
        echo "${CROSS_ROOT}${_includedir}"
    elif [ "$1" == "--pkgincludedir" ]; then
        echo "${CROSS_ROOT}${_includedir}/pgsql"
    else
        /usr/bin/pg_config "$@"
    fi
}

function mysql_config() {
    if [ "$1" == "--include" ]; then
        pkg-config libmariadb --cflags-only-I
    else
        pkg-config libmariadb "$@"
    fi
}

function autoconf() {
    /usr/bin/autoconf "$@"
    for f in $(find $HOME/build/BUILD -type f -name configure); do
        /usr/bin/sed -i s:cross_compiling=yes:cross_compiling=no: $f
    done
}

export -f pg_config
export -f mysql_config
export -f autoconf

#TCL
export TCL_PREFIX="@TCL_PREFIX@"

#----------  cross profile options End ---------
