# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=wxWidgets
_wx_basever=3.0
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
provides=("${MINGW_PACKAGE_PREFIX}-wxmsw${_wx_basever}")
pkgbase=mingw-w64-${_realname}
pkgver=${_wx_basever}.5.1
pkgrel=4
pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
license=("custom:wxWindows")
url="https://www.wxwidgets.org/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
        )
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
options=('strip' 'staticlibs' 'buildflags')
source=(https://github.com/wxWidgets/wxWidgets/releases/download/v${pkgver}/wxWidgets-${pkgver}.tar.bz2
        "001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch"
        "002-wxWidgets-3.0.2-relax-abi-compatibility-gcc.patch"
        "003-wxWidgets-3.0.2-fix-access-sample.patch"
        "004-wxWidgets-3.0-clang-windows-link.patch")
sha256sums=('440f6e73cf5afb2cbf9af10cec8da6cdd3d3998d527598a53db87099524ac807'
            '7c3b8f6ba275a448a5e82d64c4914acd5aefb8bbb952389688f3e7167a787c56'
            '3138f7b84bf988892f62167afc6fa640ac154b629b243d86413f7c811e508713'
            'b8684dca94b288a023a8a3d55ad56bce87570576ead71670a237d909ff1c3625'
            'b8b49b1df4a7c53d72c7c117606ac9dc44589da474ca58f0806b107469f33dcb')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -p1 -i "${srcdir}"/001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch
  patch -p1 -i "${srcdir}"/002-wxWidgets-3.0.2-relax-abi-compatibility-gcc.patch
  patch -p1 -i "${srcdir}"/003-wxWidgets-3.0.2-fix-access-sample.patch
  # squashed upstream commits
  # https://github.com/wxWidgets/wxWidgets/commit/c0f2f3801150ea9c6640f3fdced1d9120938f0d7
  # and
  # https://github.com/wxWidgets/wxWidgets/commit/e8a7bae0a750bcb5d8aadc45629d2c9adaf2106c
  patch -p1 -i "${srcdir}"/004-wxWidgets-3.0-clang-windows-link.patch
}

#check() {
  #cd "${srcdir}"/build-${CARCH}/samples && make
  #cd "${srcdir}"/build-${CARCH}-static/samples && make

  #cd "${srcdir}"/build-${CARCH}/tests && make || true
  #cd "${srcdir}"/build-${CARCH}-static/tests && make || true
#}

build() {
  #CXXFLAGS+=" -std=gnu++11"
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-shared \
    --enable-iniconf \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --disable-mediactrl \
    --disable-monolithic \
    --disable-mslu \
    --disable-precomp-headers \
    --with-msw \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-zlib=sys \
    --with-expat=sys \
    --with-regex=builtin

  make #VERBOSE=1

  #CXXFLAGS+=" -std=gnu++11"
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    --enable-static \
    --disable-shared \
    --enable-iniconf \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --disable-mediactrl \
    --disable-monolithic \
    --disable-mslu \
    --disable-precomp-headers \
    --with-msw \
    --with-opengl \
    --with-libpng=builtin \
    --with-libjpeg=builtin \
    --with-libtiff=builtin \
    --with-zlib=builtin \
    --with-expat=builtin \
    --with-regex=builtin

  make #VERBOSE=1
}

package() {
  cd "${srcdir}"/build-${CARCH}-static
  make DESTDIR="${pkgdir}" install
  mv ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-static}

  cd "${srcdir}"/build-${CARCH}
  make DESTDIR="${pkgdir}" install

  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin

  # Add missing file with exe extension (Likely bug in wxWidgets makefile)
  cp -f ${pkgdir}${MINGW_PREFIX}/bin/wxrc-${_wx_basever} ${pkgdir}${MINGW_PREFIX}/bin/wxrc-${_wx_basever}.exe

  # License files
  cd "${srcdir}"/${_realname}-${pkgver}/docs
  install -Dm644 preamble.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/preamble.txt"
  install -Dm644 licence.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/licence.txt"
  install -Dm644 licendoc.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/licendoc.txt"
  install -Dm644 lgpl.txt     "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/lgpl.txt"
  install -Dm644 gpl.txt      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/gpl.txt"
  install -Dm644 xserver.txt  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/xserver.txt"
}
