# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libslirp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.9.1
pkgrel=2
pkgdesc="General purpose TCP-IP emulator (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gitlab.freedesktop.org/slirp/libslirp"
msys2_references=(
  "cpe: cpe:/a:libslirp_project:libslirp"
)
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-glib2")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://gitlab.freedesktop.org/slirp/${_realname}/-/archive/v${pkgver}/${_realname}-v${pkgver}.tar.bz2")
noextract=("${_realname}-v${pkgver}.tar.bz2")
sha256sums=('3caff6e2de445f4995629d4929c55419f661b2b1d14f12481e155a71c1e8f811')

prepare() {
  bsdtar -xf "${_realname}-v${pkgver}.tar.bz2" 2>/dev/null || bsdtar -xf "${_realname}-v${pkgver}.tar.bz2"
}

build() {
  local -a _static_flags=(
    -DGIO_STATIC_COMPILATION
    -DGLIB_STATIC_COMPILATION
    -DGMODULE_STATIC_COMPILATION
    -DGOBJECT_STATIC_COMPILATION
  )

  CFLAGS+=" ${_static_flags[@]}" \
  CXXFLAGS+=" ${_static_flags[@]}" \
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Ddefault_library=static \
    "${_realname}-v${pkgver}" \
    "build-${MSYSTEM}-static"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson.exe setup \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Ddefault_library=shared \
    "${_realname}-v${pkgver}" \
    "build-${MSYSTEM}-shared"

  ${MINGW_PREFIX}/bin/meson.exe compile -C "build-${MSYSTEM}-shared"
}

package() {
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-static" --destdir "${pkgdir}"
  ${MINGW_PREFIX}/bin/meson.exe install -C "build-${MSYSTEM}-shared" --destdir "${pkgdir}"

  echo "Cflags.private: -DLIBSLIRP_STATIC" >> "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/slirp.pc

  install -Dm644 "${srcdir}/${_realname}-v${pkgver}/COPYRIGHT" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYRIGHT"
}
