# Maintainer: omni <omni+alpine@hack.org>
maintainer="omni <omni+alpine@hack.org>"
pkgname=uutils
pkgver=0.1.0
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-groups-doc:_groups_doc
	$pkgname-coreutils-hostname:_hostname
	$pkgname-coreutils-hostname-doc:_hostname_doc
	$pkgname-coreutils-more:_more
	$pkgname-coreutils-more-doc:_more_doc
	$pkgname-coreutils-stat:_stat
	$pkgname-coreutils-stat-doc:_stat_doc
	$pkgname-coreutils-uptime:_uptime
	$pkgname-coreutils-uptime-doc:_uptime_doc
	"
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

	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
}

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
	mv "$builddir"/man1/groups.1 "$builddir"/
	# TODO: resolve this some other way between this aport, main/coreutils and
	# community/shadow

	# resolve conflict between procps-ng and uutils-coreutils for cmd:uptime
	rm "$builddir"/usr/bin/uptime
	mv "$builddir"/man1/uptime.1 "$builddir"/
	# TODO: resolve this some other way between this aport and main/procps-ng

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

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

	# 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
}

_groups_doc() {
	pkgdesc="$pkgdesc - cmd:groups (documentation)"
	install_if="docs $pkgname-coreutils-groups=$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/groups.1 -t "$subpkgdir"/usr/share/man/man1/
	gzip -n -9 "$subpkgdir"/usr/share/man/man1/groups.1
}

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

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

_hostname_doc() {
	pkgdesc="$pkgdesc - cmd:hostname (documentation)"
	install_if="docs $pkgname-coreutils-hostname=$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/hostname.1 -t "$subpkgdir"/usr/share/man/man1/
	gzip -n -9 "$subpkgdir"/usr/share/man/man1/hostname.1
}

_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)"
	install_if="docs $pkgname-coreutils-more=$pkgver-r$pkgrel"

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

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

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

_stat_doc() {
	pkgdesc="$pkgdesc - cmd:stat (documentation)"
	install_if="docs $pkgname-coreutils-stat=$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/stat.1 -t "$subpkgdir"/usr/share/man/man1/
	gzip -n -9 "$subpkgdir"/usr/share/man/man1/stat.1
}

_uptime() {
	pkgdesc="$pkgdesc - cmd:uptime"
	depends="$pkgname"

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

_uptime_doc() {
	pkgdesc="$pkgdesc - cmd:uptime (documentation)"
	install_if="docs $pkgname-coreutils-uptime=$pkgver-r$pkgrel"

	install -Dm0644 "$builddir"/uptime.1 -t "$subpkgdir"/usr/share/man/man1/
	gzip -n -9 "$subpkgdir"/usr/share/man/man1/uptime.1
}

sha512sums="
e9b3941573b2267e6a4b17c49a64d71234e4cbf067f1180fe14bfe48012c9d400741067f6231855a6132f93138eb4bbcb1e970e98c5f937f8d3a5e5bc019f03c  uutils-coreutils-0.1.0.tar.gz
"
