# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Miloš Komarčević <miloskomarcevic@aim.com>

_realname=openjph
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.25.3
pkgrel=1
pkgdesc="Open-source implementation of HTJ2K (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://openjph.org/"
msys2_repository_url='https://github.com/aous72/OpenJPH'
license=("spdx:BSD-2-Clause")
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-libtiff")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/aous72/OpenJPH/archive/${pkgver}/${_realname}-${pkgver}.tar.gz)
sha256sums=('815b0d345daf3bbad72f3930d4f6c831643dcb2b734d8bb44d871d68db12f4d2')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=OFF \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -S OpenJPH-${pkgver} \
    -B build-static-${MSYSTEM}

  cmake --build build-static-${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ${_extra_config[@]} \
    -DBUILD_SHARED_LIBS=ON \
    -DCMAKE_SYSTEM_PREFIX_PATH=${MINGW_PREFIX} \
    -S OpenJPH-${pkgver} \
    -B build-shared-${MSYSTEM}

  cmake --build build-shared-${MSYSTEM}
}

package() {
  DESTDIR="${pkgdir}" cmake --install build-static-${MSYSTEM}
  DESTDIR="${pkgdir}" cmake --install build-shared-${MSYSTEM}

  install -Dm644 "${srcdir}"/OpenJPH-${pkgver}/LICENSE \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
