# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=gnatcoll-bindings
pkgbase=mingw-w64-${_realname}
pkgname=(${MINGW_PACKAGE_PREFIX}-gnatcoll-gmp
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-iconv
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-lzma
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-omp
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-python
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-readline
         ${MINGW_PACKAGE_PREFIX}-gnatcoll-zlib)
pkgver=25.0.0
pkgrel=3
pkgdesc="GNAT Components Collection - Language and library bindings. (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64')
url="https://github.com/AdaCore/gnatcoll-bindings"
msys2_references=(
  'aur: gnatcoll-bindings'
  'gentoo: dev-ada/gnatcoll-bindings'
)
license=('spdx:GPL-3.0-or-later WITH GCC-exception-3.1')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-ada"
         "${MINGW_PACKAGE_PREFIX}-cc-libs"
         "${MINGW_PACKAGE_PREFIX}-gnatcoll-core")
makedepends=("${MINGW_PACKAGE_PREFIX}-gprbuild"
             "${MINGW_PACKAGE_PREFIX}-gmp"
             "${MINGW_PACKAGE_PREFIX}-libiconv"
             "${MINGW_PACKAGE_PREFIX}-omp"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-readline"
             "${MINGW_PACKAGE_PREFIX}-xz"
             "${MINGW_PACKAGE_PREFIX}-zlib")
source=("https://github.com/AdaCore/gnatcoll-bindings/archive/v${pkgver}/gnatcoll-bindings-${pkgver}.tar.gz"
        '001-Fix-setup.py-for-cross-volume-builds.patch')
sha256sums=('80ddd7f20adf45f0e155cd43e0a5ba752020616af0b0700447563776acd688a3'
            '93254d329f0ce785b62d43e772b972b8717ff5536ae960de31dfe796cfc46d01')

prepare() {
  cd "${srcdir}/gnatcoll-bindings-${pkgver}"

  # Fix setup.py, which caused the CI pipeline in github.com/MSYS2/MINGW-packages to fail
  # when the MSYS2 installation and package sources were on different volumes:
  #
  #   Traceback:
  #     ...
  #     File "C:/_/B/src/gnatcoll-bindings-24.0.0/python3/setup.py", line 45, in relocate
  #       rel_path = os.path.relpath(path, build_prefix)
  #                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  #     File "<frozen ntpath>", line 778, in relpath
  #   ValueError: path is on mount 'D:', start on mount 'C:'
  #
  patch -p1 -i "${srcdir}/001-Fix-setup.py-for-cross-volume-builds.patch"

  rm -r python && mv python3 python

  echo "${pkgver}" > "${srcdir}/gnatcoll-bindings-${pkgver}/version_information"
}

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

  for _lib in gmp iconv lzma omp python zlib; do
    cd ${_lib}
    ${MINGW_PREFIX}/bin/python setup.py build --jobs="$(nproc)" --reconfigure
    cd ..
  done

  cd readline
  ${MINGW_PREFIX}/bin/python setup.py build --accept-gpl --jobs="$(nproc)" --reconfigure
}

package_gnatcoll-gmp() {
  pkgdesc="GNAT Components Collection - Bindings to GMP (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-gmp")

  cd "${srcdir}"/build-${MSYSTEM}/gmp

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-gmp" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-iconv() {
  pkgdesc="GNAT Components Collection - Bindings to Iconv (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-libiconv")

  cd "${srcdir}"/build-${MSYSTEM}/iconv

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-iconv" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-lzma() {
  pkgdesc="GNAT Components Collection - Bindings to LZMA (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-xz")

  cd "${srcdir}"/build-${MSYSTEM}/lzma

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-lzma" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-omp() {
  pkgdesc="GNAT Components Collection - Bindings to OpenMP (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-omp")

  cd "${srcdir}"/build-${MSYSTEM}/omp

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-omp" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-python() {
  pkgdesc="GNAT Components Collection - Bindings to Python (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-python")

  cd "${srcdir}"/build-${MSYSTEM}/python

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  sed -e "s|\"-L${PREFIX_WIN}/lib\", ||g" -i "${pkgdir}"${MINGW_PREFIX}/share/gpr/gnatcoll_python.gpr
  sed -e "s|-I${PREFIX_WIN}/include|-I../../include|g" -i "${pkgdir}"${MINGW_PREFIX}/share/gpr/gnatcoll_python.gpr

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-python" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-readline() {
  pkgdesc="GNAT Components Collection - Bindings to readline (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-readline")

  cd "${srcdir}"/build-${MSYSTEM}/readline

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-readline" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

package_gnatcoll-zlib() {
  pkgdesc="GNAT Components Collection - Bindings to Zlib (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-zlib")

  cd "${srcdir}"/build-${MSYSTEM}/zlib

  ${MINGW_PREFIX}/bin/python setup.py install --prefix="${pkgdir}${MINGW_PREFIX}"

  install -Dm644 -t "${pkgdir}${MINGW_PREFIX}/share/licenses/gnatcoll-zlib" \
    "${srcdir}/gnatcoll-bindings-${pkgver}"/COPYING*
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
