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

_realname=DevIL
pkgbase=mingw-w64-devil
pkgname="${MINGW_PACKAGE_PREFIX}-devil"
pkgver=1.8.0
pkgrel=7
pkgdesc="Library for reading several different image formats (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://openil.sourceforge.io/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
depends=("${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libsquish"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-zlib")
provides=("${MINGW_PACKAGE_PREFIX}-DevIL")
conflicts=("${MINGW_PACKAGE_PREFIX}-ResIL")
options=('staticlibs' 'strip')
source=("https://downloads.sourceforge.net/openil/DevIL-${pkgver}.tar.gz"
        0001-Add-missing-export-function.patch
        0002-FindOpenEXR-cmake-module-define-OPENEXR_INCLUDE_DIR-.patch
        0003-Search-openexr-includes-also-in-subfolder.patch
        0004-Fix-export-in-ILUT.patch
        0005-Guard-compile-flags-for-MSVC.patch
        0006-ILUT-need-opengl-and-glut-for-linking.patch
        0007-Use-standart-library-name-for-mingw-builds.patch
        0008-fix-static-build.patch
        0009-fix-build.patch)
sha256sums=('0075973ee7dd89f0507873e2580ac78336452d29d34a07134b208f44e2feb709'
            'd494a1d0ad48fb28b339b55a83ee2b602ee03bd0e52650dabebf5edd325b3606'
            '7d68730f933883e3353ad0ba221efc3e02463202bad2f92008c33145e93a00c5'
            'bcade972d44dbba5858c1cdffe661ad858b0d416a9d327724c067a7e6b382d7a'
            'c6716a4ba16892a79b4bbe606d6cd0d192d0af81743354d03fadceacba6a9ea5'
            '3a8f923ddb94f4ec7c033e48727dc8770692a9cb3edab0bd492961456d3e2f11'
            'ad8d05e1f41e40e74a1a2cd1ed75615f210b2cada296c10641c5894b51ff060a'
            '9b0276e5749160bcd48cee5780c53492758447c4a29ccb118fd68ce44fae68b7'
            '41930eea658164cef490129d477a8e9a2227e5c1dddb84f78bcd401602d68982'
            '0caad5bb3d5ea14078f915ff100d219e3d82b440a23f8eb0e00663fc93f5e367')

prepare() {
  cd "${srcdir}/${_realname}/"
  patch -p1 -i ${srcdir}/0001-Add-missing-export-function.patch
  patch -p1 -i ${srcdir}/0002-FindOpenEXR-cmake-module-define-OPENEXR_INCLUDE_DIR-.patch
  patch -p1 -i ${srcdir}/0003-Search-openexr-includes-also-in-subfolder.patch
  patch -p1 -i ${srcdir}/0004-Fix-export-in-ILUT.patch
  patch -p1 -i ${srcdir}/0005-Guard-compile-flags-for-MSVC.patch
  patch -p1 -i ${srcdir}/0006-ILUT-need-opengl-and-glut-for-linking.patch
  patch -p1 -i ${srcdir}/0007-Use-standart-library-name-for-mingw-builds.patch
  patch -p1 -i ${srcdir}/0008-fix-static-build.patch
  patch -p1 -i ${srcdir}/0009-fix-build.patch
}

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

  export OPENEXR_DIR=${MINGW_PREFIX}
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ../${_realname}/${_realname}
    
  # if [[ $CARCH == i686 ]]; then
    # local _conf="--disable-sse3"
  # else
    # local _conf=""
  # fi
  # export lt_cv_deplibs_check_method='pass_all'
  # CXXFLAGS+=" -std=c++11"

  
  # ../${_realname}/${_realname}/configure \
    # --prefix=${MINGW_PREFIX} \
    # --build=${MINGW_CHOST} \
    # --host=${MINGW_CHOST} \
    # --target=${MINGW_CHOST} \
    # --enable-ILU \
    # --enable-ILUT \
    # --enable-directx8=no \
    # --enable-directx9=yes \
    # --enable-wdp=no \
    # --disable-allegro \
    # --with-sdl-prefix=${MINGW_PREFIX} \
    # --without-x \
    # ${_conf}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
