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

_realname=xdiff
pkgbase=mingw-w64-ada-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-ada-${_realname}
pkgver=26.0.r3.13326c9
pkgrel=1
_branch=26.0
_commit=13326c907e0f81eb1ef2276b5c2e41ac87627cd6
pkgdesc="Ada binding to the libfswatch library (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/xdiff"
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "git")
options=(!emptydirs)
source=("${_realname}"::"git+https://github.com/AdaCore/xdiff.git#commit=${_commit}")
sha256sums=('9094621fec863d22732005a5e2b37d961ea611d61fa3da24950e9c30e058fabe')

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

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

pkgver() {
  cd "${srcdir}/${_realname}"
  printf "%s.r%s.%s" "$_branch" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/${_realname}"
}

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

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

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

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

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

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

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