#!/bin/sh

prereqs()
{
	if [ "${BUSYBOX}" = "n" ] || [ -z "${BUSYBOXDIR}" ]; then
		echo ''
	else
		# Ensure busybox installs all its commands before
		# checking what we should provide
		echo zz-busybox
	fi
}

case $1 in
# get pre-requisites
prereqs)
	prereqs
	exit 0
	;;
esac

for src in /usr/lib/klibc/bin/*; do
	command="$(basename "$src")"
	case "$command" in
	gzip | kinit | zcat)
		# Don't install; these will not be needed
		;;
	*)
		# Don't install commands that already exist in /bin
		if ! [ -e "${DESTDIR}/bin/$command" ]; then
			if [ "${verbose?}" = "y" ]; then
				echo "Adding binary-link ${DESTDIR}/bin/$command"
			fi
			cp -pnL "${src}" "${DESTDIR}/bin"
		else
			# Suppress our log output when busybox is enabled and
			# it belongs to busybox
			# We don't want to write a logentry for this when
			# the user configured busybox to take priority
			if [ "${BUSYBOX}" = "n" ] || [ -z "${BUSYBOXDIR}" ] || [ "$(basename $(realpath "${DESTDIR}/bin/$command"))" != "busybox" ]; then
				if [ "${verbose?}" = "y" ]; then
					echo "Skipping existing ${DESTDIR}/bin/$command"
				fi
			fi
		fi
		;;
	esac
done

if [ "${verbose?}" = "y" ]; then
	echo "Adding libraries-link ${DESTDIR}/lib/klibc-*.so"
fi
cp -pL /lib/klibc-*.so "${DESTDIR}/lib"
