# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Jakub Jirutka <jakub@jirutka.cz>
pkgname=yash
pkgver=2.58.1
pkgrel=0
pkgdesc="Yet another shell"
url="https://magicant.github.io/yash/"
arch="all"
license="GPL-2.0-or-later"
# Ensure add-shell is available in post-install on rootfs creation
depends="busybox"
makedepends="ncurses-dev"
checkdepends="ed"
install="
	$pkgname.post-install
	$pkgname.pre-deinstall
	$pkgname.post-upgrade
	"
subpackages="$pkgname-doc $pkgname-binsh::noarch"
source="https://github.com/magicant/yash/releases/download/$pkgver/yash-$pkgver.tar.xz
	default-yashrc.patch
	sample-yashrc.patch
	tests-musl-regex.patch
	tests-busybox-ps.patch
	tests-busybox-argv0.patch
	global-yashrc
	"

_compdir="usr/share/yash/completion"

# Move some bigger completion files to separate subpackages.
_comps="git:git:git[gkx]:git-*
	openssh-client:ssh:ssh-*
	subversion:svn
	valgrind:valgrind
	"
for _i in $_comps; do
	subpackages="$subpackages ${_i%%:*}-yash-completion:_completion:noarch"
done
subpackages="$subpackages $pkgname-completion::noarch"

prepare() {
	# setgid bit is present on the riscv64 builder, causing 58 tests to fail
	find . -type d -exec chmod -c g-s {} \;

	default_prepare

	# Remove completions for programs that are not available on Alpine
	# (just to decrease size of the package).
	cd share/completion
	rm carthage dnf pgawk useradd
}

build() {
	sh ./configure \
		--prefix=/usr \
		--mandir=/usr/share/man \
		--disable-nls
	make
}

check() {
	make tests LANG=en_US.UTF-8 || {
		cat tests/summary.log
		return 1
	}
}

package() {
	make DESTDIR="$pkgdir" install
	rm -Rf "$pkgdir"/usr/share/man/ja

	install -D -m644 "$srcdir"/global-yashrc "$pkgdir"/etc/yashrc
}

_completion() {
	local name="${subpkgname%-yash-completion}"
	pkgdesc="Yash completions for $name"
	depends="$pkgname-completion"
	install_if="$pkgname-completion=$pkgver-r$pkgrel $name"

	local files=$(printf '%s\n' $_comps \
		| sed -En "s|^$name:(.*)|\1|p" \
		| tr : ' ')
	[ "$files" ] || die "$name not found in \$_comps"

	local f; for f in $files; do
		amove usr/share/yash/completion/$f
	done
}

completion() {
	pkgdesc="$pkgdesc (command-line completion)"
	depends=""

	amove usr/share/yash/completion
}

binsh() {
	pkgdesc="yash as /bin/sh"
	provides="/bin/sh"
	provider_priority=50  # lower (other provider is busybox-binsh)
	# Scripts assume busybox tools are available when depending on /bin/sh
	depends="busybox"

	mkdir -p "$subpkgdir"/bin
	ln -s /usr/bin/yash "$subpkgdir"/bin/sh
}

sha512sums="
01e87d579db4ad15ff475cdb7a5360e1b1b6d0e39301e191910943f99adb6686e2462645114c68b59e3de53c5d4486748a9d070a47e3d148e6e3f84b000f1318  yash-2.58.1.tar.xz
8fe512d219204ddbbd59d683732c930970db08a23997c88e91035b60fd5141ea813c4d3ab2d7f19fcf6147fca59b085600a904de0c595dd92f24ab63e19ae91d  default-yashrc.patch
ac24612a82a353d65e2e1a6b99727363be62eb7801eae3109c4661d694d82ddd5c9fc1233443e69d8b26abab725eeaac2c4110e26b1b366983b13cbf7699ebf5  sample-yashrc.patch
441f8eab939c524edf88c164a0d44ea76faa892ba8c67924ca93bb266d0b65885b498b3e8df9ea723ef8263fce9cd9bd0ffb346b8b7b96c84c91f8f141842cf3  tests-musl-regex.patch
beac976835806cce7ec1b89672d297a88ab325b5cbd49033604f620f40025c42f4bcb2c2a8a5930e7df48d5ef709a8a0d79d03eb1836dd1052e9500bd288de74  tests-busybox-ps.patch
3ec0d1ca964fed8a4afcc8c5db0126f8e952e08ea9638f41565478dbc6d74623c6d88e503cd30b27fc7b58ce00fff88f45615d05468e9b9eb0db379695c61cb9  tests-busybox-argv0.patch
778b6a4fa415692293cc9a91074be373572106dc90aeb7490b37643a737f223861f05a7245e4889d8ffea975570f56fd746877726630b9486c481d3159a23e30  global-yashrc
"
