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

_realname=openjpeg
pkgbase=mingw-w64-${_realname}2
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}2"
pkgver=2.3.1
pkgrel=1
pkgdesc="An open source JPEG 2000 codec (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.openjpeg.org/"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/uclouvain/openjpeg/archive/v${pkgver}.tar.gz
        0001-fix-install-for-dlls.all.patch
        0002-cmake-files-destination.patch
        0003-versioned-dlls.mingw.patch
        0004-mingw-disable-java.patch
        0005-sock-jpip.all.patch
        0006-openjpeg-2.1.0-stdcall-for-all-win.patch)
sha256sums=('63f5a4713ecafc86de51bfad89cc07bb788e9bba24ebbf0c4ca637621aadb6a9'
            '3f3bde353ca3432f157258164c5e3c345af82ca3a9d5a68f815c3030b01cbc32'
            '31b2cc6824b07b705ba1c05b38904c8c4fc6ba39acefebda3e6ec85dbef9a174'
            '0f95f52b6fd662acb84b481a998ec4170369fe94247e0fa227fcda1f6ba18f1b'
            'adade9b29e4292b642d547644e26fab6e193bbfe31d25f07d89a8c35e4d0993d'
            '9df169f03e6e69cba0d7d33592f9eba373a02cd9554b456d3eb9ff3d9135391b'
            'e8c375faa317c819174b50aa43b6be61c108872661b014738c4a0cfa95aac919')

# 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 "${srcdir}/${_realname}-${pkgver}"
  apply_patch_with_msg \
   0001-fix-install-for-dlls.all.patch \
   0002-cmake-files-destination.patch \
   0003-versioned-dlls.mingw.patch \
   0004-mingw-disable-java.patch \
   0005-sock-jpip.all.patch
  #patch -p1 -i ${srcdir}/0006-openjpeg-2.1.0-stdcall-for-all-win.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=OFF \
    -DBUILD_TESTING=OFF \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=OFF \
    -DBUILD_JP3D=ON \
    -DBUILD_JAVA=OFF \
    ../${_realname}-${pkgver}

  make

  msg "Build shared version"
  [[ -d ${srcdir}/build-shared-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-shared-${MINGW_CHOST}
  mkdir ${srcdir}/build-shared-${MINGW_CHOST}
  cd ${srcdir}/build-shared-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    -DBUILD_TESTING=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=ON \
    -DBUILD_JP3D=ON \
    -DBUILD_JAVA=OFF \
    -DBUILD_DOC=OFF \
    -DBUILD_PKGCONFIG_FILES=ON \
    ../${_realname}-${pkgver}
  make
}

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

  cd "${srcdir}/build-shared-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
}
