# Based on the Arch package
# Don't building with installed jbigkit package as we build tiff without jbigkit support

_pkgname=tkimg
pkgbase=mingw-w64-${_pkgname}
pkgname=${MINGW_PACKAGE_PREFIX}-$_pkgname
pkgver=2.1.0
pkgrel=1
pkgdesc="Adds support to Tk for many other Image formats: BMP, XBM, XPM, GIF, PNG, JPEG, TIFF and postscript (mingw-64)"
url="https://tkimg.sourceforge.io/"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('BSD')
depends=(#"${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-tk"
         "${MINGW_PACKAGE_PREFIX}-zlib")
# Uses dtplite for generating the man pages:
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-tcllib"
  "${MINGW_PACKAGE_PREFIX}-autotools"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://downloads.sourceforge.net/project/tkimg/tkimg/${pkgver:0:3}/tkimg%20${pkgver}/Img-${pkgver}.tar.gz"
        002-dtplite_tcl.patch)
sha256sums=('98286b5f4ae57e1c73705ac5a08ede54444b610329241ecc13e99d60ce5c7095'
            'eb39aa5f628852b8030041c082e1ede89e2516930125572e5b1f8bc1bcbe8fca')

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}"/${_patch}
  done
}

prepare() {
  cd "${srcdir}/Img-${pkgver}"
  
  apply_patch_with_msg \
    002-dtplite_tcl.patch

  autoreconf -fiv
}

build() {
  if check_option "debug" "y"; then
    extra_config+=( --enable-symbols )
  fi

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

  ../Img-${pkgver}/configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --mandir=${MINGW_PREFIX}/share/man \
    "${extra_config[@]}"
  make
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  make INSTALL_ROOT="${pkgdir}" install-libraries
  make INSTALL_ROOT="${pkgdir}" install-man
  rm -f "${pkgdir}"/${MINGW_PREFIX}/lib/*.sh
}
