# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Sheila Aman <sheila@vulpine.house>
# Contributor: Lucas Ramage <ramage.lucas@protonmail.com>
# Maintainer: fossdd <fossdd@pwned.life>
pkgname=bash-completion
pkgver=2.16.0
pkgrel=0
pkgdesc="Command-line tab-completion for bash"
url="https://github.com/scop/bash-completion"
arch="noarch"
license="GPL-2.0-or-later"
depends="bash"
makedepends="autoconf automake bc grep iputils musl-utils procps psmisc sed usbutils"
checkdepends="py3-pexpect py3-pytest"
subpackages="$pkgname-dev $pkgname-doc"
source="https://github.com/scop/bash-completion/releases/download/$pkgver/bash-completion-$pkgver.tar.xz
	add-doas-completion.patch
	"

# Provided by other packages
_conflicting="
	_adb
	cal
	chsh
	dmesg
	eject
	hd
	hexdump
	hwclock
	insmod
	ionice
	look
	makepkg
	ncal
	newgrp
	renice
	rfkill
	rmmod
	rtcwake
	su
	nmcli
	umount
	mount
	"

prepare() {
	default_prepare

	# ifup/down tests are still failing
	rm $builddir/test/t/test_ifdown.py
	rm $builddir/test/t/test_ifup.py
	sed -i '/test_ifdown.py \\/d' $builddir/test/t/Makefile.am
	sed -i '/test_ifup.py \\/d' $builddir/test/t/Makefile.am
	autoreconf -fiv
}

build() {
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--localstatedir=/var
	make
}

check() {
	make check
}

package() {
	make DESTDIR="$pkgdir" install

	# /etc/profile sources profile.d, and this occurs only in login shells
	# it should not then load completions or expect the bashrc to source the profile directory.
	# so, we move the sourcing script to /etc/bash, and include it from the bashrc when present.
	mkdir -p "$pkgdir"/etc/bash
	mv "$pkgdir"/etc/profile.d/bash_completion.sh \
		"$pkgdir"/etc/bash/
	rmdir -p "$pkgdir"/etc/profile.d/ || true

	mkdir -p "$pkgdir"/usr/share/doc/$pkgname/
	install -m644 AUTHORS CHANGELOG.md CONTRIBUTING.md README.md \
		"$pkgdir"/usr/share/doc/$pkgname/

	cd "$pkgdir"/usr/share/bash-completion/completions
	for c in $_conflicting; do
		rm -f $c
	done

	mkdir -p "$pkgdir"/usr/lib
	mv "$pkgdir"/usr/share/pkgconfig "$pkgdir"/usr/lib/pkgconfig
}

sha512sums="
3711d6cb53cedff94efca0325479ac73f40b536fce250dbcc18ef120a8d0d2248f086ca26e245d163e6058d1b1087d57ced041317163d0e25592d6cb7acbc102  bash-completion-2.16.0.tar.xz
39f49a43a660049721a80bccb6a6cf63916e605ab9d33b5bbadf99256650f5ed2e0caa38b22d23b79b9cd3975cb297f7b5d9e12353202eecc01253dd9780002a  add-doas-completion.patch
"
