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

_realname=libfdf
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.5.1
pkgrel=1
pkgdesc="Flexible Data Format library. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://gitlab.com/siesta-project/libraries/libfdf'
msys2_references=(
  'aur: libfdf'
)
license=('spdx:BSD-3-Clause')
depends=($([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran"))
makedepends=("${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://gitlab.com/siesta-project/libraries/libfdf/-/archive/${pkgver}/${_realname}-${pkgver}.tar.bz2"
        "001-fix-install-dll.patch")
sha256sums=('31341ab4ba5be67defb6b894bab9390de2b7eeb604d031f2cda65212fc6299f4'
            'f382786b832f1e2675116382d441bf81004fa9f44db2cc550a3a40ef27ce6428')

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

  patch -Np1 -i "${srcdir}"/001-fix-install-dll.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=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

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

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

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