# Maintainer (Arch:extra/uwsgi): Sven-Hendrik Haase <svenstaro@gmail.com>
# Maintainer (Arch:extra/uwsgi): David Runge <dvzrv@archlinux.org>
# Contributor (Arch:extra/uwsgi): Daniel Wallace <danielwallace at gtmanfred dot com>
# Contributor (Arch:extra/uwsgi): Valentin Hăloiu <vially.ichb+aur@gmail.com>
# Contributor (Arch:extra/uwsgi): Angel Velasquez <angvp@archlinux.org>
# Contributor (Arch:extra/uwsgi): Kevin Zuber <uKev@knet.eu>
# Contributor (Arch:extra/uwsgi): Vsevolod Balashov <vsevolod@balashov.name>
# Contributor (Arch:extra/uwsgi): David Runge <dave@sleepmap.de>

# Maintainer: Luke T. Shumaker <lukeshu@parabola.nu>

_pkgbase=uwsgi
pkgname=uwsgi-plugin-rack2.7
pkgver=2.0.31
pkgrel=3
pkgdesc="A full stack for building hosting services"
arch=(x86_64)
arch+=(i686 armv7h)
url="https://uwsgi-docs.readthedocs.io/en/latest/"
license=(LicenseRef-GPL-2.0-or-later-with-linking-exception)
makedepends=(
  jansson
  libcap
  libxcrypt
  libxml2
  openssl
  pam
  pcre2
  python
  ruby2.7
  systemd
  util-linux
  zlib
)
source=(
  $_pkgbase-$pkgver.tar.gz::https://github.com/unbit/$_pkgbase/archive/refs/tags/$pkgver.tar.gz
  archlinux.ini
)
sha512sums=('d4d327f36455438e165298cb5a7d483c951f4acd3d834b46ceeda105bd3664792ead226b02e06db62e92c210544778119bf75a55d9d4f30f8fba896c7de51e6d'
            '88ea486a406f206eeee665b9178d64c6849359a0d1028628900c26ea02ebc5cdc3ce2c937b083e3be06643c5e10e5af8e28b3c2f62834112bb683dd0f3906c83')
b2sums=('dd2d8b7d3b2293fb2f4ae587862b107238e5b8b4fbd9953a5de52b41329f3895609fe98f8476603d1f5e3f209d490cc5fa46a43cfb7d2b046c22bbddf55236b2'
        '2b82ec68b2936675470c102f6be15ed137bd7f350c867d681f2204bea562d63d8604ee965594df38d911176abbf56184a1f081c4bba9eb5f4df0ea24ca85864f')

prepare() {
  cd $_pkgbase-$pkgver

  # move ruby plugin to new location and fix its name
  mv plugins/ruby{19,}
  sed -e 's/ruby19/ruby/' -i plugins/ruby/uwsgiplugin.py

  # create "27" variants of each of the Ruby plugins
  cp -av plugins/fiber{,27}
  cp -av plugins/rack{,27}
  cp -av plugins/rbthreads{,27}
  cp -av plugins/ruby{,27}
  sed -e "/^NAME=/s/'\$/27'/" \
      -e 's/UWSGICONFIG_RUBYPATH/UWSGICONFIG_RUBY27PATH/g' \
      -e 's/uwsgi_plugin_get("rack")/uwsgi_plugin_get("rack27")/g' \
      -e 's,\.\./rack,../rack27,g' \
      -i -- plugins/{fiber,rack,rbthreads,ruby}27/*
  sed -e 's/fiber_plugin/fiber27_plugin/g' -i -- plugins/fiber27/fiber.c
  sed -e 's/rack_plugin/rack27_plugin/g' -i -- plugins/rack27/*.c
  sed -e 's/rbthreads_plugin/rbthreads27_plugin/g' -i -- plugins/rbthreads27/rbthreads.c
  sed -e 's/ruby_plugin/ruby27_plugin/g' \
      -e '/CFLAGS/s/rack_plugin/rack27_plugin/g' \
      -i -- plugins/ruby27/uwsgiplugin.py

  # copy our custom build profile into place
  cp -v ../archlinux.ini buildconf/
}

build() {
  cd $_pkgbase-$pkgver
  export UWSGICONFIG_RUBY27PATH='ruby-2.7'
  python uwsgiconfig.py --verbose --build archlinux
}

package() {
  depends=(
    glibc
    ruby2.7
    uwsgi="$pkgver"
  )
  pkgdesc="Ruby 2.7 rack plugin"

  cd $_pkgbase-$pkgver
  install -vDm 755 {rack,fiber,rbthreads}27_plugin.so -t "$pkgdir"/usr/lib/uwsgi/
  install -vDm 644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
}
