# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=vector_blf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.4.2
pkgrel=1
pkgdesc="A library to access Binary Log File (BLF) files from Vector Informatik. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/Technica-Engineering/vector_blf'
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/Technica-Engineering/vector_blf/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-install-dll.patch"
        "002-fix-build-with-gcc-14.patch")
sha256sums=('02d4bdccc2a4bfa93a3d2f62d95c77c3166bc94d2df55dd49298e8099dc7c090'
            'ffec1f477e3acb3a213f85d8ae51b97c817e679d2fd9e3a09d22f65b79c93bda'
            '64db10094ce9cb743b4293115cc8967a4591d8f41ef0928383907efe6f295f35')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/001-fix-install-dll.patch
  patch -p1 -i "${srcdir}"/002-fix-build-with-gcc-14.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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -DOPTION_RUN_DOXYGEN=OFF \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSES/* \
    -t "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}
}
