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

_realname=osgocean
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
_ver_base=1.0.1
pkgver=1.0.1.r164
pkgrel=2
pkgdesc="An ocean rendering nodekit for OpenSceneGraph (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
license=('spdx:LGPL-3.0-or-later')
url="https://github.com/kbale/osgocean/"
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
depends=("${MINGW_PACKAGE_PREFIX}-fftw"
         "${MINGW_PACKAGE_PREFIX}-OpenSceneGraph")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "git"
             "unrar")
_commit='f235e99977cb868f9f8e1ec5cb0d74006faf4e26'
source=("${_realname}"::"git+https://github.com/kbale/osgocean.git#commit=${_commit}"
        "https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/osgocean/osgOcean-Resources-1.0.1.rar"
        "000-mingw-w64-cmake.patch"
        "001-posix-layout.patch"
        "002-pkgconfig.patch"
        "003-exports.patch"
        "004-fftw3-no-version-suffix.patch"
        "005-provide-OSGSHADOW_LIBRARY_DEBUG.patch")
sha256sums=('SKIP'
            'b3093a48259c3df5af99841934be0468691e506cfbb82eae76eadba4ab71faaf'
            '6d87aae193bbd49c696aa3e78cf8f98c1df6a5459ffd100acb2db748f6f602f7'
            '7aa8298607b760848a30088ca5e8f1df3446c3a690758351d759924342f62c62'
            '7edc108c3a74233d9f3abe0e7022c60de4e8ffc74a7e7b3139a7ce8a09d5bbb5'
            '6a35d93047da6da41adf951d0f992a5f1d532db5c3c4a0d061591780d5604e0e'
            '2d57a2a6234feb6ba0beb073f2a91268c52bb2520daf85d8ec1ff76eac0339bd'
            '13571d5990c47d6723e9bec3328d77ce3c398f1990d69c5ea26dea75cee5e10b')
noextract=( 'osgOcean-Resources-1.0.1.rar' )

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s" "${_ver_base}" "$(git rev-list --count HEAD)"
}

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/${_realname}"/resources
  yes | unrar x "${srcdir}/osgOcean-Resources-1.0.1.rar"
  cd ../
  apply_patch_with_msg \
    000-mingw-w64-cmake.patch \
    001-posix-layout.patch \
    002-pkgconfig.patch \
    003-exports.patch \
    004-fftw3-no-version-suffix.patch \
    005-provide-OSGSHADOW_LIBRARY_DEBUG.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  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=" \
  "${MINGW_PREFIX}"/bin/cmake.exe \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "${_extra_config[@]}" \
    -DUSE_FFTW3=ON \
    -DUSE_FFTSS=OFF \
    ../${_realname}

  "${MINGW_PREFIX}"/bin/cmake.exe --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .
}
