# 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.3
pkgrel=1
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'
  'purl: pkg: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")
sha256sums=('20d6e0c927e27ced85643719bd63e9f7fd501df6e9a8aab1489b039897fd7c01')

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

  # wxPython possible bug fixes 1xx

  # backport wxPython git changes 4xx

  # 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
}
