# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Simon Sobisch <simonsobisch@gnu.org>

_realname=gnucobol
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=3.2
pkgrel=9
epoch=1
pkgdesc="GnuCOBOL, a free and modern COBOL compiler (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://www.gnu.org/software/gnucobol/"
msys2_references=(
  "cpe: cpe:/a:gnucobol_project:gnucobol"
)
license=('spdx:GPL-3.0-or-later AND LGPL-3.0-or-later AND GFDL-1.3-or-later')
# note: the final binaries will call cc, therefore it is a dependency
depends=("${MINGW_PACKAGE_PREFIX}-cc"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-ncurses"
         "${MINGW_PACKAGE_PREFIX}-json-c"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-db")
makedepends=("${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
# Note: "make test" actually needs a version from the build environment (MSYS)
checkdepends=(perl)
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "cobenv.sh"
        "cobenv.cmd"
        "https://web.archive.org/web/20240322143655if_/https://www.itl.nist.gov/div897/ctg/suites/newcob.val.Z"
        0001-gnucobol-extras-include-srcdir.patch
        0002-deprecated-ucrt-functions.patch
        0003-missing-libxml2-header.patch
        )
sha256sums=('3bb48af46ced4779facf41fdc2ee60e4ccb86eaa99d010b36685315df39c2ee2'
            'SKIP'
            '05a859975a39428823933c4612a7d8137c3ab1bf6c5127b1f35ec39533fb7c69'
            '244afbd011b0c0141753c7259173d9f49f161a97c7252b52369e0cec50147308'
            '1e9a92ddbd5d730cbeb764281f7810c22b18e0163985b09675393ab22bbd61f9'
            '4c0a506334d358c6cfc607b8be3aff3fccf19df82232be385c8b298fd7a5cb8b'
            '4eb7b851401200c845cdf900fc8a8ca9fa50afdcfb92cb26febf0b1c85b25dc7'
            '337a5453a19dda5e189a5e17f3941025ea67969ebb884d632eea955c1f097e2b')
validpgpkeys=('B9459D0CA8A740B323235CDF13E96B53C005604E')

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

prepare() {
  cd "${_realname}-${pkgver}"
  cp "${srcdir}"/newcob.val.Z tests/cobol85/

  apply_patch_with_msg \
    0001-gnucobol-extras-include-srcdir.patch \
    0002-deprecated-ucrt-functions.patch \
    0003-missing-libxml2-header.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  export lt_cv_deplibs_check_method='pass_all'   # libncurses with unconforming name
  ../${_realname}-${pkgver}/configure \
    PERL="/usr/bin/perl" \
    CFLAGS="${CFLAGS} -std=gnu17" \
    CURSES_LIBS="$(ncursesw6-config --libs)" \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --infodir=${MINGW_PREFIX}/share/info \
    --mandir=${MINGW_PREFIX}/share/man \
    --with-curses=ncursesw \
    --with-json=json-c \
    --with-db \
    --with-xml2 \
    --without-cisam \
    --without-disam \
    --without-vbisam \
    --disable-rpath \
    --enable-shared \
    --disable-static
    #  --> as using the static libcob would be the builtin default,
    #      all generated COBOL modules would be GPLed (and big)...
    #      ... and only work when all linked together

  make
}

check() {
  cd "${srcdir}"/build-${MSYSTEM}
  # note: strangely multiple tests seem to halt the complete package creation
  #       in any case the parameter should be resolved from the common makevars
  #       of the system (check for --jobs=N / -jN and take N over here)
  #make check TESTSUITEFLAGS="--jobs=$(($(nproc)+1))"
  make check || echo "warning, not all internal tests passed"
  PATH=/usr/bin:$PATH make test  || echo "warning, not all NIST tests passed"
}

package() {
  # install package previously build
  cd "${srcdir}"/build-${MSYSTEM}
  make DESTDIR="${pkgdir}" install

  # copy license files and docs from source to expected places
  cd "${srcdir}"/${_realname}-${pkgver}
  install -d "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -p -m644 COPYING* "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"

  install -d "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  install -p -m644 README NEWS "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  cd doc
  install -p -m644 gnucobol*.pdf "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"

  # copy extra files
  install -p -m777 "${srcdir}"/cobenv.* "${pkgdir}${MINGW_PREFIX}/bin"
}
