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

_realname=seacas
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2025.10.14
_releases=${pkgver//./-}
pkgrel=3
pkgdesc="The Sandia Engineering Analysis Code Access System (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://gsjaardema.github.io/seacas/"
msys2_repository_url="https://github.com/gsjaardema/seacas"
license=('spdx:BSD-3-Clause')
conflicts=("${MINGW_PACKAGE_PREFIX}-libexodus")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         #"${MINGW_PACKAGE_PREFIX}-adios2"
         #"${MINGW_PACKAGE_PREFIX}-aws-sdk-cpp"
         "${MINGW_PACKAGE_PREFIX}-cgns"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-fmt"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-matio"
         "${MINGW_PACKAGE_PREFIX}-netcdf")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
             #"${MINGW_PACKAGE_PREFIX}-cereal")
source=("https://github.com/gsjaardema/seacas/archive/v${_releases}/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-add-libs.patch"
        "0002-cmake-findHDF5.patch"
        "0003-fix-find-aws-sdk.patch")
sha256sums=('f9351a8f1a555a015020f249b1e5c26a282fbb6e274f9b71eb38720d61267dda'
            'a3561756bfdeb942fc62add0aefa542b0e689b76a7f0d2d357e299465ee344d2'
            'ad1fecedab32279712afb8112d0d39dd80cbd67fbf2c4f30b26f41b7ad26c731'
            'bbe6bfd9676fdc2b0640255f90b79e0cb1ab541b062dbe5ab002006b1535f377')
noextract=(${_realname}-${pkgver}.tar.gz)

prepare() {
  plain "Extract ${_realname}-${pkgver}.tar.gz..."
  tar -xzf ${_realname}-${pkgver}.tar.gz || true

  cd "${srcdir}/${_realname}-${_releases}"
  patch -p1 -i "${srcdir}/0001-cmake-add-libs.patch"
  patch -p1 -i "${srcdir}/0002-cmake-findHDF5.patch"
  patch -p1 -i "${srcdir}/0003-fix-find-aws-sdk.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

  #Static Build
  mkdir -p "${_realname}-${_releases}/build-${MSYSTEM}-static" && cd "${_realname}-${_releases}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    INSTALL_PATH=${MINGW_PREFIX} \
    THREADSAFE=ON \
    ZOLTAN=NO \
    ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config[@]} \
      -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/${CC}.exe \
      -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/${CXX}.exe \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DSeacas_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSeacas_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=OFF

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

  #Shared Build
  mkdir -p "../build-${MSYSTEM}-shared" && cd "../build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    INSTALL_PATH=${MINGW_PREFIX} \
    THREADSAFE=ON \
    ZOLTAN=NO \
    ../cmake-config \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      ${extra_config[@]} \
      -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/${CC}.exe \
      -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/${CXX}.exe \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
      -DSeacas_ENABLE_Fortran=OFF \
      -DTPL_ENABLE_X11=OFF \
      -DSeacas_ENABLE_TESTS=OFF \
      -DBUILD_SHARED_LIBS=ON

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

package() {
  #Static Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MSYSTEM}-static"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  #Shared Install
  cd "${srcdir}/${_realname}-${_releases}/build-${MSYSTEM}-shared"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}"${MINGW_PREFIX}/lib -type f -name '*.cmake'); do
    sed -e "s|${PREFIX_WIN}|\$\{CMAKE_CURRENT_LIST_DIR\}/../../..|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{CMAKE_CURRENT_LIST_DIR\}/../../..|g" -i ${_f}
  done
}
