# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=sdl12-compat
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=1.2.68
pkgrel=1
pkgdesc="SDL 1.2 runtime compatibility library using SDL 2.0"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/libsdl-org/sdl12-compat"
license=('spdx:Zlib')
conflicts=("${MINGW_PACKAGE_PREFIX}-SDL")
provides=("${MINGW_PACKAGE_PREFIX}-SDL")
depends=(
  "${MINGW_PACKAGE_PREFIX}-SDL2"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  "${MINGW_PACKAGE_PREFIX}-cc")
source=("https://github.com/libsdl-org/sdl12-compat/archive/release-${pkgver}.tar.gz"
        "0001-just-build-normal.patch"
        "0002-sdl-config-win-paths.patch")
sha256sums=('63c6e4dcc1154299e6f363c872900be7f3dcb3e42b9f8f57e05442ec3d89d02d'
            'a368e638dd050042c3811087a63670d61c0234ac252b1f5c10212b4eb835c47c'
            '731f9475c171b034c10d57d2c13fb822cf46fba61b5fc938365c11ca70b2fba7')

prepare() {
  cd "${_realname}-release-${pkgver}"

  patch -Np1 -i "${srcdir}/0001-just-build-normal.patch"
  patch -Np1 -i "${srcdir}/0002-sdl-config-win-paths.patch"
}

build() {
  cd "${_realname}-release-${pkgver}"
  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[@]}" \
      -DBUILD_SHARED_LIBS=ON \
      "../${_realname}-release-${pkgver}"

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

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

  "${MINGW_PREFIX}"/bin/ctest.exe . || warning "Tests failed"
}

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

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

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