# Maintainer:  Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_wx_basever=3.2

_realname=wxPython
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.2.1
pkgrel=3
pkgdesc="A wxWidgets GUI toolkit for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.wxpython.org/"
msys2_repository_url="https://github.com/wxWidgets/Phoenix"
msys2_references=(
  'archlinux: python-wxpython'
  'pypi: wxPython'
)
license=('spdx:wxWindows')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-numpy"
         "${MINGW_PACKAGE_PREFIX}-python-pillow"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-waf"
             "${MINGW_PACKAGE_PREFIX}-sip"
             "${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-requests"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             # for waf
             "python"
             "python-setuptools")
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "401-fix-build-with-sip-6.8.3.patch"
        "402-fix-build-with-sip-6.8.5.patch")
sha256sums=('e48de211a6606bf072ec3fa778771d6b746c00b7f4b970eb58728ddf56d13d5c'
            '09a223434911e46a3b9016286f6067c7b7783afc9a0063f1aa6f425e88953593'
            '50a18abea8f778f4012e1a75805fb61d33810e2a424efa4cad2d0c1dbb49ebe9')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  rm -f etg/{_,}webkit.py sip/gen/{_,}webkit.sip

  # wxPython possible bug fixes 1xx

  # backport wxPython git changes 4xx
  # https://github.com/wxWidgets/Phoenix/commit/2c630714
  patch -p1 -i "${srcdir}"/401-fix-build-with-sip-6.8.3.patch
  # https://github.com/wxWidgets/Phoenix/commit/fd8781c9
  patch -p1 -i "${srcdir}"/402-fix-build-with-sip-6.8.5.patch

  # wxWidgets changes 2xx
  cd "${srcdir}/${_realname}-${pkgver}/ext/wxWidgets"
  # Reduce warnings
  ${MINGW_PREFIX}/bin/doxygen -u docs/doxygen/Doxyfile
}

build() {
  rm -rf build-${MSYSTEM} | true
  cp -r ${_realname}-${pkgver} build-${MSYSTEM} && cd build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--release")
  else
    _extra_config+=("--debug")
  fi

  export PYTHONDONTWRITEBYTECODE=1
  local _jobs=${MAKEFLAGS:--j1}

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  WAF="${MINGW_PREFIX}/bin/waf" \
  SIP="${MINGW_PREFIX}/bin/sip" \
  DOXYGEN="${MINGW_PREFIX}/bin/doxygen" \
  WX_CONFIG="${MINGW_PREFIX}/bin/wx-config-${_wx_basever}" \
  ${MINGW_PREFIX}/bin/python -m build \
    --prefix="${MINGW_PREFIX}" \
    --python="${MINGW_PREFIX}/bin/python.exe" \
    ${_extra_config[@]} \
    --use_syswx \
    --no_msedge \
    --nodoc \
    --cairo \
    -vv \
    --no_allmo \
    --no_magic \
    --regenerate_sysconfig \
    --jobs=${_jobs#-j} \
    dox sip

  # Use MSYS2 Python because, with mingw-w64 Python, waf has issues using
  # the wx-config* shell script.
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  CC_NAME=${CC} \
  CC="${MINGW_PREFIX}/bin/${CC}.exe" \
  CXX_NAME=${CXX} \
  CXX="${MINGW_PREFIX}/bin/${CXX}.exe" \
  LDFLAGS="${LDFLAGS} $(${MINGW_PREFIX}/bin/python-config --ldflags)" \
  PYTHON_CONFIG="${MINGW_PREFIX}/bin/python-config" \
  /usr/bin/python "${MINGW_PREFIX}/bin/waf" \
    --prefix="${MINGW_PREFIX}" \
    --python="${MINGW_PREFIX}/bin/python.exe" \
    --no_msvc \
    --check-c-compiler=${CC} \
    --check-cxx-compiler=${CXX} \
    --color=yes \
    --jobs=${_jobs#-j} \
    --wx_config="sh.exe ${MINGW_PREFIX}/bin/wx-config-${_wx_basever}" \
    --no_magic \
    --nopyc \
    --nopyo \
    --nopycache \
    configure build
}

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

  MSYS2_ARG_CONV_EXCL="-p" python -m compileall \
    -o 0 -o 1 -q -s"${pkgdir}" -p"/" "${pkgdir}${MINGW_PREFIX}/lib/python"*

  # remove shebang line
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done

  install -vDm 644 LICENSE.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/$_realname/LICENSE.txt"
}
