
# Copyright (C) Igor Sysoev
# Copyright (C) NGINX, Inc.


printf "checking for $njs_feature ..."

cat << END >> $NJS_AUTOCONF_ERR
----------------------------------------
checking for $njs_feature
END


njs_found=no
njs_feature_value=
njs_feature_inc_path=

if test -n "$njs_feature_incs"; then
    case "$njs_feature_incs" in
        -*)
            njs_feature_inc_path="$njs_feature_incs"
        ;;

        *)
            for njs_temp in $njs_feature_incs; do
                njs_feature_inc_path="$njs_feature_inc_path -I $njs_temp"
            done
        ;;
    esac
fi


cat << END > $NJS_AUTOTEST.c
$njs_feature_test
END


njs_test="$CC $CFLAGS $NJS_CFLAGS $NJS_CC_OPT $NJS_TEST_CFLAGS \
          $njs_feature_inc_path -o $NJS_AUTOTEST $NJS_AUTOTEST.c \
          $NJS_LD_OPT $NJS_TEST_LIBS $njs_feature_libs"

# /bin/sh -c "(...)" is to intercept "Killed", "Abort trap",
# "Segmentation fault", or other shell messages.
# "|| true" is to bypass "set -e" setting.

/bin/sh -c "($njs_test || true)" >> $NJS_AUTOCONF_ERR 2>&1


if [ -x $NJS_AUTOTEST ]; then

    case "$njs_feature_run" in

        value)
            if /bin/sh -c "($NJS_AUTOTEST)" >> $NJS_AUTOCONF_ERR 2>&1; then
                echo >> $NJS_AUTOCONF_ERR
                njs_found=yes
                njs_feature_value=`$NJS_AUTOTEST`
                echo " $njs_feature_value"
                if [ -n "$njs_feature_name" ]; then
                    cat << END >> $NJS_AUTO_CONFIG_H

#ifndef $njs_feature_name
#define $njs_feature_name  $njs_feature_value
#endif

END
                fi
            else
                echo " not found"
            fi
        ;;

        yes)
            if /bin/sh -c "($NJS_AUTOTEST)" >> $NJS_AUTOCONF_ERR 2>&1; then
                echo " found"
                njs_found=yes
                cat << END >> $NJS_AUTO_CONFIG_H

#ifndef $njs_feature_name
#define $njs_feature_name  1
#endif

END
            else
                echo " found but is not working"
            fi
        ;;

        *)
            echo " found"
            njs_found=yes
            cat << END >> $NJS_AUTO_CONFIG_H

#ifndef $njs_feature_name
#define $njs_feature_name  1
#endif

END
        ;;
    esac

else
    echo " not found"

    echo "----------"    >> $NJS_AUTOCONF_ERR
    cat $NJS_AUTOTEST.c       >> $NJS_AUTOCONF_ERR
    echo "----------"    >> $NJS_AUTOCONF_ERR
    echo $njs_test       >> $NJS_AUTOCONF_ERR
    echo "----------"    >> $NJS_AUTOCONF_ERR
fi

rm -rf $NJS_AUTOTEST*
