# Maintainer: Fabien Chouteau <chouteau@adacore.com>

_realname=gnatcoll
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=21.0.0
pkgrel=1

pkgdesc="The GNAT Components Collection (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-gpl")
url="https://github.com/AdaCore/gnatcoll-core"
license=('GPL3 with GCC exception')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
             "${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-python3")
depends=("${MINGW_PACKAGE_PREFIX}-libgpr")
source=("gnatcoll-core.tar.gz::https://github.com/AdaCore/gnatcoll-core/archive/v${pkgver}.tar.gz"
        "gnatcoll-bindings.tar.gz::https://github.com/AdaCore/gnatcoll-bindings/archive/v${pkgver}.tar.gz"
        'gmp.gpr'
        'iconv.gpr')
sha256sums=('daa9b3d5fc5d90b015ece1b48d54192bd06a71fec64bdfc242066f2598c30ff5'
            '53965f54647ea9c8b59856c7a00079ff5d5a473f549f2d4c9a517dffc6ee7c8f'
            'd2456f318fee53f010dcf9e236f1a06dfcec51a541e80b8eaf8c3b58bbce9318'
            'c0951f33fcc65ee9a333621135941e5ffee7884445e943538ec744762d628f26')
options=('strip')

prepare() {
  echo "${pkgver}" > "${srcdir}/gnatcoll-bindings-${pkgver}/version_information"
  echo "${pkgver}" > "${srcdir}/gnatcoll-core-${pkgver}/version_information"
}

build() {
  cd "${srcdir}/gnatcoll-core-${pkgver}"

  make BUILD=PROD PROCESSORS="$(nproc)" ENABLE_SHARED=yes

  export "GPR_PROJECT_PATH=${srcdir}/gnatcoll-core-${pkgver}"

  # Build bindings components
  for component in gmp iconv; do
    cd "${srcdir}/gnatcoll-bindings-${pkgver}/${component}"
    python3 setup.py build --jobs="$(nproc)" --reconfigure
  done
}

package() {

  cd "${srcdir}/gnatcoll-core-${pkgver}"

  make install BUILD=PROD PROCESSORS="$(nproc)" ENABLE_SHARED=yes prefix="${pkgdir}${MINGW_PREFIX}"

  export "GPR_PROJECT_PATH=${pkgdir}${MINGW_PREFIX}/share/gpr/"

  # Install bindings components
  for component in gmp iconv; do
    cd "${srcdir}/gnatcoll-bindings-${pkgver}/${component}"
    python3 setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"
  done

  # In GNAT Community gnatcoll_gmp is built with libgmp embedded in
  # libgnatcoll_gmp.dll This is not what we want here, so we install
  # a gmp.gpr with Externally_Built attribute. Same for iconv.

  # Copy external gpr files
  cp -pf "${srcdir}/gmp.gpr" "${pkgdir}${MINGW_PREFIX}/share/gpr/"
  cp -pf "${srcdir}/iconv.gpr" "${pkgdir}${MINGW_PREFIX}/share/gpr/"

  # Add with for external gpr files
  sed -i 's/with "gnatcoll";/with "gnatcoll"; with "gmp";/' "${pkgdir}${MINGW_PREFIX}/share/gpr/gnatcoll_gmp.gpr"
  sed -i 's/with "gnatcoll";/with "gnatcoll"; with "iconv";/' "${pkgdir}${MINGW_PREFIX}/share/gpr/gnatcoll_iconv.gpr"

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