# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Stéphane Gaudreault <stephane@archlinux.org>
# Contributor: Tom Newsom <Jeepster@gmx.co.uk>

# Note:
# When bumping major or minor versions, some downstream packages that use somewhat
# unorthodox runtime arrangements need rebuilding to match. After SWIG version
# bumps please rebuild first python-pivy then freecad in addition to usual suspects.
# c.f. https://github.com/FreeCAD/FreeCAD/issues/25048#issuecomment-3627741495
# c.f. https://gitlab.archlinux.org/archlinux/packaging/packages/python-pivy/-/issues/1
# c.f. https://gitlab.archlinux.org/archlinux/packaging/packages/swig/-/issues/2

pkgname=swig
pkgver=4.4.1
pkgrel=1
pkgdesc="Generate scripting interfaces to C/C++ code"
arch=('x86_64' 'aarch64')
url="https://www.swig.org/"
license=(
  GPL-3.0-or-later
  LicenseRef-BSD-Arizona
  LicenseRef-BSD-Chicago
  LicenseRef-BSD-Utah-California
)
depends=('gcc-libs' 'glibc' 'pcre2' 'zlib')
checkdepends=('ruby' 'python' 'java-environment' 'tcl' 'php' 'lua' 'r' 'go' 'boost')
makedepends=('git')
source=(git+https://github.com/swig/swig#tag=v$pkgver)
sha512sums=('faa8e527e384a320b6151cca8fcab962e1c9c7a4be41dc4075ac7e385c841247521c37c2d8d485725bb036c0c9e82002d01c91f6025f6abf1de23a178eac7992')

prepare() {
  sed -n '5,32p' $pkgname/LICENSE-UNIVERSITIES > LicenseRef-BSD-Utah-California.txt
  sed -n '37,64p' $pkgname/LICENSE-UNIVERSITIES > LicenseRef-BSD-Chicago.txt
  sed -n '69,94p' $pkgname/LICENSE-UNIVERSITIES > LicenseRef-BSD-Arizona.txt

  # https://github.com/swig/swig/issues/2858
  sed '/stl_no_default_constructor/d' -i $pkgname/Examples/test-suite/common.mk

  # https://github.com/swig/swig/issues/2859
  sed '/li_std_list/d' -i $pkgname/Examples/test-suite/java/Makefile.in

  cd $pkgname
  ./autogen.sh
}

build() {
  cd ${pkgname}
  ./configure --prefix=/usr
  make
}

check() {
  cd ${pkgname}
  make check PY3=y
}

package() {
  cd ${pkgname}
  make DESTDIR="${pkgdir}" install
  install -D -m644 LICENSE "${pkgdir}"/usr/share/licenses/${pkgname}/LICENSE
  install -D -m644 ../*.txt -t "${pkgdir}"/usr/share/licenses/${pkgname}/
}
