# 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.1.2
pkgrel=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=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://www.gnu.org/software/gnucobol/"
options=('strip')
license=('GPL3' 'LGPL3')
# note: the final binaries will call GCC, therefore it is a dependency
depends=("${MINGW_PACKAGE_PREFIX}-gcc"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${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}-pkg-config")
#FIXME deactivated for now as cobc.exe is not found during make test
#checkdepends=("${MINGW_PACKAGE_PREFIX}-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"
#FIXME deactivated for now as cobc.exe is not found during make test
#       "https://www.itl.nist.gov/div897/ctg/suites/newcob.val.Z"
        )
sha256sums=('597005D71FD7D65B90CBE42BBFECD5A9EC0445388639404662E70D53DDF22574'
            SKIP
            'EEDFC170CFD6606527DD701C3CF6BBA2029C33CE694F697F8B7EE527B4ED7F1C'
            '225F7F6E17FC125AF6348028FDB86278C990BEAB07D230A158D9B9373CD58506'
#FIXME deactivated for now as cobc.exe is not found during make test
#           '1E9A92DDBD5D730CBEB764281F7810C22B18E0163985B09675393AB22BBD61F9'
            )
validpgpkeys=('B9459D0CA8A740B323235CDF13E96B53C005604E')

prepare() {
  cd "${_realname}-${pkgver}"
#FIXME deactivated for now as cobc.exe is not found during make test
# cp ${srcdir}/newcob.val.Z tests/cobol85/
}

build() {
  export lt_cv_deplibs_check_method='pass_all'   # libncurses with unconforming name
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  cp -prf ${_realname}-${pkgver} build-${CARCH}
  cd ${srcdir}/build-${CARCH}
  ./configure \
    CPPLAGS="-D__USE_MINGW_ANSI_STDIO=1" \
    --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-${CARCH}
  # 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"
  #FIXME deactivated for now as cobc.exe is not found during make test
  #make test  || echo "warning, not all NIST tests passed"
}

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

  cd ${srcdir}
  install -p -m777 cobenv.* "${pkgdir}${MINGW_PREFIX}/bin"

  cd ${_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}"
}
