# SPDX-License-Identifier: CC0-1.0
# Maintainer: Luke Shumaker <lukeshu@sbcglobal.net>
# Contributor: bill-auger <bill-auger@programmer.net>


pkgbase=parabola-hackers
pkgname=(parabola-hackers parabola-hackers-nshd)
pkgver=20220802
pkgrel=1
pkgdesc='Programs for doing magic with hackers.git'
url='https://git.parabola.nu/packages/parabola-hackers.git/'
license=('GPL')
arch=('armv7h' 'i686' 'x86_64')

makedepends=('git' 'go')
source=("parabola-hackers::git://git.parabola.nu/packages/parabola-hackers.git#tag=v${pkgver}")
_go_sources=("git.lukeshu.com/go/libgnulinux#branch=master"
             "git.lukeshu.com/go/libnslcd#branch=master"
             "git.lukeshu.com/go/libsystemd#commit=0a43955"
             "github.com/pkg/errors#tag=v0.8.0"
             "go.googlesource.com/sys#commit=bb9c189"
             "gopkg.in/check.v1#commit=20d25e2"
             "gopkg.in/yaml.v2#commit=5420a8b")
for _src in ${_go_sources[*]}; do
  source+=("gopkg-$(sed -e 's/#.*//' -e 's/\//_/g' <<<"$_src")::git+https://$_src")
done
sha256sums=(SKIP)
sha256sums+=( $(echo ${_go_sources[*]/*/SKIP}) )


prepare() {
  # verify versioning
  local go_pkg_ver=$(grep VERSION "$srcdir"/$pkgbase/config.mk | awk '{print $3}')
  local err_msg="VERSION in config.mk does not match the git tag (aka: pkgver)"
  [[ "$go_pkg_ver" == "$pkgver" ]] || ! echo "$err_msg" || return 1

  # install golang deps
  mv -Tv gopkg-go.googlesource.com_sys "$srcdir/$pkgbase"/go/src/golang.org/x/sys
  for gopkg in gopkg-*; do
    mv -Tv "$gopkg" "$srcdir/$pkgbase"/go/src/"$(sed 's/_/\//g' <<<"${gopkg#gopkg-}")"
  done

  # load git submodules
  cd "$srcdir/$pkgbase"
  git submodule update --init
}

build() {
  cd "$srcdir/$pkgbase"
  make
}

check() {
  # FIXME:
  # On armv7h the tests fail with:
  #   FAIL gopkg.in/yaml.v2 [build failed]
  # On x86_64 the tests fail with:
  #   FAIL: TestSCMCredentials (0.00s)
  #     fatal error: checkptr: pointer arithmetic result points to invalid allocation
  #   FAIL golang.org/x/sys/unix 0.288s
  cd "$srcdir/$pkgbase"
  # make check
  make check || :
}

package_parabola-hackers() {
  backup=(etc/$pkgbase.yml)
  depends=('ruby' 'colordiff')

  cd "$srcdir/$pkgbase"
  make DESTDIR="${pkgdir}" install

  # cleanup 'parabola-hackers-nshd' files
  find "$pkgdir" -type f \( -name 'nshd*' -o -name shadow \) -delete
  find "$pkgdir" -type d -empty -exec rmdir -p --ignore-fail-on-non-empty -- {} +
}

package_parabola-hackers-nshd() {
  pkgdesc='NSS and PAM integration for parabola-hackers'
  license=(AGPL3 Apache custom:BSD2 custom:BSD3 GPL LGPL2.1 WTFPL2)
  depends=("parabola-hackers=$pkgver" 'nss-pam-ldapd')
  backup=(etc/nshd/shadow)
  install=parabola-hackers-nshd.install
  replaces=(nshd)
  conflicts=(nshd)

  cd "$srcdir/$pkgbase"
  make DESTDIR="${pkgdir}" install

  # cleanup 'parabola-hackers' files
  find "$pkgdir" -type f -not \( -name 'nshd*' -o -name shadow \) -delete
  find "$pkgdir" -type d -empty -exec rmdir -p --ignore-fail-on-non-empty -- {} +

  # install licenses
  install -d "${pkgdir}/usr/share/licenses/${pkgname}"
  cp LICENSES.txt LICENSE.bsd* "${pkgdir}/usr/share/licenses/${pkgname}"
}
