#!/bin/sh

# Determine if pkg-config exists and knows about libpq, otherwise
# fall back on pg_config to determine compiler and linker flags.
if pkg-config --exists libpq >/dev/null 2>/dev/null; then
   CFLAGS="`pkg-config --cflags libpq`"
   LDFLAGS="`pkg-config --libs libpq`"
else
   CFLAGS="-I`pg_config --includedir`"
   LDFLAGS="-L`pg_config --libdir` -Wl,-rpath `pg_config --libdir` -lpq"
fi

known_features="escape-identifier connectdb-params diag-query-position diag-schema-info"

feature_flags=""
for feature in $known_features; do
    if "$CHICKEN_CSC" -C "$CFLAGS" -L "$LDFLAGS" "feature-tests/$feature.c" >/dev/null 2>/dev/null; then
	feature_flags="$feature_flags -Dhas-$feature"
    fi
    rm -f feature-tests/$feature.o feature-tests/$feature
done

"$CHICKEN_CSC" $feature_flags -C "$CFLAGS" -L "$LDFLAGS" "$@"