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

_realname=dosbox-staging
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.82.2
pkgrel=1
pkgdesc="Emulator with builtin DOS for running DOS Games (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/dosbox-staging/dosbox-staging'
license=('spdx:GPL-2.0-or-later')
depends=(
    "${MINGW_PACKAGE_PREFIX}-fluidsynth"
    "${MINGW_PACKAGE_PREFIX}-libpng"
    "${MINGW_PACKAGE_PREFIX}-opusfile"
    "${MINGW_PACKAGE_PREFIX}-SDL2"
    "${MINGW_PACKAGE_PREFIX}-SDL2_image"
    "${MINGW_PACKAGE_PREFIX}-SDL2_net"
    "${MINGW_PACKAGE_PREFIX}-zlib"
    "${MINGW_PACKAGE_PREFIX}-libslirp"
    "${MINGW_PACKAGE_PREFIX}-munt-mt32emu"
    "${MINGW_PACKAGE_PREFIX}-speexdsp"
    "${MINGW_PACKAGE_PREFIX}-iir"
)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
source=("https://github.com/dosbox-staging/dosbox-staging/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "enet.h::https://github.com/zpl-c/enet/raw/refs/tags/v2.6.5/include/enet.h")
sha256sums=('387c97b373c3164ab5abbbc2b210bf94b5567057abe44ee1e8b4d4e725bd422c'
            '6cb8f4606f064650e46d7d295c19f2885aa94eb07b6222c5c9c97ecf6c493603')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  bsdtar -xf "${_realname}-${pkgver}.tar.gz" || bsdtar -xf "${_realname}-${pkgver}.tar.gz"

  cd "${_realname}-${pkgver}"
  echo -e "#!python\nprint('${pkgver}')" > scripts/get-version.sh

  # Use newer enet.h v2.6.5 (from https://github.com/zpl-c/enet), because that
  # has some fixes which are not in the older version 2.3.6 used by dosbox-staging
  # and would cause compile errors instead.
  cp ${srcdir}/enet.h src/libs/enet/include/enet.h
}

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

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("--buildtype=release")
  else
    extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    "${extra_config[@]}" \
    -Dunit_tests=disabled \
    -Ddefault_library=shared \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

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

  ${MINGW_PREFIX}/bin/meson test
}

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install

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