# Maintainer: @naveen521kk on Github, Naveen M K <naveen521kk@gmail.com>

_realname=manim
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.0
pkgrel=1
pkgdesc="Animation engine for explanatory math videos (community edition) (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url='https://www.manim.community'
license=('MIT' 'custom')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-ffmpeg"  
  "${MINGW_PACKAGE_PREFIX}-python-click"
  "${MINGW_PACKAGE_PREFIX}-python-click-default-group"
  "${MINGW_PACKAGE_PREFIX}-python-colour"
  "${MINGW_PACKAGE_PREFIX}-python-numpy"
  "${MINGW_PACKAGE_PREFIX}-python-pillow"
  "${MINGW_PACKAGE_PREFIX}-python-scipy"
  "${MINGW_PACKAGE_PREFIX}-python-tqdm"
  "${MINGW_PACKAGE_PREFIX}-python-pydub"
  "${MINGW_PACKAGE_PREFIX}-python-pygments"
  "${MINGW_PACKAGE_PREFIX}-python-rich"
  "${MINGW_PACKAGE_PREFIX}-python-cairo"
  "${MINGW_PACKAGE_PREFIX}-python-manimpango"
  "${MINGW_PACKAGE_PREFIX}-python-networkx"
  "${MINGW_PACKAGE_PREFIX}-python-decorator"
  "${MINGW_PACKAGE_PREFIX}-python-setuptools"
  "${MINGW_PACKAGE_PREFIX}-python-watchdog"
  "${MINGW_PACKAGE_PREFIX}-python-moderngl"
  "${MINGW_PACKAGE_PREFIX}-python-moderngl-window"
  "${MINGW_PACKAGE_PREFIX}-mapbox-earcut"
  "${MINGW_PACKAGE_PREFIX}-python-cloup"
  "${MINGW_PACKAGE_PREFIX}-python-requests"
)
optdepends=("${MINGW_PACKAGE_PREFIX}-texlive-scheme-medium: For LaTeX support")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=(
  "${_realname}-${pkgver}.tar.gz"::"https://github.com/ManimCommunity/${_realname}/releases/download/v${pkgver}/${_realname}-${pkgver}.tar.gz") # from https://github.com/ManimCommunity/manim/pull/1567
sha512sums=('2c9c939c8771b3f48583295d63e230f7db89f8a4f6b5d97d3afec8eb295df7e9b5035be6b2e0c1bf0db9f9a14257f0ad86d48c25ab3d24e6600437f708c4641f')

prepare() {
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  cd "python-build-${CARCH}"
  sed -i 's/rich>=6.0,<7.0/rich/' setup.py
  sed -i 's/decorator<5.0.0/decorator/' setup.py
  sed -i 's/click>=7.1,<8.0/click/' setup.py
  sed -i 's/manimpango>=0.2.4,<0.3.0/manimpango/' setup.py
}

build() {  
  msg "Python build for ${CARCH}"
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

package() {
  cd "${srcdir}/python-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -i 's/\#\!.*//' $_f
  done
  
  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE"
  install -Dm644 LICENSE.community "${pkgdir}${MINGW_PREFIX}/share/licenses/python-${_realname}/LICENSE.community"
}
