_realname=slim-simulator
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.1
pkgrel=1
pkgdesc="SLiM forward simulation software package for population genetics (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://messerlab.org/slim/"
license=("spdx:GPL-3.0-or-later")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-qt6-base")
source=("https://github.com/MesserLab/SLiM/releases/download/v${pkgver}/SLiM.zip"
        "0001-Remove-Qt-version-check.patch")
sha256sums=('3a8499f92b8abe3b68136238139d3e43898da0aef84dd40d5e4b36b5a1aaf09b'
            '11679c3844de8d86b98f392ed1bf3711b15c7b5cc89eb2639c9e34fce3ede8c1')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  cd SLiM

  apply_patch_with_msg \
    0001-Remove-Qt-version-check.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SLIMGUI=ON \
      ../SLiM

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

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install .

  install -Dm 644 "${srcdir}/SLiM/LICENSE" -t "${pkgdir}${MINGW_PREFIX}"/share/licenses/SLiM/
  install -Dm 644 "${srcdir}/SLiM/VERSIONS" "${srcdir}/SLiM"/README.html \
    -t "${pkgdir}${MINGW_PREFIX}"/share/doc/SLiM/
}
