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

_realname=FreeImage
pkgbase=mingw-w64-freeimage
pkgname="${MINGW_PACKAGE_PREFIX}-freeimage"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=3.18.0
pkgrel=15
libver=3
pkgdesc="Library project for developers who would like to support popular graphics image formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:FreeImage OR GPL-2.0-or-later')
url="https://freeimage.sourceforge.io/"
msys2_references=(
  "cpe: cpe:/a:freeimage_project:freeimage"
)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-jxrlib"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libraw"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-openexr")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "dos2unix"
             "lndir")
options=('!buildflags')
source=("https://downloads.sourceforge.net/sourceforge/freeimage/${_realname}${pkgver//./}.zip"
        'FreeImage-3.18.0_mingw-makefiles.patch'
        'FreeImage-3.18.0_unbundle.patch'
        'freeimage-libraw-0.20.patch' # taken from Arch
        'freeimage-libraw-0.21.patch' # taken from Arch
        'freeimage-libtiff-4.4.0.patch')
sha256sums=('f41379682f9ada94ea7b34fe86bf9ee00935a3147be41b6569c9605a53e438fd'
            'ccb04cd703530d73b123f84310287a0318e13786a7320b7e81a33f2dccb445ca'
            'a1480bc158002c9f4b093f9199ec708eb931f45ca1d7ec0af6508626c70fc0b9'
            '71c25974c25dfced11b08206aae6e759675b53bae061be5f34fa1107beec9233'
            'cacb6ae3a2cebf8208d34ea01423a5309ee88ff649afbbb371e49cfeb57d8331'
            '1f743835f4b06bf14a8ce01b7125e5eed886939dca3853814c5ca46d500d275a')

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

prepare() {
  cd "${srcdir}"/${_realname}

  apply_patch_with_msg \
    FreeImage-3.18.0_mingw-makefiles.patch \
    FreeImage-3.18.0_unbundle.patch \
    freeimage-libraw-0.20.patch \
    freeimage-libraw-0.21.patch

  # taken from https://sourceforge.net/p/freeimage/discussion/36109/thread/2018fdc6e7/
  msg2 "Applying freeimage-libtiff-4.4.0.patch"
  patch -Np2 -i "${srcdir}/freeimage-libtiff-4.4.0.patch"

  # remove all included libs to make sure these don't get used during compile
  rm -r Source/Lib* Source/ZLib Source/OpenEXR

  # clear files which cannot be built due to dependencies on private headers
  # (see also unbundle patch)
  > Source/FreeImage/PluginG3.cpp
  > Source/FreeImageToolkit/JPEGTransform.cpp

  # sanitize encodings / line endings
  for file in `find . -type f -name '*.c' -or -name '*.cpp' -or -name '*.h' -or -name '*.txt' -or -name Makefile`; do
    ${MINGW_PREFIX}/bin/iconv -f ISO-8859-15 -t UTF-8 ${file} > ${file}.new && \
    sed -i 's|\r||g' ${file}.new && \
    touch -r ${file} ${file}.new && mv ${file}.new ${file}
  done

  #cp -f "${srcdir}"/Makefile.mingw-w64 "${srcdir}"/FreeImage/Makefile.mingw-w64

  # Generate source list
  sh ./gensrclist.sh
  sh ./genfipsrclist.sh
}

build() {
  cd "${srcdir}"
  rm -rf build-${MSYSTEM} | true
  cp -r "${_realname}" "build-${MSYSTEM}"

  msg "Building freeimage..."
  cd "${srcdir}/build-${MSYSTEM}"
  make \
    CC="${CC} -std=gnu11" \
    CXX="${CXX} -std=gnu++11" \
    MINGW_TARGET=${MINGW_CHOST} -f Makefile.gnu

  msg "Building freeimageplus..."
  make \
    CC="${CC} -std=gnu11" \
    CXX="${CXX} -std=gnu++11" \
    MINGW_TARGET=${MINGW_CHOST} -f Makefile.fip
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  install -Dm755 Dist/libfreeimage-${libver}.dll "${pkgdir}${MINGW_PREFIX}/bin/libfreeimage-${libver}.dll"
  install -Dm644 Dist/libfreeimage.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libfreeimage.dll.a"
  install -Dm755 Dist/libfreeimageplus-${libver}.dll "${pkgdir}${MINGW_PREFIX}/bin/libfreeimageplus-${libver}.dll"
  install -Dm644 Dist/libfreeimageplus.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libfreeimageplus.dll.a"
  install -Dm644 Dist/FreeImage.h "${pkgdir}${MINGW_PREFIX}/include/FreeImage.h"
  install -Dm644 Dist/FreeImagePlus.h "${pkgdir}${MINGW_PREFIX}/include/FreeImagePlus.h"

  install -Dm644 Dist/libfreeimage.a "${pkgdir}${MINGW_PREFIX}/lib/libfreeimage.a"
  install -Dm644 Dist/libfreeimageplus.a "${pkgdir}${MINGW_PREFIX}/lib/libfreeimageplus.a"

  install -Dm644 license-fi.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
