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

_realname=gnatstudio
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=20250417
pkgrel=1
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}-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}-vss-extra"
         "${MINGW_PACKAGE_PREFIX}-vss-text"
         "${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"
        "003-Use-system-wide-Python-3-installation.patch"
        "004-fix-finding-gtkada.patch"
        "https://github.com/AdaCore/gnatstudio/commit/14a6978b.patch"
        "https://github.com/AdaCore/gnatstudio/commit/c6d5b25c.patch"
        "https://github.com/AdaCore/gnatstudio/commit/90b19df1.patch"
        "https://github.com/AdaCore/gnatstudio/commit/e205b4cb.patch"
        "https://github.com/AdaCore/gnatstudio/commit/25fae07c.patch"
        "https://github.com/AdaCore/gnatstudio/commit/8df27f71.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-15.2.0")
sha256sums=('a2f8e29680a6423bd78bfe56263dc69d5374ae2c16a89cec0c1b895fd6b41aea'
            'b3c2548e7f5da1b29e9038d3de48f754f7e135774993cc3058dab56d40cc6180'
            '99e7aaedac07370819c2da683380696c2f24d8d9cccd3e9602010086e980ab8e'
            'c29f8049167380e6ac5480f8c170fa21ba7419f4b057196743a4aec9a4b2ddee'
            'f350cb89250a7a26952249e1534c8284528e59a71ee56bb68c62503cdca62d2f'
            '8466ffcf8e78e3359ba826e7159dd92b4a18e011e59770a85f5b9917036932cd'
            'dcdaae45e24fdce3000d8502c16df3a4da0e62de7ba19c20cd63c8815d82cdbe'
            'f6f73e93c49225233effc8b73ee9364fe7316b82689b08b0f43b00b257aa570b'
            '42f1a3d7924c5ea8c6b4d64c0bc74216b16701391ce329eb5f6cd9bc38f3d8d9'
            'dd1dec05a8e063136b2ac9db92120255e4f29fb273daba1926cecfac8bfb71f0'
            'a7b2bcbdbe6bddcbd83dbbb180e1f69f4c82e8885226fef88d647ab44885b389')

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 \
    003-Use-system-wide-Python-3-installation.patch \
    004-fix-finding-gtkada.patch \
  # backports to build with latest AdaCore libs/tools
  apply_patch_with_msg \
    14a6978b.patch \
    c6d5b25c.patch \
    90b19df1.patch \
    e205b4cb.patch \
    25fae07c.patch \
    8df27f71.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*
}
