# Maintainer: Alexey Pavlov <alexpux@gmail.com>

pkgbase=guile
pkgname=("${pkgbase}" "lib${pkgbase}" "lib${pkgbase}-devel")
pkgver=3.0.10
pkgrel=3
pkgdesc="a portable, embeddable Scheme implementation written in C"
url="https://www.gnu.org/software/guile/"
msys2_references=(
  "cpe: cpe:/a:gnu:guile"
)
arch=(i686 x86_64)
license=('GPL')
options=('!libtool')
makedepends=('autotools'
             'gcc'
             'gettext-devel'
             'gmp-devel'
             'libxcrypt-devel'
             'libffi-devel'
             'libgc-devel'
             'libiconv-devel'
             'libintl'
             'libreadline-devel'
             'libunistring-devel'
             'pkgconf'
             'gperf')
source=("https://ftp.gnu.org/pub/gnu/${pkgname}/${pkgbase}-${pkgver}.tar.xz"
        '0001-Fix-dynamic-library-guile-readline-name-on-Cygwin.patch'
        '0002-Fix-warning-if-libguile-is-used-with-compile-option-.patch'
        '0003-Fix-make-custom-port-in-case-encoding-is-f.patch'
        '0004-libguile-scm.h-fix-BUILDING_LIBGUILE-for-Werror-unde.patch'
        '0101-Add-convert-a-standard-shared-library-name-for-MSYS.patch'
        '0201-Activate-test-pthread-create-secondary-for-Cygwin.patch'
        '0202-Include-ITIMER_REAL-test-in-signals.test-for-Cygwin.patch'
        '0301-In-foreign.test-add-linked-dll-for-host-type-msys-explicitly.patch'
        '0302-In-i18n.test-add-unresolved-for-host-type-msys.patch'
        '0303-In-tests-add-dynamic-link-to-msys-2.0-for-host-type-msys.patch'
        '0304-Skip-tests-using-setrlimit-for-MSYS-as-done-for-Cygwin.patch'
        '0305-Remove-failed-test-00-repl-server.test.patch'
        '0401-Narrow-parameter-of-logand-immediate-if-no-bits-used.patch'
        '0402-Fix-fixpoint-needed-bits-computation-in-specialize-n.patch'
        '0403-Fix-boxing-of-non-fixnum-negative-u64-values.patch'
        '0404-Run-sigbits-fixpoint-based-on-use-def-graph-not-cfg.patch')
sha256sums=('bd7168517fd526333446d4f7ab816527925634094fbd37322e17e2b8d8e76388'
            '76482f80bd2b2ade6cdc015230f95a34ef9e13d3798dcd88b881afb63f7bab44'
            '07c0dd3718b8edd8eef545c90b7bf013bdd9418f0eda662c29e31064c4870422'
            '95ad8edc2ac1079e3959ce2a399aedf78b9b9d7d1b31d1aa49e0aba40bbece58'
            '4b9fc785b5072c4bca978eabf4e30fe90bbb79d8ce89539a916af936e28a69a6'
            '7347d4008223245dfeeab04c7490b801d5989c30f263d73415848c6f113518e9'
            '61edb7c2f77aca3bdff316f0f72fe474c5a57b44be00a8fc3cd4209da1d7f33d'
            '60248b9055733309ff21372301e6af513aee7f2400b5681578c0fa509ee8763a'
            'aa1ebdba81f42a80119b1b3b25178d590d9499b25a2ebea0bd8ec060ddf90e43'
            'd75375ab4a78695f2aa04b1499e85dce4f53599271cd88f9e51ae7b27590118a'
            '150201aa84a21eb09bcf46784c92496801f014ee4091d88e6b9e1aedeece6a34'
            'eb683df1331e063fd8d190650a01f30baba06f48b2c6dbce998ac2b3811574c6'
            '1f2b39f919a508dc83363daa80b5ccde14a9d07d70cea2f126b828494af675be'
            'b3012ea06e6f2e4a98ce3d72e9faa84662c3b027bf4023095375005368112ea1'
            '944699d89f0e4f068f4a169e8a4d063e8f5295c1f1c19411d9b1d319b3b8ade1'
            'e3e846eabe2a1e8b3e6136aca219a7b507a737fbbfa6e77e0864931e7f4549ff'
            '80a3e4477a96cf68e29a8e8f688d48b97564c52f8dcd1c4a127749d4e45fd013')

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

  patch -p1 -i ${srcdir}/0001-Fix-dynamic-library-guile-readline-name-on-Cygwin.patch
  patch -p1 -i ${srcdir}/0002-Fix-warning-if-libguile-is-used-with-compile-option-.patch
  patch -p1 -i ${srcdir}/0003-Fix-make-custom-port-in-case-encoding-is-f.patch
  patch -p1 -i ${srcdir}/0004-libguile-scm.h-fix-BUILDING_LIBGUILE-for-Werror-unde.patch

  patch -p1 -i ${srcdir}/0101-Add-convert-a-standard-shared-library-name-for-MSYS.patch

  patch -p1 -i ${srcdir}/0201-Activate-test-pthread-create-secondary-for-Cygwin.patch
  patch -p1 -i ${srcdir}/0202-Include-ITIMER_REAL-test-in-signals.test-for-Cygwin.patch

  patch -p1 -i ${srcdir}/0301-In-foreign.test-add-linked-dll-for-host-type-msys-explicitly.patch
  patch -p1 -i ${srcdir}/0302-In-i18n.test-add-unresolved-for-host-type-msys.patch
  patch -p1 -i ${srcdir}/0303-In-tests-add-dynamic-link-to-msys-2.0-for-host-type-msys.patch
  patch -p1 -i ${srcdir}/0304-Skip-tests-using-setrlimit-for-MSYS-as-done-for-Cygwin.patch
  patch -p1 -i ${srcdir}/0305-Remove-failed-test-00-repl-server.test.patch

  # backports to fix 32-bit
  # https://git.savannah.gnu.org/gitweb/?p=guile.git;a=patch;h=5e6288c9304b60f1875a44808ee3858e3d6efc83
  patch -p1 -i ${srcdir}/0401-Narrow-parameter-of-logand-immediate-if-no-bits-used.patch
  # https://git.savannah.gnu.org/gitweb/?p=guile.git;a=patch;h=0dab58fc2a6ac6a8354439749d598f8c24f57ddd
  patch -p1 -i ${srcdir}/0402-Fix-fixpoint-needed-bits-computation-in-specialize-n.patch
  # https://git.savannah.gnu.org/gitweb/?p=guile.git;a=patch;h=e45b70dcded37eb77e71ec30445b12040b6bb1b7
  patch -p1 -i ${srcdir}/0403-Fix-boxing-of-non-fixnum-negative-u64-values.patch
  # https://git.savannah.gnu.org/gitweb/?p=guile.git;a=patch;h=aff9ac968840e9c86719fb613bd2ed3c39b9905c
  patch -p1 -i ${srcdir}/0404-Run-sigbits-fixpoint-based-on-use-def-graph-not-cfg.patch

  autoreconf -fi
}

