#!/bin/bash
#

mkl_require cxx
mkl_require lib
mkl_require pic
mkl_require atomics must pass
mkl_require good_cflags
mkl_require socket

# Generate version variables from rdkafka.h hex version define
# so we can use it as string version when generating a pkg-config file.

verdef=$(grep '^#define  *RD_KAFKA_VERSION  *0x' src/rdkafka.h | sed 's/^#define  *RD_KAFKA_VERSION  *\(0x[a-f0-9]*\)\.*$/\1/')
mkl_require parseversion hex2str "%d.%d.%d" "$verdef" RDKAFKA_VERSION_STR

mkl_require gen-pkg-config "rdkafka" "The Apache Kafka C/C++ library" \
    "Full Apache Kafka 0.8 protocol support, including producer and consumer" \
    "\$RDKAFKA_VERSION_STR"

function checks {

    # required libs
    mkl_lib_check "libpthread" "" fail CC "-lpthread"
    mkl_lib_check "zlib" "" fail CC "-lz"

    # -lrt is needed on linux for clock_gettime: link it if it exists.
    mkl_lib_check "librt" "" cont CC "-lrt"

    # Older g++ (<=4.1?) gives invalid warnings for the C++ code.
    mkl_mkvar_append CXXFLAGS CXXFLAGS "-Wno-non-virtual-dtor"

    # Required on SunOS
    if [[ $MKL_DISTRO == "SunOS" ]]; then
	mkl_mkvar_append CPPFLAGS CPPFLAGS "-D_POSIX_PTHREAD_SEMANTICS"
    fi

    # Figure out what tool to use for dumping public symbols.
    # We rely on configure.cc setting up $NM if it exists.
    if mkl_env_check "nm" "" cont "NM" ; then
	# nm by future mk var
	if [[ $MKL_DISTRO == "osx" ]]; then
	    mkl_mkvar_set SYMDUMPER SYMDUMPER '$(NM) -g'
	else
	    mkl_mkvar_set SYMDUMPER SYMDUMPER '$(NM) -D'
	fi
    else
	# Fake symdumper
	mkl_mkvar_set SYMDUMPER SYMDUMPER 'echo'
    fi

    # The linker-script generator (lds-gen.pl) requires perl
    if [[ $WITH_LDS == y ]]; then
        if ! mkl_command_check perl "HAVE_PERL" "disable" "perl -v"; then
            mkl_err "disabling linker-script since perl is not available"
            mkl_mkvar_set WITH_LDS WITH_LDS "n"
        fi
    fi
}

