# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=wxPython
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=4.1.1
pkgrel=1
pkgdesc="A wxWidgets GUI toolkit for Python (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=("custom:wxWindows")
url="https://www.wxpython.org/"
depends=("${MINGW_PACKAGE_PREFIX}-python"
         $( [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo \
           "${MINGW_PACKAGE_PREFIX}-python-numpy" )
         "${MINGW_PACKAGE_PREFIX}-python-pillow"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets3.1")
makedepends=("${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-python-requests"
             "${MINGW_PACKAGE_PREFIX}-sip4"
             "${MINGW_PACKAGE_PREFIX}-waf"
             # for waf
             "python" "python-setuptools")
options=('strip' 'staticlibs' 'buildflags')
source=("https://files.pythonhosted.org/packages/b0/4d/80d65c37ee60a479d338d27a2895fb15bbba27a3e6bb5b6d72bb28246e99/${_realname}-${pkgver}.tar.gz"
        "0001-wxWidgets-win-from-cygwin.patch"
        "0002-handle-cast.patch"
        "0003-wxWidgets.Doxyfile.patch"
        "0006-grid_h.patch"
        "0100-wxPython-wxHandleFatalExceptions.patch")
noextract=("${_realname}-${pkgver}.tar.gz")
sha256sums=('00e5e3180ac7f2852f342ad341d57c44e7e4326de0b550b9a5c4a8361b6c3528'
            '88cc6c7c9cdb6dd6f8e7a74356b275262f4e8d920d64bae987408ee2ff651bc1'
            '7af6ee0a93cfd5d5a621ed09ba91a42a017c8c42a2ff4936b98bdf4014086359'
            'f0216f2e38f338d52311a963b976f17998a4754b4c5ab2698074c08ab4104538'
            'fc786aa69d9070ef0f8e4663798916323256f5c87ea9dd8cb12b68230eabddb8'
            'f2416985340363453c44b62c659d378e345a1e1b28c2173bbad2671a751cf844')

prepare() {
  plain "Extracting ${_realname}-${pkgver}.tar.gz due to symlink(s) without pre-existing target(s)"
  cd "${srcdir}"
  [[ -d ${_realname}-${pkgver} ]] && rm -rf ${_realname}-${pkgver}
  tar zxf "${srcdir}/${_realname}-${pkgver}.tar.gz" || true
  cd "${srcdir}/${_realname}-${pkgver}"
  rm -f etg/{_,}webkit.py sip/gen/{_,}webkit.sip
  patch -Np1 -i "${srcdir}/0001-wxWidgets-win-from-cygwin.patch"
  # clang doesn't like casting from pointer to smaller integer type (long)
  # https://github.com/wxWidgets/Phoenix/pull/1972
  patch -Np1 -i "${srcdir}/0002-handle-cast.patch"
  patch -Np1 -i "${srcdir}/0003-wxWidgets.Doxyfile.patch"
  patch -Np1 -i "${srcdir}/0006-grid_h.patch"
  # https://github.com/wxWidgets/Phoenix/commit/3500ac7a9e7377c154a507dd7ea1b5b7bfda8c09
  patch -Np1 -i "${srcdir}/0100-wxPython-wxHandleFatalExceptions.patch"
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  export PYTHONDONTWRITEBYTECODE=1
  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-3.1" \
    "${MINGW_PREFIX}/bin/python" build.py \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --release --use_syswx --no_msedge --nodoc --cairo --verbose \
      --no_allmo --no_magic --regenerate_sysconfig \
        dox touch etg sip

  local _jobs=${MAKEFLAGS:--j1}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  CC_NAME=${CC} CXX_NAME=${CXX} \
  LDFLAGS="${LDFLAGS} $(python-config --ldflags)" \
  PYTHON_CONFIG="${MINGW_PREFIX}/bin/python-config" \
    "${MINGW_PREFIX}/bin/waf" \
      --prefix="${MINGW_PREFIX}" --python="${MINGW_PREFIX}/bin/python.exe" \
      --check-cxx-compiler=${CXX} --check-c-compiler=${CC} --color=yes --jobs=${_jobs#-j} \
      --wx_config=wx-config-3.1 \
      --no_magic --nopyc --nopyo --nopycache \
        configure build
}

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

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