#!/bin/sh

# Use xml2-config or pkg-config to get the include directories for libxml-2.0
if xml2-config --version >/dev/null 2>&1; then
    libxml2_cflags=$(xml2-config --cflags)
    libxml2_libs=$(xml2-config --libs)
elif pkg-config --version >/dev/null 2>&1; then
    libxml2_cflags=$(pkg-config --cflags libxml-2.0)
    libxml2_libs=$(pkg-config --libs --static libxml-2.0)
else
    echo "Error: Neither xml2-config nor pkg-config is available"
    exit 1
fi

if [ -n "$libxml2_cflags" ]; then
    PKG_CFLAGS="$libxml2_cflags"
    echo "libxml2 include directories: $libxml2_cflags"
else
    echo "Error: libxml2 include directory not found"
    exit 1
fi

if [ -n "$libxml2_libs" ]; then
    PKG_LIBS="$libxml2_libs"
    echo "libxml2 library link flags: $libxml2_libs"
else
    echo "Error: libxml2 library link flags not found"
    exit 1
fi

echo "# Generated from Makevars.in, do not edit by hand" > src/Makevars.new
sed -e "s|@cflags@|$PKG_CFLAGS|" -e "s|@libs@|$PKG_LIBS|" src/Makevars.in >> src/Makevars.new
if [ ! -f src/Makevars ] || (which diff > /dev/null && ! diff -q src/Makevars src/Makevars.new); then
    cp -f src/Makevars.new src/Makevars
fi
rm -f src/Makevars.new
