# Maintainer: omni <omni+alpine@hack.org>
maintainer="omni <omni+alpine@hack.org>"
pkgname=uutils
pkgver=0.0.30
pkgrel=0
pkgdesc="Rust rewrite of the GNU coreutils"
arch="all"
url="https://uutils.github.io/coreutils/"
license="MIT"
makedepends="cargo help2man oniguruma-dev cargo-auditable"
checkdepends="coreutils" # test_ls.rs incompatible with busybox truncate
subpackages="$pkgname-doc
	$pkgname-coreutils:_coreutils
	$pkgname-coreutils-doc:_coreutils_doc
	$pkgname-coreutils-groups:_groups
	$pkgname-coreutils-hostname:_hostname
	$pkgname-coreutils-more:_more
	$pkgname-coreutils-more-doc:_more_doc
	$pkgname-coreutils-stat:_stat
	"
source="$pkgname-coreutils-$pkgver.tar.gz::https://github.com/uutils/coreutils/archive/$pkgver.tar.gz"
builddir="$srcdir/coreutils-$pkgver"
options="!check" # package builder file descriptor issues

export RUSTONIG_DYNAMIC_LIBONIG=1

prepare() {
	default_prepare

	cargo fetch --target="$CTARGET" --locked
}

build() {
	cargo auditable build --frozen \
		--profile=release-small --features=feat_os_unix_musl

	install -Dm0755 target/release-small/"coreutils" usr/bin/"$pkgname"

	help2man --no-info --name=uutils --manual="$pkgname" \
		--version-string="$pkgver" usr/bin/"$pkgname" > "$pkgname".1

	install -dm0755 man1/
	# create symlinks and individual man pages
	for uutil in $(./usr/bin/uutils | tail -n +11 | tr -d '\n' | sed -e 's/,//g' -e 's/sha3-\s*512sum/sha3-512sum/'); do
		ln -s uutils usr/bin/$uutil
		case "$uutil" in
			test) ;; # helpless
			\[) help2man --no-info --name=test --manual="$pkgname coreutils" \
				usr/bin/"$uutil" > man1/test.1 ;;
			uniq) help2man --no-info --name="$uutil" --manual="$pkgname coreutils" \
				--no-discard-stderr \
				usr/bin/"$uutil" > man1/"$uutil".1 ;;
			*) help2man --no-info --name="$uutil" --manual="$pkgname coreutils" \
				usr/bin/"$uutil" > man1/"$uutil".1 ;;
		esac
	done
}

check() {
	ulimit -n 4096 # running out of file descriptors

	cargo test --frozen \
		--profile=release-small --features=feat_os_unix_musl
}

package() {
	pkgdesc="$pkgdesc - main binary"
	replaces="uutils-coreutils<0.0.28-r0"

	install -Dm0755 usr/bin/"$pkgname" -t "$pkgdir"/usr/bin/
	# remove the binary and keep the symlinks
	rm usr/bin/"$pkgname"

	install -Dm0644 "$pkgname".1 -t "$pkgdir"/usr/share/man/man1/
	install -Dm0644 LICENSE -t "$pkgdir"/usr/share/licenses/"$pkgname"/
}

_coreutils() {
	# prefix with 0. to ensure it never gets above GNU coreutils' version
	provides="coreutils=0.$pkgver-r$pkgrel"
	depends="$pkgname"

	# binaries that busybox puts in /bin (mimic main/coreutils)
	local busybox_bin="arch base64 cat chgrp chmod chown cp date dd df echo
		false kill link ln ls mkdir mknod mktemp mv nice printenv pwd rm rmdir
		sleep stty sync touch true uname"

	# as these binaries live in /bin on busybox, we want to put them in /bin with uutils
	install -dm0755 "$subpkgdir"/bin/
	for i in $busybox_bin; do
		rm "$builddir"/usr/bin/$i
		ln -s ../usr/bin/"$pkgname" "$subpkgdir"/bin/$i
	done

	# chroot lives in /usr/sbin with busybox
	rm "$builddir"/usr/bin/chroot
	install -dm0755 "$subpkgdir"/usr/sbin/
	ln -s ../bin/"$pkgname" "$subpkgdir"/usr/sbin/chroot

	# resolve conflict between shadow and uutils-coreutils for cmd:groups
	rm "$builddir"/usr/bin/groups
	# TODO: resolve this some other way between this aport, main/coreutils and
	# community/shadow

	# uutils hostname does not take -F FILE (keep busybox for now)
	rm "$builddir"/usr/bin/hostname

	# uutils stat does not work with switch_root or init? (keep busybox for now)
	rm "$builddir"/usr/bin/stat

	# resolve conflict between utils-linux-misc and uutils-coreutils for cmd:more
	rm "$builddir"/usr/bin/more
	mv "$builddir"/man1/more.1 "$builddir"/

	# install what's left
	install -dm0755 "$subpkgdir"/usr/bin/
	mv "$builddir"/usr/bin/* "$subpkgdir"/usr/bin/
}

_coreutils_doc() {
	# prefix with 0. to ensure it never gets above GNU coreutils' version
	provides="coreutils-doc=0.$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/man1/* -t "$subpkgdir"/usr/share/man/man1/

	default_doc
}

_groups() {
	pkgdesc="$pkgdesc - cmd:groups"
	depends="$pkgname"

	install -dm0755 "$subpkgdir"/usr/bin/
	ln -s "$pkgname" "$subpkgdir"/usr/bin/groups
}

_hostname() {
	pkgdesc="$pkgdesc - cmd:hostname"
	depends="$pkgname"

	# busybox path
	install -dm0755 "$subpkgdir"/bin/
	ln -s ../usr/bin/"$pkgname" "$subpkgdir"/bin/hostname
}

_more() {
	pkgdesc="$pkgdesc - cmd:more"
	depends="$pkgname"

	# busybox/util-linux-misc path
	install -dm0755 "$subpkgdir"/bin/
	ln -s ../usr/bin/"$pkgname" "$subpkgdir"/bin/more
}

_more_doc() {
	pkgdesc="$pkgdesc - cmd:more (documentation)"
	# prefix with 0. to ensure it never gets above the util-linux version
	provides="util-linux-doc=0.$pkgver-r$pkgrel"
	install_if="docs $pkgname-coreutils-more=$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/more.1 -t "$subpkgdir"/usr/share/man/man1/

	default_doc
}

_stat() {
	pkgdesc="$pkgdesc - cmd:stat"
	depends="$pkgname"

	# busybox path
	install -dm0755 "$subpkgdir"/bin/
	ln -s ../usr/bin/"$pkgname" "$subpkgdir"/bin/stat
}

sha512sums="
4d9f002329513e4cbf8787d050d852a209a02e9fbea84a63e442ace4fec38ce362dbac5ccee341834ade5f96586be393d6fc64e8421285598dd64804b660e633  uutils-coreutils-0.0.30.tar.gz
"
