# 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=2
epoch=1
pkgdesc="GnuCOBOL, a free and modern COBOL compiler (mingw-w64)"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-svn" "${MINGW_PACKAGE_PREFIX}-gnu-cobol-svn")
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64')
url="https://www.gnu.org/software/gnucobol/"
license=('spdx:GPL-3.0-or-later AND spdx:LGPL-3.0-only')
# 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}-cjson"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-db")

# the optional depencies cannot be used for creating the binary packages, moved to depends
#optdepends=("${MINGW_PACKAGE_PREFIX}-gettext: support for internationalization"
#            "${MINGW_PACKAGE_PREFIX}-ncurses: support for extended screenio"
#            "${MINGW_PACKAGE_PREFIX}-pdcurses: support for screenio"
#            "${MINGW_PACKAGE_PREFIX}-cjson: support for JSON GENERATE"
#            "${MINGW_PACKAGE_PREFIX}-json-c: support for JSON GENERATE"
#            "${MINGW_PACKAGE_PREFIX}-libxml2: support for XML GENERATE")
makedepends=("${MINGW_PACKAGE_PREFIX}-autotools" "${MINGW_PACKAGE_PREFIX}-gettext-tools")
# Note: it actually needs the one from the build environment (MSYS)
checkdepends=(perl)

# local definitions
#source=("https://alpha.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
source=("https://ftp.gnu.org/gnu/${_realname}/${_realname}-${pkgver}.tar.xz"{,.sig}
        "cobenv.sh"
        "cobenv.cmd"
        "https://www.itl.nist.gov/div897/ctg/suites/newcob.val.Z"
        0001-gnucobol-extras-include-srcdir.patch
        )
sha256sums=('3bb48af46ced4779facf41fdc2ee60e4ccb86eaa99d010b36685315df39c2ee2'
            'SKIP'
            '05a859975a39428823933c4612a7d8137c3ab1bf6c5127b1f35ec39533fb7c69'
            '244afbd011b0c0141753c7259173d9f49f161a97c7252b52369e0cec50147308'
            '1e9a92ddbd5d730cbeb764281f7810c22b18e0163985b09675393ab22bbd61f9'
            'd8ee50c9f22849081783cc65c62536aa1f4e017fa683c4041400e5dde8b30159')
validpgpkeys=('B9459D0CA8A740B323235CDF13E96B53C005604E')

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

  patch -p1 -i "${srcdir}/0001-gnucobol-extras-include-srcdir.patch"
  autoreconf -fiv
}

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

  export lt_cv_deplibs_check_method='pass_all'   # libncurses with unconforming name
  CFLAGS+=" -Wno-implicit-function-declaration" \
  ../${_realname}-${pkgver}/configure \
    CPPLAGS="-D__USE_MINGW_ANSI_STDIO=1" \
    PERL="/usr/bin/perl" \
    --with-db --without-vbisam --without-disam --without-cisam \
    --with-xml2 --with-json=cjson --with-curses=ncursesw \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --infodir=${MINGW_PREFIX}/share/info \
    --mandir=${MINGW_PREFIX}/share/man \
    --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"
}
