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

_realname=graphicsmagick
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.3.31
pkgrel=1
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
url="http://www.graphicsmagick.org/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-jasper"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-libtiff"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libwebp"
             "${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}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib")
         #"${MINGW_PACKAGE_PREFIX}-perl"
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support"
            "${MINGW_PACKAGE_PREFIX}-libxml2: for XML support"
            "${MINGW_PACKAGE_PREFIX}-jasper: for JPEG-2000 support"
            "${MINGW_PACKAGE_PREFIX}-libpng: for PNG support"
            "${MINGW_PACKAGE_PREFIX}-libtiff: for PNG support"
            "${MINGW_PACKAGE_PREFIX}-libwebp: for WEBP support")
options=('staticlibs' 'strip' 'libtool')
source=(https://sourceforge.net/projects/graphicsmagick/files/${_realname}/${pkgver}/GraphicsMagick-${pkgver}.tar.xz
        001-relocate.patch)
sha256sums=('096bbb59d6f3abd32b562fc3b34ea90d88741dc5dd888731d61d17e100394278'
            'd960ba9ebb98acc922794315913e743938fb0b903aa3ab0652eb653e2641b232')

# 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
  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 {} \;
}
