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

_realname=libproxy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.5.11
pkgrel=2
pkgdesc="A library that provides automatic proxy configuration management (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://libproxy.github.io/libproxy/"
msys2_repository_url="https://github.com/libproxy/libproxy"
msys2_references=(
  "cpe: cpe:/a:libproxy_project:libproxy"
)
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-duktape"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-vala")
checkdepends=("${MINGW_PACKAGE_PREFIX}-libsoup3")
source=("https://github.com/libproxy/libproxy/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-install-backend-bindir.patch")
sha256sums=('b364f4dbbffc5bdf196330cb76b48abcb489f38b1543e67595ca6cb7ec45d265'
            '659e9f5ab6097cff46b04e49b833c89422c2b7fbd6e26baf370fac7159c0aecf')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # Install libpxbackend-1.0.dll in bindir for libproxy-1.dll
  # Partially reverts https://github.com/libproxy/libproxy/pull/223
  patch -p1 -i "${srcdir}/0001-install-backend-bindir.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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    "${_extra_config[@]}" \
    -Ddocs=false \
    -Dtests=false \
    -Dconfig-kde=false \
    ../${_realname}-${pkgver}

  meson compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  meson test || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" meson install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
