# Maintainer: Diego Sogari <diego.sogari@falker.com.br>

_realname=ng-log
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.8.2
pkgrel=2
pkgdesc="C++14 library that implements application-level logging (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/ng-log/ng-log"
license=('spdx:BSD-3-Clause')
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gflags"
         "${MINGW_PACKAGE_PREFIX}-libunwind"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread")
source=("https://github.com/ng-log/ng-log/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-force-linking-to-dbghelp.patch"
        "cmake-dual-lib.patch")
sha256sums=('4d7467025b800828d3b2eb87eb506b310d090171788857601a708a46825953a8'
            '1b80e26dd3a25c62c2a7a8fec57aad1217ad8d5cfdc837ae39be17db2b1ed120'
            '15e7ea0d248bfdd53ec2ceb1e456a47f450059c6d479b301f8e2d8853811e92c')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "../001-force-linking-to-dbghelp.patch"
  patch -Np1 -i "../cmake-dual-lib.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}/static-${MSYSTEM}" && cd "${srcdir}/static-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_COMPAT=OFF \
    "${_extra_config[@]}" \
    -DWITH_PKGCONFIG=ON \
    -DWITH_TLS=ON \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
  
  # Build shared
  mkdir -p "${srcdir}/shared-${MSYSTEM}" && cd "${srcdir}/shared-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    -DBUILD_COMPAT=OFF \
    "${_extra_config[@]}" \
    -DWITH_PKGCONFIG=ON \
    -DWITH_TLS=ON \
    "../${_realname}-${pkgver}"

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

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

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

  local WINPREFIX=$(cygpath -wm ${MINGW_PREFIX})
  sed -i "s|${WINPREFIX}|${MINGW_PREFIX}|g" \
    "${pkgdir}"${MINGW_PREFIX}/lib/cmake/ng-log/ng-log*-targets.cmake
}
