#!/bin/bash

if [ -z "${M}" ]; then
  M=$(pwd)
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  if [ -f .release-version ]; then
    RTPENGINE_VERSION=$(<.release-version)
  elif [ -f ../.release-version ]; then
    RTPENGINE_VERSION=$(<../.release-version)
  fi
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  have_dpkg_parsechangelog=no
  if command -v dpkg-parsechangelog >/dev/null; then
    have_dpkg_parsechangelog=yes
  fi
  if [ -f "${M}/../debian/changelog" ]; then
    deb_changelog="${M}/../debian/changelog"
  else
    deb_changelog="${M}/debian/changelog"
  fi
  if [ "${have_dpkg_parsechangelog}" = yes ]; then
    deb_version="$(dpkg-parsechangelog -l"${deb_changelog}" | awk '/^Version: / {print $2}')"
    RTPENGINE_VERSION="${deb_version}"
  fi

  git_br_commit="git-$(cd "${M}" && git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(cd "${M}" && git rev-parse --short HEAD 2> /dev/null)"

  if [ "${git_br_commit}" != "git--" ]; then
    if [ -n "${RTPENGINE_VERSION}" ]; then
      RTPENGINE_VERSION+=" "
    fi
    RTPENGINE_VERSION+="${git_br_commit}"
  fi
fi

if [ -z "${RTPENGINE_VERSION}" ]; then
  RTPENGINE_VERSION="undefined"
fi

if [ -n "${RTPENGINE_VERSION}" ]; then
  echo "RTPENGINE_VERSION := ${RTPENGINE_VERSION}"
fi

# Compile test: detect nft_expr_ops.validate callback signature.
# Distribution kernels (e.g. Ubuntu) may backport the 2-param validate
# callback (mainline commit eaf9b2c875ec, merged in 6.12) to earlier
# kernel versions without changing LINUX_VERSION_CODE. A compile test
# is the only reliable way to detect the actual API.
#
# The test tries to assign a 3-param function to .validate. If it
# compiles, the old API is present and we set the flag. If it fails
# (incompatible pointer types), the kernel has the new 2-param version.

# KERNELRELEASE is set by kbuild to the target kernel version, which
# may differ from the running kernel during DKMS cross-kernel builds.
KSRC="${KSRC:-/lib/modules/${KERNELRELEASE:-$(uname -r)}/build}"

if [ -d "${KSRC}" ]; then
  nft_test_dir=$(mktemp -d)
  cat > "${nft_test_dir}/nft_test.c" <<'TESTEOF'
#include <linux/module.h>
#include <net/netfilter/nf_tables.h>
static int test_validate(const struct nft_ctx *ctx, const struct nft_expr *expr,
    const struct nft_data **data) { return 0; }
static struct nft_expr_ops __attribute__((unused)) test_ops = {
    .validate = test_validate };
MODULE_LICENSE("GPL");
TESTEOF
  echo "obj-m := nft_test.o" > "${nft_test_dir}/Makefile"

  # Use env -i to prevent kbuild state (KBUILD_EXTMOD, M, MAKEFLAGS, etc.)
  # leaking from an outer kbuild invocation into the compile test.
  if env -i PATH="$PATH" make -j1 -C "${KSRC}" M="${nft_test_dir}" modules >/dev/null 2>&1; then
    echo "ccflags-y += -DNFT_EXPR_OPS_VALIDATE_HAS_DATA"
  fi

  rm -rf "${nft_test_dir}"
fi
