# Maintainer: Tim S <stahta01@gmail.com>
# ArchLinux Contributor: Pierre-Marie de Rodat <pmderodat on #ada at freenode.net>
# ArchLinux Contributor: Rod Kay <charlie5 on #ada at freenode.net>


_realname=gprbuild-bootstrap
pkgbase=mingw-w64-${_realname}-git
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}-git
pkgver=r3206.f95f0c68
pkgrel=1
pkgdesc="Static GPRbuild to bootstrap XML/Ada and GPRbuild itself (mingw-w64)"
arch=('any')
url='https://github.com/AdaCore/gprbuild/'
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-gcc-ada")
provides=("${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
conflicts=("${MINGW_PACKAGE_PREFIX}-gprbuild"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-gpl"
           "${MINGW_PACKAGE_PREFIX}-gprbuild-bootstrap")
source=('git+https://github.com/AdaCore/gprbuild.git'
        'git+https://github.com/AdaCore/xmlada.git')
sha1sums=('SKIP'
          'SKIP')

pkgver() {
  cd "${srcdir}/gprbuild"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "${srcdir}/gprbuild"

  # GPRbuild hard-codes references to ${MINGW_PREFIX}/libexec, but MINGW packages
  # must use ${MINGW_PREFIX}/lib instead.
  sed -i "s|libexec|lib|g" doinstall gprbuild.gpr \
    share/gprconfig/compilers.xml \
    share/gprconfig/linker.xml \
    share/gprconfig/gnat.xml
}

build() {
  cd "${srcdir}/gprbuild"

  export GNATMAKEFLAGS="-j$(nproc)"
  export DESTDIR="${srcdir}/bootstrap"
  ./bootstrap.sh \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=/lib \
    --with-xmlada="${srcdir}/xmlada"
}

package() {
  cd "$srcdir/bootstrap"
  cp -a --no-preserve=ownership -- "${srcdir}/bootstrap/${MINGW_PREFIX}" "${pkgdir}"
}
