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

_realname=opencollada
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.6.68
pkgrel=2
pkgdesc="Stream based reader and writer library for COLLADA files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/khronosGroup/OpenCOLLADA/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config" "${MINGW_PACKAGE_PREFIX}-cmake" "git")
depends=("${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-pcre")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/KhronosGroup/OpenCOLLADA/archive/v${pkgver}.tar.gz"
        fix-mingw-w64.patch
        fix-missing-include.patch
        fix-inttypes-mingw.patch
        fix-pcre-type-conflict.patch
        opencollada-cmake.patch
        opencollada-disable-test-mingw.patch
        use-cpp-headers.patch
        move-hash-function.patch
        disable-indentation-warnings.patch)
sha256sums=('d9db0c0a518aa6ac0359626f222707c6ca1b63a83cbf229d97a5999c9cde347b'
            '10fdc2746b83161b853df6ad4ac600252c4f964377ea0186ff97f99bd3417e34'
            '18f1bf9f57d1c67aa35927fb0d1b0b38def52b189742021f1268eb2bdbffc963'
            '6fb7cf3116bdcb93af58a2487c44b298afd11e83c21ec87a8c921eda6dcba766'
            'be24a0ccaee8f2db24acafd378326cec8f3a57cf95a5575ae613886fda68dd4a'
            'd96a0eb2c535042a053c47cc06d4edf20bb195b869e1a215747c2bf86297cc54'
            'a0eb0e2385897cd2b7e1335229128ceda0f607aa6d8f50ad152d3236811488d6'
            '8ba7875069585e99f9ebd5044ae10c7a92af5d5a7cfcd61cf69ab24cc70cddc3'
            '93eaf5c565ff30d8d37f1055a432e9516dd518ee040e9384633f55d4a9c305ca'
            '4fc1d79fc2965c4fbebb9e25d174eb48b91a2d93601dfc5417c90acd88909495')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/fix-mingw-w64.patch
  patch -p1 -i ${srcdir}/fix-missing-include.patch
  patch -p1 -i ${srcdir}/fix-inttypes-mingw.patch
  patch -p1 -i ${srcdir}/fix-pcre-type-conflict.patch
  patch -p1 -i ${srcdir}/opencollada-cmake.patch
  patch -p1 -i ${srcdir}/opencollada-disable-test-mingw.patch
  patch -p1 -i ${srcdir}/use-cpp-headers.patch
  patch -p1 -i ${srcdir}/move-hash-function.patch
  patch -p1 -i ${srcdir}/disable-indentation-warnings.patch
}

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

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
     -G"MSYS Makefiles" \
     -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
     ${extra_config} \
     ../${_realname}-${pkgver}

  make -j1
}

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

  # replace absolute paths in .cmake scripts
  sed -i "s;$(cygpath -ma "${MINGW_PREFIX}");\${_IMPORT_PREFIX};" "${pkgdir}${MINGW_PREFIX}/lib/cmake/OpenCOLLADA"/*Targets*.cmake
}
