# Maintainer: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=libexodus
_realname2=seacas
_releases=2025-05-22
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=9.04
pkgrel=1
pkgdesc="EXODUS is a model developed to store and retrieve data for finite element analyses (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://sandialabs.github.io/seacas-docs"
msys2_repository_url="https://github.com/sandialabs/seacas"
license=('spdx:BSD-3-Clause')
conflicts=("${MINGW_PACKAGE_PREFIX}-seacas"
           "${MINGW_PACKAGE_PREFIX}-trilinos")
depends=("${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-netcdf")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("https://github.com/sandialabs/seacas/archive/v${_releases}/${_realname2}-${_releases}.tar.gz")
sha256sums=('50e941ad2bfffa84d84465576cd118a14698b3f0cfcfc613dbb1aa3f2f4ebdda')
noextract=("${_realname2}-${_releases}.tar.gz")

prepare() {
  echo "Extracting ${_realname2}-${_releases}.tar.gz ..."
  tar -xzf ${_realname2}-${_releases}.tar.gz || true

  cd "${srcdir}"/${_realname2}-${_releases}
  sed -i 's:-pedantic::g' cmake-exodus

}

build() {
  mkdir -p "${_realname2}-${_releases}/build-${MSYSTEM}" && cd "${_realname2}-${_releases}/build-${MSYSTEM}"

  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=" \
      INSTALL_PATH=${MINGW_PREFIX} \
      THREADSAFE=ON \
      COMPILER=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "gnu" || echo "clang") \
      FC=$([[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]] && echo "gfortran" || echo "flang") \
      ../cmake-exodus \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DSeacas_ENABLE_TESTS=OFF \
      "${_extra_config[@]}"

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

package() {
  cd "${_realname2}-${_releases}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .
  install -Dm644 "${srcdir}"/seacas-${_releases}/packages/seacas/libraries/exodus/COPYRIGHT \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  # Remove extra files
  rm -rf "${pkgdir}"${MINGW_PREFIX}/lib/external_packages
}
