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

_realname=sfml
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-docs")
pkgver=3.0.2
pkgrel=1
pkgdesc="A simple, fast, cross-platform, and object-oriented multimedia API (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.sfml-dev.org/"
msys2_changelog_url='https://github.com/SFML/SFML/blob/master/changelog.md'
msys2_repository_url='https://github.com/SFML/SFML'
msys2_references=(
  'anitya: 12315'
  'archlinux: sfml'
  'gentoo: media-libs/libsfml'
)
license=('spdx:Zlib')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
depends=("${MINGW_PACKAGE_PREFIX}-flac"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-openal")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "unzip")
source=("https://github.com/SFML/SFML/archive/${pkgver}/SFML-${pkgver}.tar.gz"
        mingw-w64-msys2.patch
        fix-pkgconfig-generation.patch)
sha256sums=('0034e05f95509e5d3fb81b1625713e06da7b068f210288ce3fd67106f8f46995'
            'd01d85f39c7cdb149d6038dcc6c65b8a3d130c20f10ef0f68ceeffa94b0b4c4a'
            '22d5e7922539054f3c83bf6dc08ca5d01ee624557ad0c10959cd1183f0be6bdf')

prepare() {
  cd SFML-${pkgver}

  patch -p1 -i ../mingw-w64-msys2.patch
  # https://github.com/SFML/SFML/issues/3582
  patch -p1 -i ../fix-pkgconfig-generation.patch
}

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DSFML_PKGCONFIG_INSTALL_DIR=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSFML_BUILD_EXAMPLES=ON \
    -DSFML_BUILD_DOC=OFF \
    -DSFML_INSTALL_PKGCONFIG_FILES=ON \
    -DSFML_PKGCONFIG_INSTALL_DIR=${MINGW_PREFIX}/lib/pkgconfig \
    -DBUILD_SHARED_LIBS=ON \
    -DSFML_USE_SYSTEM_DEPS=ON \
    -S SFML-${pkgver} \
    -B build-${MSYSTEM}-shared

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DSFML_PKGCONFIG_INSTALL_DIR=" \
  cmake \
    -GNinja \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSFML_BUILD_EXAMPLES=OFF \
    -DSFML_BUILD_DOC=OFF \
    -DSFML_INSTALL_PKGCONFIG_FILES=ON \
    -DSFML_PKGCONFIG_INSTALL_DIR=${MINGW_PREFIX}/lib/pkgconfig \
    -DBUILD_SHARED_LIBS=OFF \
    -DSFML_USE_SYSTEM_DEPS=ON \
    -S SFML-${pkgver} \
    -B build-${MSYSTEM}-static

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

package_sfml() {
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}-static
  DESTDIR="${pkgdir}" cmake --install build-${MSYSTEM}-shared
  install -Dm644 "SFML-${pkgver}/license.md" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"

  # split docs
  mkdir -p dest${MINGW_PREFIX}/share
  mv "${pkgdir}${MINGW_PREFIX}"/share/doc dest${MINGW_PREFIX}/share/doc
}

package_sfml-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mv dest/* "${pkgdir}"
}

# 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;
