# Maintainer: Aleksandr Serbin <aleksandr.serbin@gmail.com>

_realname=amqp-cpp
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.6
pkgrel=2
pkgdesc="C++ library for asynchronous non-blocking communication with RabbitMQ (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/CopernicaMarketingSoftware/AMQP-CPP"
license=('Apache2.0')
depends=()
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("${_realname}-${pkgver}.tar.gz"::https://github.com/CopernicaMarketingSoftware/AMQP-CPP/archive/v${pkgver}.tar.gz
        001-cmake-install-config.patch
        002-fix-shared-linking.patch
        003-shared-lib-install.patch)
sha256sums=('48832068dc1e25a5313dd9e96fb33ba954c19f9dc04eeca8f7b2c6cecde3afc9'
            '236787a1151b097cf5584cbbdb9882c9a3507b87168a4671ea54690f3144ee33'
            '23def70fa83a02613945df1c15446f67c66e6999113b97416d01e7c89c062b1c'
            '54ef815fb5bda56812bbbdd98ec3108268e238f3eb2c9ccb74892d3059ce19ec')

prepare() {
  cd  ${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/001-cmake-install-config.patch
  patch -p1 -i ${srcdir}/002-fix-shared-linking.patch
  patch -p1 -i ${srcdir}/003-shared-lib-install.patch
}

build() {
  msg "Build static library"
  rm -rf "${srcdir}/build-${MINGW_CHOST}-static"
  mkdir "${srcdir}/build-${MINGW_CHOST}-static" && cd "${srcdir}/build-${MINGW_CHOST}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DAMQP-CPP_BUILD_SHARED=OFF \
    -DAMQP-CPP_LINUX_TCP=OFF \
    "../${_realname}-${pkgver}"

  make

  msg "Build shared library"
  rm -rf "${srcdir}/build-${MINGW_CHOST}-shared"
  mkdir "${srcdir}/build-${MINGW_CHOST}-shared" && cd "${srcdir}/build-${MINGW_CHOST}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DAMQP-CPP_BUILD_SHARED=ON \
    -DAMQP-CPP_LINUX_TCP=OFF \
    "../${_realname}-${pkgver}"
  
  make
}

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

  cd "${srcdir}/build-${MINGW_CHOST}-shared"
  make DESTDIR=${pkgdir} install
  
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/LICENSE" \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
