# Maintainer: wszqkzqk <wszqkzqk@qq.com>

_realname=pymol
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.1.0
pkgrel=2
pkgdesc="Molecular visualization system on an Open Source foundation (mingw-w64)"
arch=(any)
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url="https://pymol.org"
_url="https://github.com/schrodinger/pymol-open-source"
license=('custom')
msys2_references=(
  'archlinux: pymol'
  'purl: pkg:pypi/pymol-open-source'
)
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-glm"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-python-pyqt5"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-netcdf-cxx"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-mmtf-cpp"
             "${MINGW_PACKAGE_PREFIX}-msgpack-c"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('!strip')
_archive="pymol-open-source-$pkgver"
source=("$_url/archive/v$pkgver/$_archive.tar.gz"
        "support-to-build-pymol-with-mingw-w64.patch"
        "fix-update-launch-script-handling-for-mingw-w64-msys.patch")
sha256sums=('54306d65060bd58ed8b3dab1a8af521aeb4fd417871f15f463ff05ccb4e121fe'
            '784e2f29c73cd361a80b5c0ea14b8ca2820b19ffaefe37ad138bb567a8c802b5'
            '8f0aa54849bfbd3b738a93fa4ffdc875e07bfdbb58eaaf0de8950b14c3b8336f')

prepare() {
  cd "${_archive}"
  # Backport https://github.com/schrodinger/pymol-open-source/pull/486
  patch -Np1 -i "${srcdir}/support-to-build-pymol-with-mingw-w64.patch"
  # Backport https://github.com/schrodinger/pymol-open-source/pull/487
  patch -Np1 -i "${srcdir}/fix-update-launch-script-handling-for-mingw-w64-msys.patch"
  # unpin over-aggressive dependency pinning
  sed -i -e '/numpy/s/>.*"/"/g' pyproject.toml
  # avoid duplicate binary creation
  sed -i -e '/pymol:launch/d' pyproject.toml
}

build() {
  cp -r "${_archive}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"
  python -m build --wheel --skip-dependency-check --no-isolation
}

package() {
  cd "${srcdir}/python-build-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    python -m installer --prefix=${MINGW_PREFIX} \
      --destdir="${pkgdir}" dist/*.whl

  install -Dm644 "$srcdir/${_archive}/LICENSE" "${pkgdir}${MINGW_PACKAGE_PREFIX}/share/licenses/${_realname}/LICENSE"
}
