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

_realname=imagemagick
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
_basever=7.0.10
_rc=-11
pkgver=${_basever}${_rc//-/.} # pkgver doesn't have "," "/", "-" and space.
pkgrel=4
pkgdesc="An image viewing/manipulation program (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.imagemagick.org/"
license=("custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-ghostscript"
             "${MINGW_PACKAGE_PREFIX}-graphviz"
             "${MINGW_PACKAGE_PREFIX}-librsvg"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-openexr"
             "${MINGW_PACKAGE_PREFIX}-pango"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-djvulibre"
         "${MINGW_PACKAGE_PREFIX}-flif"
         "${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gsfonts"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-jbigkit"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libheif"
         "${MINGW_PACKAGE_PREFIX}-liblqr"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libraqm"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libtool"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libwmf"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         #"${MINGW_PACKAGE_PREFIX}-perl"
         "${MINGW_PACKAGE_PREFIX}-ttf-dejavu"
         "${MINGW_PACKAGE_PREFIX}-xz"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd"
        )
optdepends=("${MINGW_PACKAGE_PREFIX}-ghostscript: for Ghostscript support"
            "${MINGW_PACKAGE_PREFIX}-openexr: for OpenEXR support"
            "${MINGW_PACKAGE_PREFIX}-librsvg: for SVG support"
            "${MINGW_PACKAGE_PREFIX}-graphviz: for GVC 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' '!debug' 'libtool')
source=(https://imagemagick.org/download/releases/ImageMagick-${_basever}${_rc}.tar.xz
        ImageMagick-7.0.1.3-mingw.patch
        001-7.0.4.1-relocate.patch
        002-7.0.4.1-build-fixes.patch)
sha256sums=('a06d686bbad1eae663b2571bd7bb45f0e1ba438ed2217863d2e79ab394c5dfbd'
            '313e781918c66a2acfdbd99fab3de2fd680c34efdaaab18318d7543d4c79bb3e'
            '28c54cd97706e14e631f5dd4d67b79c1680be6eacaf94c15b0f5acd8159b64b0'
            '5ad829d563aed15e6d829f81ea0fa91053108d428449e91c733c5acae5a341b3')
#Lexie Parsimoniae (ImageMagick code signing key) <lexie.parsimoniae@imagemagick.org>
validpgpkeys=('D8272EF51DA223E4D05B466989AB63D48277377A')

# 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 ImageMagick-${_basever}${_rc}
  del_file_exists \
    MagickCore/pathtools.c \
    MagickCore/pathtools.h
  apply_patch_with_msg \
    ImageMagick-7.0.1.3-mingw.patch \
    001-7.0.4.1-relocate.patch \
    002-7.0.4.1-build-fixes.patch

  autoreconf -fi
}

build() {
  export lt_cv_deplibs_check_method='pass_all'
  [[ $CARCH = "i686" ]] && EXTRAOPTS="--with-gcc-arch=i686"
  [[ $CARCH = "x86_64" ]] && EXTRAOPTS="" #EXTRAOPTS="--with-gcc-arch=x86-64"

  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  #--with-lcms2, --with-opencl, --without-ltdl
  ../ImageMagick-${_basever}${_rc}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-deprecated \
    --enable-legacy-support \
    --enable-hdri \
    --with-djvu \
    --with-fftw \
    --with-gslib \
    --with-gvc \
    --with-flif \
    --with-lcms \
    --with-lqr \
    --with-modules \
    --with-openexr \
    --with-openjp2 \
    --with-rsvg \
    --with-webp \
    --with-wmf \
    --with-xml \
    --without-autotrace \
    --without-dps \
    --without-fpx \
    --with-jbig \
    --without-perl \
    --without-x \
    --with-raqm \
    --with-magick-plus-plus \
    --with-windows-font-dir=c:/Windows/fonts \
    --with-gs-font-dir=${MINGW_PREFIX}/share/fonts/gsfonts \
    --with-dejavu-font-dir=${MINGW_PREFIX}/share/fonts/TTF \
    $EXTRAOPTS \
    CFLAGS="${CFLAGS}" CPPFLAGS="${CPPFLAGS}" LDFLAGS="${LDFLAGS}"

  if check_option "debug" "y"; then
    MAKE_VERBOSE="V=1"
  fi
  make ${MAKE_VERBOSE}

    #--enable-opencl \
    #--with-perl-options="INSTALLDIRS=vendor"
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}
  if check_option "debug" "y"; then
    MAKE_VERBOSE="V=1"
  fi
  make -j1 DESTDIR="${pkgdir}" install ${MAKE_VERBOSE}

  #find . -name "*.xml" -exec sed -i "s/${MINGW_PREFIX}/newWord/g" '{}' \;

  install -Dm644 ${srcdir}/ImageMagick-${_basever}${_rc}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
  install -Dm644 ${srcdir}/ImageMagick-${_basever}${_rc}/NOTICE  "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE"

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  # fix hard-coded pathes in XML files.
  find ${pkgdir}${MINGW_PREFIX}/lib -name "*.xml" -exec sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
  # fix libtool .la files
  find ${pkgdir}${MINGW_PREFIX}/lib -name "*.la" -exec sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i {} \;
  # fix hard-coded pathes in .pc files
  for _f in "${pkgdir}${MINGW_PREFIX}"\/lib\/pkgconfig\/*.pc; do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${_f}
  done
}
