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

_realname=poco
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.2
pkgrel=1
pkgdesc="POrtable COmponents C++ Libraries (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://pocoproject.org/"
msys2_references=(
  "cpe: cpe:2.3:a:pocoproject:poco"
)
license=('spdx:BSL-1.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             # workaround lack of windmc in llvm
             $([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] || echo "binutils")) 
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         #"${MINGW_PACKAGE_PREFIX}-apr"
         #"${MINGW_PACKAGE_PREFIX}-apr-util"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-postgresql"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
source=(https://pocoproject.org/releases/${_realname}-${pkgver}/${_realname}-${pkgver}-all.tar.bz2
        001-fix-build-on-mingw.patch
        011-cmake-mingw.patch
        012-dll-resources.patch
        013-templates-export.patch)
sha256sums=('7738a4c880f08e89318fc53915f86be5a0e70f14eb1e7f0e1767c8781c40d794'
            '2b2a4ccc48061874a8c8d36d96ac6f2d40b01130a5c10816232cba1988a6c97a'
            'c5084394e28bf9bcfaaa3491ff28ca963efd57a79fb20aea6368f0a2490fb1ea'
            '8059fc9bb6b158f7705323358d38c9d952061b546d6ae8fbd3c4ac2493677a0a'
            '6d67531c0c11f6fd3892a899230c746ad0dc7d7c6e11146e4b81b3b1031b1a9f')

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

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}-all
  _apply_patch_with_msg \
    001-fix-build-on-mingw.patch \
    011-cmake-mingw.patch \
    012-dll-resources.patch \
    013-templates-export.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

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

  CFLAGS+=" -DPCRE2_STATIC" \
  CXXFLAGS+=" -DPCRE2_STATIC" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=OFF \
    -DPOCO_UNBUNDLED=ON \
    -DENABLE_MONGODB=OFF \
    -DENABLE_DATA_ODBC=OFF \
    -DPOCO_DATA_NO_SQL_PARSER=ON \
    ../${_realname}-${pkgver}-all

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DPOCO_UNBUNDLED=ON \
    -DENABLE_MONGODB=OFF \
    -DENABLE_DATA_ODBC=OFF \
    -DPOCO_DATA_NO_SQL_PARSER=ON \
    ../${_realname}-${pkgver}-all

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

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

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

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