build() {
  cd "${srcdir}/${pkgbase}-${pkgver}"
  ./configure \
    --build=${CHOST} \
    --prefix=/usr \
    --disable-rpath

# workaround since both --enable-lto and --disable-lto fail
  find . -name Makefile -exec sed -i -e "s/-flto//" {} \;

  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd "${srcdir}/${pkgbase}-${pkgver}"

  make -i check

  echo " ****  Running last tests in a complete way    ****"
  for i in  ./test-suite/tests/*.test
    do ./check-guile $(basename $i) || true
    done
}

package_guile() {
  depends=("libguile=${pkgver}")

  mkdir -p ${pkgdir}/usr/bin
  cp -rf ${srcdir}/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*.dll
  rm -f ${pkgdir}/usr/bin/*-config*

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/info ${pkgdir}/usr/share/
  cp -rf ${srcdir}/dest/usr/share/man ${pkgdir}/usr/share/
}

package_libguile() {
  depends=('gmp'
           'libxcrypt'
           'libffi'
           'libgc'
           'libreadline'
           'libunistring')
  groups=('libraries')

  mkdir -p ${pkgdir}/usr/bin
  install -Dm755 ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/

  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/guile ${pkgdir}/usr/share/

  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/lib/guile ${pkgdir}/usr/lib/

  mkdir -p ${pkgdir}/usr/share/gdb/auto-load/usr/bin
  install -Dm644 ${srcdir}/dest/usr/lib/*.scm \
                 ${pkgdir}/usr/share/gdb/auto-load/usr/bin/$(dlltool \
                   -I ${srcdir}/dest/usr/lib/libguile-*.dll.a)-gdb.scm
}

package_libguile-devel() {
  depends=("libguile=${pkgver}"
           'gmp-devel'
           'libgc-devel'
           'pkgconf')
  groups=('development')
  options=('staticlibs')

  mkdir -p ${pkgdir}/usr/bin
  install -Dm755 ${srcdir}/dest/usr/bin/*-config* ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  mkdir -p ${pkgdir}/usr/lib
  cp -rf ${srcdir}/dest/usr/lib/*.a ${pkgdir}/usr/lib/
  cp -rf ${srcdir}/dest/usr/lib/pkgconfig ${pkgdir}/usr/lib/
  mkdir -p ${pkgdir}/usr/share
  cp -rf ${srcdir}/dest/usr/share/aclocal ${pkgdir}/usr/share/
}
