# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=libpsl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.21.1
pkgrel=3
pkgdesc="Public Suffix List library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/rockdaboot/libpsl"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libunistring"
         "${MINGW_PACKAGE_PREFIX}-gettext")
makedepends=("${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-gettext"
             "${MINGW_PACKAGE_PREFIX}-gtk-doc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/rockdaboot/libpsl/releases/download/${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('ac6ce1e1fbd4d0254c4ddb9d37f1fa99dec83619c1253328155206b896210d4c')

build() {
  [[ -d "${srcdir}/build-shared-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-shared-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-shared-${MINGW_CHOST}" && cd "${srcdir}/build-shared-${MINGW_CHOST}"

  LDFLAGS+=" -liconv"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    -Druntime=libidn2 \
    -Dbuiltin=libidn2 \
    --default-library shared \
    ../${_realname}-${pkgver}

  ninja

  [[ -d "${srcdir}/build-static-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-static-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-static-${MINGW_CHOST}" && cd "${srcdir}/build-static-${MINGW_CHOST}"

  export CPPFLAGS="$CPPFLAGS -DIN_LIBUNISTRING -DIDN2_STATIC -DPSL_STATIC"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    -Druntime=libidn2 \
    -Dbuiltin=libidn2 \
    --default-library static \
    ../${_realname}-${pkgver}

  ninja
}

package() {
  cd "${srcdir}/build-static-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ninja install

  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ninja install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
