#!/bin/bash

gen-pkgconf-flags()
{
  local var="$1"
  local pc="$2"

  if ! pkg-config --exists "${pc}"; then
    echo "Error: Required package '${pc}' not found" >&2
    exit 1
  fi

  echo "export CFLAGS_${var} LDLIBS_${var}"
  echo "CFLAGS_${var} := $(pkg-config --cflags "${pc}")"
  echo "LDLIBS_${var} := $(pkg-config --libs "${pc}")"
}

if [ -z "${top_srcdir}" ]; then
  top_srcdir=.
fi

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

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

  git_br_commit="git-$(git rev-parse --abbrev-ref --symbolic-full-name HEAD 2> /dev/null)-$(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

CFLAGS_DEFAULT=
CPPFLAGS_DEFAULT=
LDFLAGS_DEFAULT=
if command -v dpkg-buildflags >/dev/null; then
  # support http://wiki.debian.org/Hardening for >=wheezy
  CFLAGS_DEFAULT=$(dpkg-buildflags --get CFLAGS)
  CPPFLAGS_DEFAULT=$(dpkg-buildflags --get CPPFLAGS)
  LDFLAGS_DEFAULT=$(dpkg-buildflags --get LDFLAGS)
fi
CFLAGS_DEFAULT+=" -O3 -flto=auto -ffat-lto-objects"
LDFLAGS_DEFAULT+=" -flto=auto -fuse-linker-plugin"

echo "export CFLAGS_DEFAULT := ${CFLAGS_DEFAULT}"
echo "export CPPFLAGS_DEFAULT := ${CPPFLAGS_DEFAULT}"
echo "export LDFLAGS_DEFAULT := ${LDFLAGS_DEFAULT}"

gen-pkgconf-flags GLIB                glib-2.0
gen-pkgconf-flags GTHREAD             gthread-2.0
gen-pkgconf-flags JSON_GLIB           json-glib-1.0
gen-pkgconf-flags LIBAVCODEC          libavcodec
gen-pkgconf-flags LIBAVFILTER         libavfilter
gen-pkgconf-flags LIBAVFORMAT         libavformat
gen-pkgconf-flags LIBAVUTIL           libavutil
gen-pkgconf-flags LIBCURL             libcurl
gen-pkgconf-flags LIBCRYPTO           libcrypto
gen-pkgconf-flags LIBEVENT            libevent_pthreads
gen-pkgconf-flags LIBMNL              libmnl
gen-pkgconf-flags LIBNFTNL            libnftnl
gen-pkgconf-flags LIBPCRE             libpcre2-8
echo "CFLAGS_LIBPCRE += -DPCRE2_CODE_UNIT_WIDTH=8"
gen-pkgconf-flags LIBSWRESAMPLE       libswresample
gen-pkgconf-flags LIBWEBSOCKETS       libwebsockets
gen-pkgconf-flags LIBZ                zlib
gen-pkgconf-flags NCURSESW            ncursesw
gen-pkgconf-flags OPENSSL             openssl
gen-pkgconf-flags OPUS                opus
gen-pkgconf-flags SPANDSP             spandsp
gen-pkgconf-flags LIBJWT              libjwt
gen-pkgconf-flags LIBHIREDIS          hiredis
gen-pkgconf-flags LIBPCAP             libpcap

if pkg-config --exists libmosquitto; then
  gen-pkgconf-flags MQTT              libmosquitto
  echo "CFLAGS_MQTT += -DHAVE_MQTT"
fi

# look for libsystemd
if pkg-config --exists libsystemd; then
  gen-pkgconf-flags LIBSYSTEMD        libsystemd
  echo "CFLAGS_LIBSYSTEMD += -DHAVE_LIBSYSTEMD"
fi

# look for liburing
if pkg-config --atleast-version=2.3 liburing; then
  gen-pkgconf-flags LIBURING liburing
  echo "CFLAGS_LIBURING += -DHAVE_LIBURING"
fi

# look for libiptc
if pkg-config libiptc; then
  gen-pkgconf-flags LIBIPTC libiptc
  echo "CFLAGS_LIBIPTC += -DHAVE_LIBIPTC"
fi

# alternative MySQL/MariaDB
if pkg-config --exists libmariadb; then
  gen-pkgconf-flags MYSQL libmariadb
else
  gen-pkgconf-flags MYSQL mysqlclient
fi
