# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=engauge-digitizer
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=12.1
pkgrel=2
pkgdesc="Extracts data points from images of graphs. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='http://markummitchell.github.io/engauge-digitizer'
msys2_repository_url="https://github.com/markummitchell/engauge-digitizer"
msys2_references=(
  'aur: engauge'
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         $([[ ${CARCH} == aarch64 ]] || echo "${MINGW_PACKAGE_PREFIX}-log4cpp")
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-poppler-qt5"
         "${MINGW_PACKAGE_PREFIX}-qt5-base"
         "${MINGW_PACKAGE_PREFIX}-qt5-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/markummitchell/engauge-digitizer/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-fix-build-on-mingw.patch")
sha256sums=('b553c71ad8b5565a42ae0ba58891d27ac47ca27cc6d7a442c979b56b1fbda282'
            '8a82f0adbe2360f0edebfea0a34976978fdb26145ed8cae956fbfb2d56b2fcd7')

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

  patch -p1 -i "${srcdir}/001-fix-build-on-mingw.patch"
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  local -a _extra_config
  if check_option "debug" "y"; then
    _extra_config+=(CONFIG+=debug)
  else
    _extra_config+=(CONFIG+=release)
  fi

  if [[ ${CARCH} == aarch64 ]]; then
    _extra_config+=("CONFIG+=log4cpp_null")
  fi

  OPENJPEG_INCLUDE=$(${MINGW_PREFIX}/bin/pkgconf --variable includedir libopenjp2) \
  OPENJPEG_LIB=${MINGW_PREFIX}/lib \
  POPPLER_INCLUDE=$(${MINGW_PREFIX}/bin/pkgconf --cflags-only-I poppler-qt5 | cut -d ' ' -f 1 | sed 's/^-I//') \
  POPPLER_LIB=${MINGW_PREFIX}/lib \
  FFTW_HOME=${MINGW_PREFIX} \
  ${MINGW_PREFIX}/bin/qmake.exe \
    "${_extra_config[@]}" \
    "CONFIG+=pdf jpeg2000" \
    ../"${_realname}-${pkgver}"/engauge.pro

  make

  # Build docs
  ${MINGW_PREFIX}/bin/qcollectiongenerator.exe \
    ../${_realname}-${pkgver}/help/engauge.qhcp \
    -o engauge.qhc
  mv ../${_realname}-${pkgver}/help/engauge.qch .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  install -d "${pkgdir}"${MINGW_PREFIX}/bin/
  install -Dm755 bin/engauge.exe "${pkgdir}"${MINGW_PREFIX}/bin/

  # for some reason Help is empty even they were installed?
  #install -d "${pkgdir}"${MINGW_PREFIX}/share/doc/engauge-digitizer
  #install -Dm755 engauge.{qch,qhc} "${pkgdir}"${MINGW_PREFIX}/share/doc/engauge-digitizer

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
