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

_realname=lammps
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=20250722.2
_pkgver=${pkgver:6:2}$(i=${pkgver:4:2}; LC_ALL=C date -d ${i}/01 +%b)${pkgver:0:4}
if [[ ${pkgver:9} != "" ]]; then
  _tag=${_pkgver}_update${pkgver:9}
else
  _tag=${_pkgver}
fi
pkgrel=1
pkgdesc="Large-scale Atomic/Molecular Massively Parallel Simulator. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://www.lammps.org'
msys2_repository_url="https://github.com/lammps/lammps"
msys2_references=(
  'aur: lammps'
)
license=('spdx:GPL-2.0-only')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-gcc-libgfortran")
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-fc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-ffmpeg"
             "${MINGW_PACKAGE_PREFIX}-qt6-base"
             "${MINGW_PACKAGE_PREFIX}-qt6-charts")
             #"${MINGW_PACKAGE_PREFIX}-msmpi")
optdepends=("${MINGW_PACKAGE_PREFIX}-ffmpeg: Support generating movies"
            "${MINGW_PACKAGE_PREFIX}-qt6-charts: For lammps-gui")
source=("https://github.com/lammps/lammps/releases/download/stable_${_tag}/lammps-src-${_tag}.tar.gz"
        "001-undef-ABSOLUTE.patch"
        "002-fix-pkgconfig-file.patch"
        "004-fix-executables-extension.patch")
sha256sums=('dcfc80a7211592fbebea4dcb99289ea197458900ad2ac57bf937280023d9856f'
            '2ab7d4e2158b953f1694ec955d993c461f151c4a6bded8edb576689d72b210a7'
            '320df126e7ca5a0c85eed2ca762e5a48a2c22c656571c0f054c514a0a7067c43'
            '50ed7acd3e81a4af0e529e2c9d56a00e591feec44ff080ef177da0dfdf9081ec')
noextract=("lammps-src-${_tag}.tar.gz")

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  echo "Extracting lammps-src-${_tag}.tar.gz..."
  tar -xzf lammps-src-${_tag}.tar.gz || true

  cd "${srcdir}"/${_realname}-${_pkgver}

  apply_patch_with_msg \
    001-undef-ABSOLUTE.patch \
    002-fix-pkgconfig-file.patch \
    004-fix-executables-extension.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

  CXXFLAGS+=" -Wno-deprecated-declarations" \
  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 \
      -DBUILD_OMP=ON \
      -DBUILD_MPI=OFF \
      -DBUILD_TOOLS=ON \
      -DBUILD_LAMMPS_GUI=ON \
      -DLAMMPS_EXCEPTIONS=ON \
      -DWITH_CURL=ON \
      -DWITH_FFMPEG=ON \
      -DWITH_GZIP=ON \
      -DWITH_JPEG=ON \
      -DWITH_PNG=ON \
      -DFFT=FFTW3 \
      -DPython_EXECUTABLE=${MINGW_PREFIX}/bin/python \
      -DENABLE_TESTING=OFF \
      -S "${_realname}-${_pkgver}"/cmake \
      -B "build-${MSYSTEM}"

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

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

  cp build-${MSYSTEM}/lammps-gui.exe "${pkgdir}"${MINGW_PREFIX}/bin/

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