# Maintainer: Zoltan Gyarmati <zgyarmati@zgyarmati.de>

_realname=simple-mail
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.0
pkgrel=3
pkgdesc="An SMTP library written in C++ for Qt (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/cutelyst/simple-mail"
license=("LGPL")
depends=("${MINGW_PACKAGE_PREFIX}-qt5")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-gcc"
            "${MINGW_PACKAGE_PREFIX}-ninja"
            "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/cutelyst/${_realname}/archive/v${pkgver}.tar.gz")
sha256sums=('ccb8d4e3dcc0c1648480284ae6e5b5c2b06dd2bfccd615bda970ff34415dd35d')

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_DEMOS=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build . --target install

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    # Replace "libdir=/mingw64/lib" with "libdir=${prefix}/lib"
    sed -s "s|${MINGW_PREFIX}/|\${prefix}/|g" -i "${pcfile}"
  done
}
