####
#
# Based on packages found at these URLs
#     https://www.archlinux.org/packages/extra/i686/wxgtk-common/
#     https://github.com/Alexpux/MINGW-packages/tree/master/mingw-w64-wxWidgets
#
# Maintainer: Tim S <stahta01@gmail.com>
#
####
# Normal 64 bit build command
#   MINGW_ARCH=mingw64 makepkg-mingw -sLf

_basename=wxWidgets
_realname=wxmsw
_wx_basever=3.1
# Example _wx_buildver value is "-rc"
_wx_buildver=

pkgbase=mingw-w64-${_basename}${_wx_basever}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}${_wx_basever}"
pkgver=${_wx_basever}.5
pkgrel=3
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')
url="https://wxwidgets.org/"
license=('custom:wxWindows')
makedepends=(
  "make"
  "patch"
  "${MINGW_PACKAGE_PREFIX}-libpng"
  "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
  "${MINGW_PACKAGE_PREFIX}-libtiff"
)
options=('strip' 'staticlibs' 'buildflags' '!debug')
source=(
  https://github.com/wxWidgets/wxWidgets/releases/download/v${pkgver}${_wx_buildver}/${_basename}-${pkgver}${_wx_buildver}.tar.bz2

  # This patch is a MSys2 run-time patch
  001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # the wxTeam rejected this patch
  005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch

  # Patches that I hope to have added to git master branch
  023-wxWidgets-3.1.2-setup-wxUSE_GRAPHICS_DIRECT2D.patch
)
sha256sums=('d7b3666de33aa5c10ea41bb9405c40326e1aeb74ee725bb88f90f1d50270a224'
            '7c3b8f6ba275a448a5e82d64c4914acd5aefb8bbb952389688f3e7167a787c56'
            '4a4828f0c9cdc638cffde6a30b5dfb14283719acc9e89e19de8ec2d5a80a5aec'
            '760d45a284b56c0cd87c834cd7346e70c11ada51035a3ff7f6984fceccfdb932')
prepare() {
  cd "${srcdir}"/${_basename}-${pkgver}${_wx_buildver}

  # Fix MSys2 Run-Time wx-config bug.
  patch -p1 -i "${srcdir}"/001-wxWidgets-3.0.2-relocate-prefix-in-bin-wx-config.patch

  # This patch is not really needed; but, WX_LIBS_STATIC does not work correctly under MSys2
  # Removed it to see if anything breaks or if anything is fixed.
  patch -p1 -i "${srcdir}"/005-wxWidgets-3.1.3-Remove-WX_LIBS_STATIC-from-m4.patch

  patch -p1 -i "${srcdir}"/023-wxWidgets-3.1.2-setup-wxUSE_GRAPHICS_DIRECT2D.patch
}

build() {
  ####
  # Configure options added to support other software:
  #   --enable-graphics_ctx     codelite
  #
  # Configure options added to check for build issues
  #   --disable-precomp-headers
  #
  # Configure options added to avoid possible future issues
  #   --with-cxx=14
  #   --enable-std_string
  #
  # Configure options added to avoid warnings:
  #   --with-regex=builtin
  #
  # Configure options known to cause build errors:
  #   --disable-regkey                                compile error
  #
  # Configure options believed to reduce code size or build time:
  #   --without-opengl
  #   --without-subdirs
  #   --disable-webview
  #   --disable-mediactrl
  ####

# Remove the -O and -g options to avoid configuration warnings
# from the normal settings found in /etc/makepkg_mingw??.conf
  unset CFLAGS
  unset CXXFLAGS
  CFLAGS="-march=${CARCH} -mtune=generic -pipe"
  CXXFLAGS="-march=${CARCH} -mtune=generic -pipe"
  #echo "CXXFLAGS := ${CXXFLAGS}"

  local -a _debug_options=()

  if check_option "debug" "y"; then
    _debug_options+=("--enable-debug=yes")
    _debug_options+=("--enable-debug_gdb")
  else
    _debug_options+=("--enable-optimise")
  fi

  #echo "_debug_options := ${_debug_options[@]}"

  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  mkdir -p "${srcdir}"/build-${CARCH}-shared && cd "${srcdir}"/build-${CARCH}-shared

  ../${_basename}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --enable-shared \
    --enable-std_string \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=14 \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-regex=builtin

  make #VERBOSE=1


  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${CARCH}-static && cd "${srcdir}"/build-${CARCH}-static

  ../${_basename}-${pkgver}${_wx_buildver}/configure \
    --prefix=${MINGW_PREFIX} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --build=${MINGW_CHOST} \
    "${_debug_options[@]}" \
    --disable-shared \
    --enable-std_string \
    --enable-iff \
    --enable-permissive \
    --enable-unicode \
    --enable-graphics_ctx \
    --enable-accessibility \
    --disable-monolithic \
    --disable-precomp-headers \
    --with-msw \
    --with-cxx=14 \
    --with-opengl \
    --with-libpng=sys \
    --with-libjpeg=sys \
    --with-libtiff=sys \
    --with-regex=builtin

  make #VERBOSE=1 -j1
}

#check() {
  #cd "${srcdir}/build-${CARCH}-shared/tests"    && make -k --jobs=1 || true

  #cd "${srcdir}/build-${CARCH}-shared/samples"  && make -k --jobs=1 || true
#}

package() {
  pkgdesc="A C++ library that lets developers create applications for Windows, Linux and UNIX (mingw-w64)"
  provides=("${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-wxWidgets${_wx_basever}")
  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"
  )

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

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

  # Rename files
  mv ${pkgdir}${MINGW_PREFIX}/bin/wx-config{,-${_wx_basever}}
  mv ${pkgdir}${MINGW_PREFIX}/share/aclocal/wxwin.m4 ${pkgdir}${MINGW_PREFIX}/share/aclocal/wxwin${_wx_basever}.m4

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

  # Remove conflicting files; that do NOT need to be renamed.
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/wxrc.exe
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/wx-config

  # Remove conflicting files; that I have no idea how to rename
  rm -fr ${pkgdir}${MINGW_PREFIX}/share/bakefile
  rm -fr ${pkgdir}${MINGW_PREFIX}/share/locale

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