# POWER Maintainer: Alexander Baldeck <alex.bldck@gmail.com>
# Maintainer: Morten Linderud <foxboron@archlinux.org>
# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Bartłomiej Piotrowski <bpiotrowski@archlinux.org>

_name=plugins
pkgname=cni-plugins
pkgver=1.5.1
pkgrel=1
pkgdesc='Some standard networking plugins, maintained by the CNI team'
arch=(x86_64 powerpc64le riscv64)
url="https://github.com/containernetworking/plugins"
license=(Apache-2.0)
depends=(glibc)
makedepends=(go)
provides=(container-network-stack=1)
options=(
  !lto  # unable to build with LTO: https://github.com/containernetworking/plugins/issues/706
  emptydirs  # NOTE: we need /etc/cni/net.d/ for configs
)
source=($url/archive/v$pkgver/$_name-v$pkgver.tar.gz)
sha512sums=('d2e01958dd8328407164cb6be9d962321742dae7011ce7cd7b2342f5e4b4bbcd992d8249c53d3d81250a60c27f049969bbf329a75440524f52c1f1466b6e7132')
b2sums=('481e6c3e929d9169c643596ba48e008151aef10ff670cac9fa00294a3fe98bc5d7c9ee9f83394becb4c127290abb3b082c4124d332b32c8d97f44fcb01b7e3cf')

prepare() {
  mkdir -vp $_name-$pkgver/bin
}

build() {
  cd $_name-$pkgver

  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export CGO_LDFLAGS="${LDFLAGS}"
  export GOPATH="${srcdir}"
  export GOFLAGS="-buildmode=pie -mod=readonly -modcacherw"

  # custom go build calls, since build_linux.sh is not flexible enough
  for plugin in plugins/meta/* plugins/main/* plugins/ipam/*; do
    if [[ -d "$plugin"  && "$plugin" != *windows ]]; then
      printf "Building plugin: %s\n" "$(basename $plugin)"
      go build -o bin/ -ldflags "-compressdwarf=false -linkmode external" "./$plugin"
    fi
  done
}

# tests are broken
# check() {
#   cd $_name-$pkgver
#   go test ./...
# }

package() {
  cd $_name-$pkgver
  install -vDm755 bin/* -t "$pkgdir/usr/lib/cni/"
  # strip files manually because makepkg doesn't do it: https://gitlab.archlinux.org/pacman/pacman/-/merge_requests/38
  strip "$pkgdir/usr/lib/cni/"*
  # some applications would build their own versions of CNI plugins in
  # /opt/cni/bin if they are not found (as non-symlink files), so we have to
  # install them twice... ;_;
  install -vDm755 bin/* -t "$pkgdir/opt/cni/bin/"
  strip "$pkgdir/opt/cni/bin/"*
  install -vdm755 "$pkgdir/etc/cni/net.d/"
  install -vDm644 LICENSE -t "$pkgdir/usr/share/licenses/$pkgname/"
  install -vDm644 plugins/ipam/dhcp/systemd/* -t "$pkgdir/usr/lib/systemd/system/"
}
