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

_realname=ada_language_server
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=2026.0.202510141
pkgrel=1
pkgdesc="Server implementing the Microsoft Language Server Protocol (LSP) for Ada and SPARK (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/ada_language_server"
msys2_references=(
  'aur: ada_language_server'
  'gentoo: dev-ada/ada_language_server'
)
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-ada_spawn"
         "${MINGW_PACKAGE_PREFIX}-ada-libfswatch"
         "${MINGW_PACKAGE_PREFIX}-ada-xdiff"
         "${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-gnatdoc"
         "${MINGW_PACKAGE_PREFIX}-gnatformat"
         "${MINGW_PACKAGE_PREFIX}-gpr"
         "${MINGW_PACKAGE_PREFIX}-lal-refactor"
         "${MINGW_PACKAGE_PREFIX}-libadalang"
         "${MINGW_PACKAGE_PREFIX}-libadalang-tools"
         "${MINGW_PACKAGE_PREFIX}-libgpr"
         "${MINGW_PACKAGE_PREFIX}-prettier-ada"
         "${MINGW_PACKAGE_PREFIX}-vss-extra"
         "${MINGW_PACKAGE_PREFIX}-vss-text"
         "${MINGW_PACKAGE_PREFIX}-xmlada")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-python")
source=("https://github.com/AdaCore/ada_language_server/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-revert-renaming-ada_xdiff-to-xdiff.patch")
sha256sums=('6a94ffd5acf7fb1eaab772605f94d9091bbe05746ace221fa6ac5bb281b2f13e'
            'cf30836c59c3a1c89dffe759b785814f0b60a40eb5620879a49ad365620b5b8f')

prepare() {
  cd ${_realname}-${pkgver}
  # Revert https://github.com/AdaCore/ada_language_server/commit/38374bb2 to build with xdiff 26
  patch -p1 -i "${srcdir}"/001-revert-renaming-ada_xdiff-to-xdiff.patch
}

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

  make \
    BUILD_MODE=prod \
    LIBRARY_TYPE=relocatable \
    VERSION="${pkgver}" \
    BUILD_DATE="$(LANG=C date '+%Y%m%d')"

  LIBRARY_TYPE=relocatable \
  gprbuild gnat/lsp_client_glib.gpr
}

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

  make install \
    prefix="${pkgdir}${MINGW_PREFIX}" \
    BUILD_MODE=prod \
    LIBRARY_TYPE=relocatable

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

  rm -fv "${pkgdir}"${MINGW_PREFIX}/share/gpr/gnatcoll.gpr
  rm -fv "${pkgdir}"${MINGW_PREFIX}/share/gpr/manifests/gnatcoll

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