# Template file for 'busybox'
pkgname=busybox
version=1.34.1
revision=6
hostmakedepends="perl"
checkdepends="tar which zip"
short_desc="Swiss Army Knife of Embedded Linux"
maintainer="Cameron Nemo <cam@nohom.org>"
license="GPL-2.0-only"
homepage="https://www.busybox.net"
distfiles="${homepage}/downloads/busybox-${version}.tar.bz2"
checksum=415fbd89e5344c96acf449d94a6f956dbed62e18e835fc83e064db33a34bd549

if [ "$XBPS_TARGET_LIBC" = "glibc" ]; then
	makedepends+=" libxcrypt-devel"
fi

_alternatives_core="
 ash:ash:/usr/bin/busybox
 awk:awk:/usr/bin/busybox
 hostname:hostname:/usr/bin/busybox
 sh:sh:/usr/bin/busybox
 vi:vi:/usr/bin/busybox
 logger:logger:/usr/bin/busybox"

_alternatives="
 ${_alternatives_core}
 ntpd:ntpd:/usr/bin/busybox
 ntpd:ntpd:/etc/sv/busybox-ntpd"

alternatives="${_alternatives}"
register_shell="/usr/bin/ash"

_patch_config() {
	local t="$1"
	shift
	while [ "$1" ]; do
		local p="$1"
		shift
		echo "Applying $p..."
		patch "${t}/.config" <"${FILESDIR}/dotconfig.d/${p}.patch"
	done
}

do_configure() {
	mkdir -p src
	mv * src || true

	local t
	for t in busybox busybox-static busybox-core busybox-huge; do
		mkdir -p $t
		cp -f ${FILESDIR}/dotconfig ${t}/.config

		case "$XBPS_TARGET_MACHINE" in
		*-musl) vsed -i -e /CONFIG_FEATURE_VI_REGEX_SEARCH/s/y/n/ \
				${t}/.config;;
		esac

		if [ "$CROSS_BUILD" ]; then
			vsed -i -e \
				"s,\(CONFIG_CROSS_COMPILER_PREFIX\).*,\1=\"${XBPS_CROSS_TRIPLET}-\",g" \
				${t}/.config
		fi
	done

	_patch_config busybox udhcpc ntpd
	_patch_config busybox-static udhcpc ntpd static
	_patch_config busybox-huge udhcpc ntpd httpd mdev sysklogd huge

	for t in busybox-core busybox busybox-static busybox-huge; do
		make -C "${t}" KBUILD_SRC="${wrksrc}/src" -f "${wrksrc}/src/Makefile" oldconfig
		make -C "${t}" KBUILD_SRC="${wrksrc}/src" -f "${wrksrc}/src/Makefile" prepare "${makejobs}"
	done

	if [ "$SOURCE_DATE_EPOCH" ]; then
		# rewrite date with the one from the date wrapper.
		d=$(date +"%F %T %Z")
		sed -i "s/\(#define AUTOCONF_TIMESTAMP\).*/\1 \"$d\"/" */include/autoconf.h
	fi
}

do_build() {
	for t in busybox-core busybox busybox-static busybox-huge; do
		make -C "${t}" SKIP_STRIP=y "${makejobs}"
	done
}

do_check() {
	for t in busybox-core busybox busybox-static busybox-huge; do
		make -C "${t}" "${makejobs}" \
			SKIP_KNOWN_BUGS=yes SKIP_INTERNET_TESTS=yes \
			check
	done
}

do_install() {
	vbin busybox/busybox_unstripped busybox
	vman busybox/docs/busybox.1
	vsv busybox-ntpd
}

busybox-core_package() {
	short_desc+=" (essential applets)"
	conflicts="busybox>=0 busybox-huge>=0"
	alternatives="${_alternatives_core}"
	register_shell="/usr/bin/ash"
	pkg_install() {
		vbin busybox-core/busybox_unstripped busybox
		vman busybox-core/docs/busybox.1
	}
}

busybox-static_package() {
	short_desc+=" (statically linked)"
	pkg_install() {
		vbin busybox-static/busybox busybox.static
	}
}

busybox-huge_package() {
	short_desc+=" (extra applets)"
	conflicts="busybox>=0 busybox-core>=0"
	alternatives="${_alternatives}"
	register_shell="/usr/bin/ash"
	pkg_install() {
		vbin busybox-huge/busybox_unstripped busybox
		vman busybox-huge/docs/busybox.1
		vsv busybox-ntpd
		vsv busybox-syslogd
		vsv busybox-klogd
	}
}