# Maintainer: Peter Budai <peterbudai at hotmail dot com>

_realname=libgd
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.3.1
pkgrel=1
pkgdesc="GD is an open source code library for the dynamic creation of images by programmers (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://libgd.github.io/"
license=("GPLv2+")
depends=("${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-libimagequant"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-xpm-nox"
         "${MINGW_PACKAGE_PREFIX}-zlib")
replaces=("${MINGW_PACKAGE_PREFIX}-gd")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/libgd/libgd/releases/download/gd-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "libgd-export-pkg-config-build-in-subdirs.patch"
        "mingw-replace-posix_memalign.patch"
        "mingw-getline-link.patch")
sha256sums=('e904a35fd3379ddb2d7c64f929b7cbdf0422863646dae252be0029b9e47c9fe3'
            'cd8b1a2dc1515d717c7359d787ff08138af449512ed9280859b5a630886a65e1'
            'c3e69c7e59c280e5cc561c984c906b5cfa5ee56aff0b173c0febfdbf90cb048f'
            'b52ec6485d43b511b9b399cda41fc85d5f15f5ebccad1dfc76bb89b451aae512')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  #This patch actually does more than simply export dirs into a subdir
  #It also is responsible for creating the pkg_config file and now also
  #the gdlib-config file that some build scripts seem to still require
  #in spite of that mechanism being depreciated.
  patch -Nbp1 -i "${srcdir}/libgd-export-pkg-config-build-in-subdirs.patch"
  patch -Nbp1 -i "${srcdir}/mingw-replace-posix_memalign.patch"
  patch -Nbp1 -i "${srcdir}/mingw-getline-link.patch"

}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p ${srcdir}/build-${MINGW_CHOST} && cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe             \
    -GNinja                                 \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX}  \
    -DBUILD_{SHARED,STATIC}_LIBS=ON         \
    -DENABLE_FONTCONFIG=ON                  \
    -DENABLE_PNG=ON                         \
    -DENABLE_JPEG=ON                        \
    -DENABLE_TIFF=ON                        \
    -DENABLE_ICONV=ON                       \
    -DENABLE_XPM=ON                         \
    -DENABLE_FREETYPE=ON                    \
    -DENABLE_WEBP=ON                        \
    -DENABLE_LIQ=ON                         \
    -DENABLE_GD_FORMATS=ON                  \
    -DXPM_XPM_INCLUDE_DIR="${MINGW_PREFIX}/include/X11" \
    -DWEBP_INCLUDE_DIR="${MINGW_PREFIX}/include/webp/" \
    -DLIQ_INCLUDE_DIR="${MINGW_PREFIX}/include/libimagequant" \
    -DFREETYPE_INCLUDE_DIR_freetype2="${MINGW_PREFIX}/include/freetype2" \
    -DFREETYPE_INCLUDE_DIRS="${MINGW_PREFIX}/include/freetype2" \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  files=("/lib/pkgconfig/gdlib.pc")
  for f in "${files[@]}"
  do
    sed -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}${f}
  done
}
