# Maintainer: Philipp Smirnov https://github.com/sad-poet

_repo_name=spawn
_realname=ada_spawn
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=25.0.0
pkgrel=3
pkgdesc="Ada API to spawn processes and communicate with them (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/spawn"
msys2_references=(
  'aur: ada_spawn'
  'gentoo: dev-ada/spawn'
)
license=('spdx:Apache-2.0 WITH LLVM-exception')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-gtkada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild")
source=("https://github.com/AdaCore/spawn/archive/v${pkgver}/${_repo_name}-${pkgver}.tar.gz"
        '001-Fix-imports-in-test-projects.patch')
sha256sums=('b6263675a7e100b25622832053fb57076953052cd6802bc36a2b4088bfcf8d03'
            'cab1eb5283dcdbe5b4dc02f7560e4c65e5765fe4ab61b00cc28264083bdb44ef')

_library_types="static static-pic relocatable"
_build_mode="prod"

prepare() {
  cd "${srcdir}/${_repo_name}-${pkgver}"

  # Workaround for https://github.com/AdaCore/spawn/issues/33
  patch -p1 -i "${srcdir}"/001-Fix-imports-in-test-projects.patch
}

build() {
  [[ -d "${srcdir}"/build-${MSYSTEM} ]] && rm -rf "${srcdir}"/build-${MSYSTEM}
  cp -r "${srcdir}/spawn-${pkgver}" "${srcdir}"/build-${MSYSTEM}
  cd "${srcdir}"/build-${MSYSTEM}

  for _library_type in ${_library_types}; do
    echo "Building ${_library_type}:"
    gprbuild -p -P gnat/spawn.gpr \
      -j"$(nproc)" \
      -XBUILD_MODE="${_build_mode}" \
      -XLIBRARY_TYPE="${_library_type}"
    rm -rf obj/
  done

  LIBRARY_TYPE=relocatable \
  gprbuild -P gnat/spawn_glib.gpr
}

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

  for _library_type in ${_library_types}; do
    echo "Installing ${_library_type}:"
    gprinstall -f -p -P gnat/spawn.gpr \
      --prefix="${pkgdir}${MINGW_PREFIX}" \
      -XBUILD_MODE="${_build_mode}" \
      -XLIBRARY_TYPE="${_library_type}" \
      --build-var=LIBRARY_TYPE \
      --build-name=${_library_type}
  done

  LIBRARY_TYPE=relocatable \
  gprinstall -p --prefix="${pkgdir}${MINGW_PREFIX}" gnat/spawn_glib.gpr

  # Copy License Files
  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}" \
    "${srcdir}/${_repo_name}-${pkgver}"/LICENSE*
}
