# Based on https://aur.archlinux.org/packages/mingw-w64-capnproto/
#
# Maintainer: Igor Matuszewski <xanewok@gmail.com>
# Contributors: Dave Reisner <dreisner@archlinux.org>
#               Matthias Blaicher <matthias@blaicher.com>
#               Severen Redwood <severen@shrike.me>
#               Igor Matuszewski <xanewok@gmail.com>
#               Björn Schäpers <bjoern@hazardy.de>

_realname=capnproto
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.0
pkgrel=1
_sourcedir=${_realname}-${pkgver}
pkgdesc="Cap'n Proto serialization/RPC system (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://capnproto.org/'
msys2_references=(
  "cpe: cpe:/a:capnproto:capnproto"
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/capnproto/capnproto/archive/refs/tags/v${pkgver}.tar.gz"
        kj.patch::https://github.com/capnproto/capnproto/commit/a2deb0511dba79f90373c3dc5d45983a574bccfa.diff
        0001-Capnp-Add-ADD_IMPORT_PATH_FROM_BINARY-build-flag.patch
        dont-be-weak.patch)
noextract=("v${pkgver}.tar.gz")
sha256sums=('763ecaa78e900101bc40c1c3397991413ff26996415b891ca9c310987fbdf3fb'
            '33ffc3e6d65982b3c692fdf5abcf38387ebd6a34da5dfe2e85dcd923017123aa'
            'ba36a807bdfc27847f380db84c513b38dfb76316ebb6ff64e25c04b847017c82'
            'a44faf9cf5e2cacfd154e0f292c2b2b3542a23f7848ced4e60c1724539f1704c')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  tar -xf v${pkgver}.tar.gz --exclude="c++/ekam-provider" --exclude="c++/src/ekam-rules"
  cd "${srcdir}/${_sourcedir}"

  apply_patch_with_msg \
    kj.patch \
    0001-Capnp-Add-ADD_IMPORT_PATH_FROM_BINARY-build-flag.patch \
    dont-be-weak.patch
}

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

MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DADD_IMPORT_PATH_FROM_BINARY=ON \
    ../${_sourcedir}

  # Help capnpc to find libkj.dll
  PATH=$PWD/c++/src/kj:$PATH ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  # Help capnp-tests to find DLLs
  PATH=$PWD/c++/src/capnp:$PWD/c++/src/kj:$PATH ${MINGW_PREFIX}/bin/ctest.exe ./ || warning "Tests failed"
}

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

  local PREFIX_WIN="$(cygpath -wm ${MINGW_PREFIX})"

  for cmkfile in "${pkgdir}${MINGW_PREFIX}"/lib/cmake/CapnProto/*.cmake; do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i "${cmkfile}"
  done

  for pcfile in "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    sed -s "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i "${pcfile}"
  done

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