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

_realname=gnatstudio
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=20240506
pkgrel=2
pkgdesc="GNAT Studio is a powerful and lightweight IDE for Ada and SPARK (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
msys2_references=(
  'aur: gnatstudio'
)
license=('spdx:GPL-3.0-or-later')
url="https://github.com/AdaCore/gnatstudio"
depends=("${MINGW_PACKAGE_PREFIX}-ada_language_server"
         "${MINGW_PACKAGE_PREFIX}-ada_spawn"
         "${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-db"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-python"
         "${MINGW_PACKAGE_PREFIX}-gtkada"
         "${MINGW_PACKAGE_PREFIX}-libadalang"
         "${MINGW_PACKAGE_PREFIX}-libadalang-tools"
         "${MINGW_PACKAGE_PREFIX}-libvss"
         "${MINGW_PACKAGE_PREFIX}-python-cairo"
         "${MINGW_PACKAGE_PREFIX}-python-coverage"
         "${MINGW_PACKAGE_PREFIX}-python-gobject"
         "${MINGW_PACKAGE_PREFIX}-python-jedi"
         "${MINGW_PACKAGE_PREFIX}-python-pycodestyle"
         "${MINGW_PACKAGE_PREFIX}-python-pynput"
         #"${MINGW_PACKAGE_PREFIX}-python-pyocd"
         "${MINGW_PACKAGE_PREFIX}-python-pyusb"
         "${MINGW_PACKAGE_PREFIX}-python-pyzmq"
         "${MINGW_PACKAGE_PREFIX}-python-yaml"
         "${MINGW_PACKAGE_PREFIX}-templates-parser"
         "${MINGW_PACKAGE_PREFIX}-xmlada")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
source=("https://github.com/AdaCore/gnatstudio/archive/gnatstudio-cr-${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-Honour-DESTDIR-in-installation-targets.patch"
        "002-Fix-collections.abc.MutableMapping-import-in-gs_utils-for-Python-3.10.patch"
        "003-Use-system-wide-Python-3-installation.patch"
        "004-fix-finding-gtkada.patch"
        "005-remove-duplicated-files.patch::https://github.com/AdaCore/gnatstudio/commit/cb3ab986.patch"
        "006-fix-compilation-errors.patch::https://github.com/AdaCore/gnatstudio/commit/2409233b.patch"
        "007-Rename-DAP-Utils-UTF8.patch::https://github.com/AdaCore/gnatstudio/commit/089f18a1.patch"
        "008-Adjust-to-new-values-in-Libadalang-Common-Token_Kind.patch::https://github.com/AdaCore/gnatstudio/commit/9e490f6c.patch"
        "009-Fix-GS-edge-builds.patch::https://github.com/AdaCore/gnatstudio/commit/49f8fa75.patch"
        "010-Send-GS-formatting-preferences-to-GNATFormat.patch::https://github.com/AdaCore/gnatstudio/commit/e6bc020e.patch"
        "building_executable_programs_with_gnat.rst::https://gcc.gnu.org/git?p=gcc.git;a=blob_plain;f=gcc/ada/doc/gnat_ugn/building_executable_programs_with_gnat.rst;hb=releases/gcc-13.2.0")
sha256sums=('b99d02c7bda902ff2a0e73686a7d633c346caa9bd5534bbadac1e68dd86a70e5'
            'b3c2548e7f5da1b29e9038d3de48f754f7e135774993cc3058dab56d40cc6180'
            '680470d8ee657b6102e95499023aae90115ec41cbf029e1c248376302dd81840'
            '99e7aaedac07370819c2da683380696c2f24d8d9cccd3e9602010086e980ab8e'
            'c29f8049167380e6ac5480f8c170fa21ba7419f4b057196743a4aec9a4b2ddee'
            '302156f7e366a3353ec0abb4eb1daf9f183c9518fcec1c483910ecb8823c7b60'
            '6fcad54619ab4b072af6c6be2434af852a237b3931a44c158987948526f37f56'
            '3a169b88f99c2eca83b2cef74df54e4df8b483761c03964113c93decd441258c'
            'e1bf6da075226564281d595d451be1e2ee86220732ef279e536724457aec3974'
            '92b53888105fd4115feb8f7980ccbbdc3d41fbfd3b2c72f97373e26569c2e70a'
            '837c9c74cde1d89aed5de75b31aca0c21247b8ae1579181ac87a12715c913310'
            '8bca44c461295437d14e216871cc29d183912db9dd63602671a59ea0850832bf')

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

prepare() {
  cd "${srcdir}/${_realname}-gnatstudio-cr-${pkgver}"

  apply_patch_with_msg \
    001-Honour-DESTDIR-in-installation-targets.patch \
    002-Fix-collections.abc.MutableMapping-import-in-gs_utils-for-Python-3.10.patch \
    003-Use-system-wide-Python-3-installation.patch \
    004-fix-finding-gtkada.patch \
    005-remove-duplicated-files.patch \
    006-fix-compilation-errors.patch \
    007-Rename-DAP-Utils-UTF8.patch \
    008-Adjust-to-new-values-in-Libadalang-Common-Token_Kind.patch \
    009-Fix-GS-edge-builds.patch \
    010-Send-GS-formatting-preferences-to-GNATFormat.patch

  # Enable parsing of GNAT command line switches (distrib/gnat_switches.py)
  cp "${srcdir}/building_executable_programs_with_gnat.rst" gnat/

  echo "${pkgver}" > VERSION.txt
  LANG=C date "+%Y%m%d" > DATE.txt

  autoreconf -fiv
}

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

  ./configure \
    --prefix="${MINGW_PREFIX}"

  make \
    PROCESSORS=0 \
    BUILD=Production \
    PRJ_BUILD=Release \
    LIBRARY_TYPE=relocatable \
    GPRBUILD_FLAGS="-cargs:C -Wno-implicit-function-declaration -Wno-implicit-int -gargs"
}

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

  make DESTDIR="${pkgdir}" install

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/" COPYING*
}
