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

_realname=openjpeg
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.5.2
pkgrel=7
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}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("dos2unix"
            "${MINGW_PACKAGE_PREFIX}-cmake"
            "${MINGW_PACKAGE_PREFIX}-gcc"
            "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('staticlibs' 'strip')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/uclouvain/openjpeg/archive/version.${pkgver}.tar.gz
        "openjpeg-1.5.1_tiff-pkgconfig.patch"
        "mingw-install-pkgconfig-files.patch"
        "versioned-dlls-mingw.patch"
        "openjpeg-1.5.2-stdcall-for-all-win.patch")

sha256sums=('3734e95edd0bef6e056815591755efd822228dc3cd866894e00a2c929026b16d'
            '0ba9e37ef0f38b2d7aa5b48160edca18646d88728705d7b87067765ae21fbddd'
            'a7409576161f5331ef6fa568a2591e27c411477e5180d2d350bc620f8a0600e7'
            '06d2043cc95f36ac8456939f29666699dd868bced9be64c2302a21f72221a36b'
            '7be4e0f98ddb9f120732309a402933cccd1a072c659abc02375726dabafad16c')

prepare() {
  cd "${srcdir}/${_realname}-version.${pkgver}"
  # Fix CRLF line endings
  dos2unix libopenjpeg/opj_malloc.h
  #rm -r thirdparty
  patch -Np1 -i "${srcdir}"/openjpeg-1.5.1_tiff-pkgconfig.patch
  patch -Np1 -i "${srcdir}"/mingw-install-pkgconfig-files.patch
  patch -Np1 -i "${srcdir}"/versioned-dlls-mingw.patch
  patch -Np1 -i "${srcdir}"/openjpeg-1.5.2-stdcall-for-all-win.patch

  #autoreconf -i
}

build() {
  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_TESTING=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DOPENJPEG_INSTALL_SUBDIR="openjpeg/${ver}" \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=ON \
    -DBUILD_SHARED_LIBS=ON \
    ../${_realname}-version.${pkgver}
  make

  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_TESTING=OFF \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -DOPENJPEG_INSTALL_SUBDIR="openjpeg/${ver}" \
    -DBUILD_MJ2=ON \
    -DBUILD_JPWL=ON \
    -DBUILD_JPIP=OFF \
    -DBUILD_SHARED_LIBS=OFF \
    ../${_realname}-version.${pkgver}
  make
}

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

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

  # Fix conflict with openjpeg2
  mv ${pkgdir}${MINGW_PREFIX}/bin/opj_dec_server{,1}.exe
}
