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

_realname=graphicsmagick
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.36
pkgrel=4
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="http://www.graphicsmagick.org/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "txt2html")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
         #"${MINGW_PACKAGE_PREFIX}-perl"
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support")
options=('staticlibs' 'strip' 'libtool')
source=(https://sourceforge.net/projects/graphicsmagick/files/${_realname}/${pkgver}/GraphicsMagick-${pkgver}.tar.xz
        001-relocate.patch
        002-fix-find-gdi32.patch)
sha256sums=('5d5b3fde759cdfc307aaf21df9ebd8c752e3f088bb051dd5df8aac7ba7338f46'
            '69cb9a06f0f4ec096dd3ed6c89f925c29da5f9bb8a335681b03cd7a38207e428'
            'a74a521226c5633697504899dda0956967ca4aec06ee91e6e0e23315a98eea36')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Nbp1 -i "${srcdir}/$_patch"
  done
}

del_file_exists() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      rm -rf $_fname
    fi
  done
}
# =========================================== #

prepare() {
  cd GraphicsMagick-${pkgver}
  del_file_exists \
    magick/pathtools.c \
    magick/pathtools.h
  apply_patch_with_msg \
    001-relocate.patch \
    002-fix-find-gdi32.patch
  autoreconf -fiv
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  cd GraphicsMagick-${pkgver}
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  ../GraphicsMagick-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-shared \
    --disable-static \
    --with-modules \
    --with-wmf \
    --with-xml \
    --with-lcms2 \
    --with-jp2 \
    --with-webp \
    --with-jbig \
    --without-fpx \
    --without-dps \
    --without-perl \
    --without-x

  make
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  make -j1 DESTDIR="${pkgdir}" install
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  # fix path references in some files.
  find ${pkgdir}${MINGW_PREFIX}/bin -name "*-config" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
  find ${pkgdir}${MINGW_PREFIX}/ -name "*.la" -exec \
    sed "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
}
