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

_realname=OpenSceneGraph
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}" "${MINGW_PACKAGE_PREFIX}-${_realname}-debug")
_pkgver=3.6.5
pkgver=${_pkgver//-/}
pkgrel=6
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="http://www.openscenegraph.org/"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cyrus-sasl")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-collada-dom-svn"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fltk"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gdal"
         "${MINGW_PACKAGE_PREFIX}-giflib"
         "${MINGW_PACKAGE_PREFIX}-gstreamer"
         "${MINGW_PACKAGE_PREFIX}-gtk2"
         "${MINGW_PACKAGE_PREFIX}-gtkglext"
         "${MINGW_PACKAGE_PREFIX}-jasper"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libvncserver"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-lua"
         "${MINGW_PACKAGE_PREFIX}-SDL"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-poppler"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-wxWidgets"
         "${MINGW_PACKAGE_PREFIX}-zlib")
options=('staticlibs' 'strip')
source=(https://github.com/openscenegraph/OpenSceneGraph/archive/${_realname}-${_pkgver}.tar.gz
        find-collada.patch
        find-freetype.patch
        additional-gl-header.patch
        add-gstreamer-cflags.patch
        las-link-with-boost.patch)
sha256sums=('aea196550f02974d6d09291c5d83b51ca6a03b3767e234a8c0e21322927d1e12'
            '24f64f975e776fc018f9c65a1bcae043dded1d4174342d863f9f205b013acc13'
            '3c7469cf0741905f2c46e4632159220213532d8f194b711b0600b50ad94ab4f8'
            '76eafa53bfc4bb9e0260bf1835622e46210b61cf244a0440beceed9969eeaca7'
            'feb994964fe636e4591394a4c287d27d54cf61c8e5fc5324aebb161cafe2e3dc'
            '54457dcee1cdd1c486607640e4e0272e619f445291ec4edd04dda99e3c4eb4f7')

prepare() {
  cd "${srcdir}/${_realname}-${_realname}-${_pkgver}"
  patch -p1 -i ${srcdir}/find-collada.patch
  patch -p1 -i ${srcdir}/find-freetype.patch
  patch -p1 -i ${srcdir}/additional-gl-header.patch
  patch -p1 -i ${srcdir}/add-gstreamer-cflags.patch
  patch -p1 -i ${srcdir}/las-link-with-boost.patch
}

build() {
  export FREETYPE_DIR=${MINGW_PREFIX}

  for builddir in {Release,Debug}-${MINGW_CHOST}; do
    [[ -d ${builddir} ]] && rm -rf ${builddir}
    mkdir -p ${builddir}
    pushd ${builddir}
      [[ "${builddir%-${MINGW_CHOST}}" == "Release" ]] && {
        local _opts="-DBUILD_OSG_EXAMPLES=ON"
      } || {
        local _opts="-DBUILD_OSG_EXAMPLES=OFF -DBUILD_OSG_APPLICATIONS=OFF"
      }

      msg "Building ${builddir%-${MINGW_CHOST}} version..."

      MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
      COLLADA_DIR=${MINGW_PREFIX} \
      ${MINGW_PREFIX}/bin/cmake.exe \
        -G"MSYS Makefiles" \
        -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
        -DCMAKE_BUILD_TYPE=${builddir%-${MINGW_CHOST}} \
        ${_opts} \
        ../${_realname}-${_realname}-${_pkgver}

      make
    popd
  done
}

package_OpenSceneGraph() {
  pkgdesc="Open source high performance 3D graphics toolkit (mingw-w64)"

  cd Release-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  install -Dm644 ${srcdir}/${_realname}-${_realname}-${_pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}

package_OpenSceneGraph-debug() {
  depends=("${MINGW_PACKAGE_PREFIX}-${_realname}=${pkgver}")
  options=('staticlibs' '!strip')
  pkgdesc="Open source high performance 3D graphics toolkit (debug) (mingw-w64)"

  cd Debug-${MINGW_CHOST}
  make DESTDIR=${pkgdir} -j1 install

  rm -rf ${pkgdir}${MINGW_PREFIX}/include
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm -rf ${pkgdir}${MINGW_PREFIX}/share

  install -Dm644 ${srcdir}/${_realname}-${_realname}-${_pkgver}/LICENSE.txt "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}-debug/LICENSE
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
