#!/bin/sh

# Use pkg-config or xml2-config to get the include directory for libxml-2.0
xml2_include_dir=""
if which xml2-config >/dev/null 2>&1; then
    xml2_include_dir=$(xml2-config --cflags | sed 's/^-I//')
elif which pkg-config >/dev/null 2>&1; then
    xml2_include_dir=$(pkg-config --cflags libxml-2.0 | sed 's/^-I//')
else
    echo "Error: Neither xml2-config nor pkg-config is available"
    exit 1
fi

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

echo "# Generated from Makevars.in, do not edit by hand" > src/Makevars.new
sed -e "s|@cflags@|$PKG_CFLAGS|" 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
