# 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=6
pkgdesc="Library project for developers who would like to support popular graphics image formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
license=('GPL' 'custom:FIPL')
url="https://freeimage.sourceforge.io/"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-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}-gcc"
             "${MINGW_PACKAGE_PREFIX}-iconv"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "dos2unix" "make" "lndir")
options=('strip' 'staticlibs' '!buildflags')
source=("https://downloads.sourceforge.net/sourceforge/freeimage/${_realname}${pkgver//./}.zip"
        'FreeImage-3.17.0_mingw-makefiles.patch'
        'FreeImage-3.18.0_unbundle.patch'
        'freeimage-libraw-0.20.patch')
sha256sums=('f41379682f9ada94ea7b34fe86bf9ee00935a3147be41b6569c9605a53e438fd'
            'c68da81f38e122943c3c1f0d42b67766a326a7dcfa99fe523ac2fc74e6cec965'
            '029960951b743bbf78557d78cd3ac5c417146b169f65aaf9fc2c7a914a87611b'
            '71c25974c25dfced11b08206aae6e759675b53bae061be5f34fa1107beec9233')

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

  patch -p1 -i ${srcdir}/FreeImage-3.18.0_unbundle.patch
  patch -p1 -i ${srcdir}/FreeImage-3.17.0_mingw-makefiles.patch
  # taken from Arch
  patch -p1 -i ${srcdir}/freeimage-libraw-0.20.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}/${_realname}

  mkdir -p ${srcdir}/build-${CARCH}
  lndir ${srcdir}/FreeImage ${srcdir}/build-${CARCH}

  plain "Build shared version..."
  cd ${srcdir}/build-${CARCH}
  make \
    MINGW_TARGET=${MINGW_CHOST} \
    -f Makefile.gnu

  make \
    MINGW_TARGET=${MINGW_CHOST} \
    -f Makefile.fip
}

package() {
  install -d "${pkgdir}${MINGW_PREFIX}"/lib
  install -d "${pkgdir}${MINGW_PREFIX}"/bin
  install -d "${pkgdir}${MINGW_PREFIX}"/include
  install -d "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage

  cd ${srcdir}/build-${CARCH}
  install -m755 Dist/libfreeimage-*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  install -m644 Dist/libfreeimage.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install -m755 Dist/libfreeimageplus-*.dll "${pkgdir}${MINGW_PREFIX}"/bin/
  install -m644 Dist/libfreeimageplus.dll.a "${pkgdir}${MINGW_PREFIX}"/lib/
  install -m644 Dist/FreeImage.h "${pkgdir}${MINGW_PREFIX}"/include/
  install -m644 Dist/FreeImagePlus.h "${pkgdir}${MINGW_PREFIX}"/include/

  install -m644 Whatsnew.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-fi.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-gplv2.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/
  install -m644 license-gplv3.txt "${pkgdir}${MINGW_PREFIX}"/share/doc/FreeImage/

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