# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=pulseaudio
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=17.0
pkgrel=4
pkgdesc="A featureful, general-purpose sound server (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.freedesktop.org/wiki/Software/PulseAudio/"
msys2_references=(
  "cpe: cpe:/a:pulseaudio:pulseaudio"
)
license=('spdx:GPL-2.0-or-later' 'spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-gdbm"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libsndfile"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libltdl"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-orc"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-speexdsp")
makedepends=("${MINGW_PACKAGE_PREFIX}-check"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "perl-XML-Parser")
source=("https://freedesktop.org/software/pulseaudio/releases/pulseaudio-${pkgver}.tar.xz"
        "0001-remove-assert-pid_t-mismatch.patch"
        "0002-port-to-pcre2.patch")
sha256sums=('053794d6671a3e397d849e478a80b82a63cb9d8ca296bd35b73317bb5ceb87b5'
            'fa00d79f0cf18999cfee2b7e479319ac819f09a46a00ce8b3906b1c8c417416a'
            '07c7d3506bd308b3f09aaae02902b457a55d9c4ccd6ac105056137ac56c659ea')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-remove-assert-pid_t-mismatch.patch"

  # port to pcre2 and force ignore regex.h from libtre
  patch -p1 -i "${srcdir}/0002-port-to-pcre2.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  if [[ $MINGW_PACKAGE_PREFIX != *-clang-* ]]; then
    CFLAGS+=" -Wno-incompatible-pointer-types"
  fi

  # _WIN32_WINNT < 0x0600 is checked before inet_ntop

  CPPFLAGS+=" -D_WIN32_WINNT=0x0603" \
  MSYS2_ARG_CONV_EXCL="--prefix=;-Dmodlibexecdir=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    -Dmodlibexecdir="${MINGW_PREFIX}/bin" \
    --wrap-mode=nodownload \
    --default-library=both \
    "${_extra_config[@]}" \
    -Ddbus=disabled \
    -Ddatabase=gdbm \
    -Dtests=false \
    -Dwebrtc-aec=disabled \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/meson.exe compile
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson.exe install

  # Workaround: Move import libraies to libdir
  mv "${pkgdir}"${MINGW_PREFIX}/bin/*.a "${pkgdir}"${MINGW_PREFIX}/lib
}
