#!/bin/sh -ue
NAME=busybox
VERSION=1.36.1
RELEASE=1
SOURCE="https://github.com/mirror/busybox/archive/refs/tags/1_36_1.tar.gz"

build() {
	msg "configuring busybox..."
	gmake distclean >/dev/null 2>&1 || :

	_cfg="${DERIVE_BUSYBOX_CONFIG:-$(dirname "$0")/../../../config/busybox.config}"
	if [ -f "$_cfg" ]; then
		msg "using busybox config: $_cfg"
		cp "$_cfg" .config || die "failed to copy busybox config"
	else
		msg "busybox config not found, using defconfig"
		gmake defconfig || die "gmake defconfig failed"
	fi

	# Prefer static busybox for base system.
	if grep -q '^# CONFIG_STATIC is not set' .config 2>/dev/null; then
		sed -i 's/^# CONFIG_STATIC is not set$/CONFIG_STATIC=y/' .config
	fi

	yes "" | gmake oldconfig || die "gmake oldconfig failed"

	msg "building busybox..."
	gmake -j"$NPROC" \
		CROSS_COMPILE="" \
		CC="${CC:-cc}" \
		CFLAGS="${CFLAGS:--O2}" \
		LDFLAGS="${LDFLAGS:--static}" \
		HOSTCC="${HOSTCC:-cc}" \
		HOSTCFLAGS="${HOSTCFLAGS:--O2}" \
		HOSTLDFLAGS="${HOSTLDFLAGS:-}" || die "gmake failed"

	msg "installing busybox..."
	gmake CROSS_COMPILE="" CC="${CC:-cc}" CONFIG_PREFIX="$PKG" install || die "gmake install failed"
}

. ${0%/*}/../../libsh/libdmake.sh
