# Contributor: Olliver Schinagl <oliver@schinagl.nl>
# Maintainer: Olliver Schinagl <oliver@schinagl.nl>
pkgname="klibc"
pkgver="2.0.13"
pkgrel=0
pkgdesc="A minimalistic libc subset for use with initramfs"
url="https://git.kernel.org/pub/scm/libs/klibc/klibc.git/"
arch="all"
license="GPL-2.0-or-later"
makedepends="linux-headers perl"
subpackages="
	${pkgname}-dash
	${pkgname}-dev
	${pkgname}-doc
	${pkgname}-static:static
	${pkgname}-fstype:bins
	${pkgname}-ipconfig:bins
	${pkgname}-kinit::noarch
	${pkgname}-klcc::noarch
	${pkgname}-nfsmount:bins
	${pkgname}-resume:bins
	${pkgname}-run-init:bins
	${pkgname}-utils
	lib${pkgname}:libs
"
source="
	https://git.kernel.org/pub/scm/libs/klibc/klibc.git/snapshot/${pkgname}-${pkgver}.tar.gz

	lib${pkgname}.post-deinstall
	lib${pkgname}.post-install
"
_installdir="/usr/lib/${pkgname}-${CARCH}/"

# secfixes:
#   2.0.9-r0:
#     - CVE-2021-31870
#     - CVE-2021-31871
#     - CVE-2021-31872
#     - CVE-2021-31873

build() {
	# klibc's header misses the struct declaration, Linux's misses
	# the function declaration. Combine them here.
	if [ ! -e "${builddir}/usr/include/sys/sysinfo.h.orig" ]; then
		mv "${builddir}/usr/include/sys/sysinfo.h" \
		   "${builddir}/usr/include/sys/sysinfo.h.orig"
	fi
	cat \
	    "/usr/include/linux/sysinfo.h" \
	    "${builddir}/usr/include/sys/sysinfo.h.orig" > \
	    "${builddir}/usr/include/sys/sysinfo.h"

	make \
	     KBUILD_REPRODUCIBLE=1 \
	     KLIBCKERNELSRC="/usr/"
}

check() {
	make \
	     KBUILD_REPRODUCIBLE=1 \
	     KLIBCKERNELSRC="/usr/" \
	     test
}

package() {
	depends="
		lib${pkgname}
		${pkgname}-dash
		${pkgname}-kinit
		${pkgname}-utils
	"

case "${CARCH}" in
	aarch64)
		_klibcarch="arm64"
		;;
	armhf)
		_klibcarch="arm"
		CPU_ARCH="armv6zk"
		CPU_TUNE="arm1176jzf-s"
		;;
	armv7)
		_klibcarch="arm"
		CPU_ARCH="armv7"
		CPU_TUNE="generic-armv7-a"
		;;
	ppc)
		_klibcarch="ppc"
		;;
	ppc64|ppc64le)
		_klibcarch="ppc64"
		;;
	riscv64)
		_klibcarch="riscv64"
		;;
	s390x)
		_klibcarch="s390"
		;;
	x86)
		_klibcarch="i386"
		;;
	x86_64)
		_klibcarch="x86_64"
		;;
	*)
		echo 'Unsupported architecture'
		;;
	esac
	export CPU_ARCH
	export CPU_TUNE
	make \
	     INSTALLDIR="${_installdir}" \
	     INSTALLROOT="${pkgdir}" \
	     KBUILD_REPRODUCIBLE=1 \
	     KLIBCKERNELSRC="/usr/" \
	     mandir="/usr/share/man" \
	     install

	# Duplicated in INSTALLDIR/lib and replaced with post.install
	rm -r "${pkgdir:?}/lib"

	for _arch in "${pkgdir}/${_installdir}/include/arch/"*; do
		if [ "${_arch##*/}" != "${_klibcarch}" ]; then
			rm -r "${_arch:?}"
		fi
	done

	# Replace hardlinks with symlinks
	unlink "${pkgdir}/${_installdir}/bin/gunzip"
	unlink "${pkgdir}/${_installdir}/bin/poweroff"
	unlink "${pkgdir}/${_installdir}/bin/reboot"
	unlink "${pkgdir}/${_installdir}/bin/zcat"

	ln -s "gzip" "${pkgdir}/${_installdir}/bin/gunzip"
	ln -s "gzip" "${pkgdir}/${_installdir}/bin/zcat"

	ln -s "halt" "${pkgdir}/${_installdir}/bin/poweroff"
	ln -s "halt" "${pkgdir}/${_installdir}/bin/reboot"
}

klcc() {
	depends="perl"

	mkdir -p "${subpkgdir}/usr/bin"
	mv "${pkgdir}/usr/bin/klcc" "${subpkgdir}/usr/bin/"
}

libs() {
	install="lib${pkgname}.post-deinstall lib${pkgname}.post-install"

	mkdir -p "${subpkgdir}/${_installdir}"
	mv "${pkgdir}/${_installdir}/lib" "${subpkgdir}/${_installdir}/"
}

bins() {
	depends="lib${pkgname}"

	mkdir -p "${subpkgdir}/${_installdir}/bin"
	mv "${pkgdir}/${_installdir}/bin/${subpkgname#${pkgname}-}" "${subpkgdir}/${_installdir}/bin/"
}

dash() {
	depends="lib${pkgname}"

	mkdir -p "${subpkgdir}/${_installdir}/bin"
	mv "${pkgdir}/${_installdir}/bin/sh" "${subpkgdir}/${_installdir}/bin/"
}

kinit() {
	depends="
		${pkgname}-fstype
		${pkgname}-ipconfig
		${pkgname}-nfsmount
		${pkgname}-resume
		${pkgname}-run-init
	"

	mkdir -p "${subpkgdir}"
}

utils() {
	depends="lib${pkgname}"

	mkdir -p "${subpkgdir}/${_installdir}/"
	mv "${pkgdir}/${_installdir}/bin" "${subpkgdir}/${_installdir}/"
}

static() {
	mkdir -p "${subpkgdir}/${_installdir}/bin"

	find "${builddir}" -type d -name 'static' | while read -r _staticbins; do
		for _bin in "${_staticbins}/"*; do
			if [ -z "${_bin%%*.g}" ]; then
				continue
			fi

			install -D -m 755 "${_bin}" \
			        "${subpkgdir}/${_installdir}/bin/${_bin##*/}.static"
		done
	done

	mv "${pkgdir}/${_installdir}/bin/gunzip" \
	   "${subpkgdir}/${_installdir}/bin/gunzip.static"

	mv "${pkgdir}/${_installdir}/bin/gzip" \
	   "${subpkgdir}/${_installdir}/bin/gzip.static"

	mv "${pkgdir}/${_installdir}/bin/zcat" \
	   "${subpkgdir}/${_installdir}/bin/zcat.static"
}

sha512sums="
5bd7d109d73af8a04d90e81de903517dce38f5773eaa0e308f6cb51b503b06f4cadf5778fb920cc04c03e8ec4b27ee1172408ee68982120b6bb96f43339e0d7b  klibc-2.0.13.tar.gz
672f87b240b3349b2ffee2768ef6b1d37f6395d8f00bdf33375c03f967041de14b6365bcecff2d3918130b16c250ec5e3d50f1ac04c9530f0b169bc01e7d39cb  libklibc.post-deinstall
7340bfd2c048b8b7a6edd752d552e1007c202a5b44bf9d0ae741a80dcea1cbfdde38ade50f7d008c723a2b99570156877476273073039da286b604693323e484  libklibc.post-install
"