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

_realname=jasper
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.0.32
pkgrel=1
pkgdesc="A software-based implementation of the codec specified in the emerging JPEG-2000 Part-1 standard (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.ece.uvic.ca/~mdadams/jasper"
license=("custom:JasPer2.0")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo")
options=('staticlibs' 'strip' '!libtool')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/mdadams/jasper/archive/version-${pkgver}.tar.gz
        jasper-1.900.1-fix-filename-buffer-overflow.patch
        001-mingw-cmake.patch
        004-jasper-exports.patch
        jasper-static-fix.patch)
sha256sums=('a3583a06698a6d6106f2fc413aa42d65d86bedf9a988d60e5cfa38bf72bc64b9'
            'f51377e9b3e4faaa6b17b2d5fcf6f6d94fe2916a65dc9c78b5a99b891f5726dc'
            '2b261c9b40b973d0d11f7b2b6842b36aee45657cbd5e0780fa73cb184f570b65'
            'aef39fbaf65c0453a785eb563253f2bb66806ddbebcd836b8bd8dce67c7059eb'
            '3102b4175d714df84a63b8ebf3c0f346fc09a0a784a560917994f6074d6b1697')

prepare() {
  cd "$srcdir"/${_realname}-version-${pkgver}

  patch -p1 -i "${srcdir}"/jasper-1.900.1-fix-filename-buffer-overflow.patch
  patch -p1 -i "${srcdir}"/001-mingw-cmake.patch
  patch -p1 -i "${srcdir}"/004-jasper-exports.patch

  patch -p1 -i "${srcdir}"/jasper-static-fix.patch
}

build() {
  cd "${srcdir}"/${_realname}-version-${pkgver}
  [[ -d "${srcdir}"/build-${CARCH}-shared ]] && rm -rf "${srcdir}"/build-${CARCH}-shared
  [[ -d "${srcdir}"/build-${CARCH}-static ]] && rm -rf "${srcdir}"/build-${CARCH}-static
  mkdir -p "${srcdir}"/build-${MINGW_CHOST}-shared "${srcdir}"/build-${MINGW_CHOST}-static

  msg2 "Building static lib..."
  (cd "${srcdir}"/build-${MINGW_CHOST}-static
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=OFF \
      ../${_realname}-version-${pkgver}
    make
  )

  msg2 "Building shared lib..."
  (cd "${srcdir}"/build-${MINGW_CHOST}-shared
    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_INSTALL_LIBDIR=lib \
      -DCMAKE_BUILD_TYPE=Release \
      -DJAS_ENABLE_OPENGL=OFF \
      -DJAS_ENABLE_LIBJPEG=ON \
      -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF \
      -DCMAKE_SKIP_RPATH=ON \
      -DJAS_ENABLE_SHARED=ON \
      ../${_realname}-version-${pkgver}
    make
  )
}

check() {
  cd "${srcdir}"/build-${MINGW_CHOST}-static
  make -j1 test || true
}

package() {
  cd "${srcdir}"/build-${MINGW_CHOST}-static
  make DESTDIR="${pkgdir}" install
  cd "${srcdir}"/build-${MINGW_CHOST}-shared
  make DESTDIR="${pkgdir}" install
  cd "${srcdir}"/${_realname}-version-${pkgver}
  install -Dm 644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
