# Contributor: Ɓukasz Jendrysik <scadu@yandex.com>
# Contributor: Oliver Smith <ollieparanoid@bitmessage.ch>
# Maintainer: 
pkgname=busybox
pkgver=1.28.4
pkgrel=2
pkgdesc="Size optimized toolbox of many common UNIX utilities"
url="https://busybox.net"
arch="all"
license="GPL-2.0-only AND GPL-2.0+"
makedepends_build="openssl-dev"
makedepends_host="linux-headers"
makedepends="$makedepends_build $makedepends_host"
checkdepends="zip"
provides="/sbin/init"
install="$pkgname.post-install $pkgname.post-upgrade $pkgname.pre-deinstall
	$pkgname-extras.post-install $pkgname-extras.pre-deinstall"
subpackages="$pkgname-static $pkgname-suid $pkgname-extras $pkgname-binsh"
options="suid !check"
triggers="busybox.trigger=/bin:/usr/bin:/sbin:/usr/sbin:/lib/modules/*"
source="http://busybox.net/downloads/$pkgname-$pkgver.tar.bz2
	0001-ash-add-support-for-command_not_found_handle-hook-fu.patch
	0002-fsck-resolve-LABEL-.-UUID-.-spec-to-device.patch
	0003-ash-exec-busybox.static.patch
	0004-app-location-for-cpio-vi-and-lspci.patch
	0005-udhcpc-set-default-discover-retries-to-5.patch
	0006-ping-make-ping-work-without-root-privileges.patch
	0007-fbsplash-support-console-switching.patch
	0008-fbsplash-support-image-and-bar-alignment-and-positio.patch
	0009-depmod-support-generating-kmod-binary-index-files.patch
	0010-Add-flag-for-not-following-symlinks-when-recursing.patch
	0011-sysklogd-add-Z-option-to-adjust-message-timezones.patch
	0012-udhcpc-Don-t-background-if-n-is-given.patch
	0013-testsuite-fix-cpio-tests.patch
	0014-miscutils-microcom-Fixed-segfault.patch
	0015-ash-introduce-a-config-option-to-search-current-dire.patch
	0016-top-handle-much-larger-VSZ-values.patch
	0017-ifupdown-do-not-fail-if-interface-disappears-during-.patch

	0001-cat-fix-cat-e-and-cat-v-erroneously-numbering-1st-li.patch

	0001-wget-emit-a-message-that-certificate-verification-is.patch

	acpid.logrotate
	busyboxconfig
	busyboxconfig-extras
	bbsuid.c
	dad.if-up
	nologin.c
	"

# secfixes:
#   1.27.2-r4:
#   - CVE-2017-16544
#   - CVE-2017-15873
#   - CVE-2017-15874

builddir="$srcdir"/$pkgname-$pkgver

_staticdir="$srcdir"/build-static
_dyndir="$srcdir"/build-dynamic
_dyndir_extras="$srcdir"/build-dynamic-extras
_config="$srcdir"/busyboxconfig
_config_extras="$srcdir"/busyboxconfig-extras

prepare() {
	default_prepare

	cd "$builddir"
	mkdir -p "$_staticdir" "$_dyndir" "$_dyndir_extras"
	cp "$srcdir"/nologin.c loginutils/
}

build() {
	# build bbsuid
	msg "Building bbsuid"
	${CC:-${CROSS_COMPILE}gcc} ${CPPFLAGS} ${CFLAGS} \
		${LDFLAGS} "$srcdir"/bbsuid.c -o "$_dyndir"/bbsuid

	# build dynamic
	cd "$_dyndir"
	msg "Building dynamic busybox"
	cp "$_config" .config
	[ "$CLIBC" = musl ] && sed -i \
		-e "s/CONFIG_EXTRA_COMPAT=y/CONFIG_EXTRA_COMPAT=n/" \
		.config
	make -C "$builddir" O="$PWD" silentoldconfig
	make

	# build dynamic (extras)
	cd "$_dyndir_extras"
	msg "Building dynamic busybox-extras"
	cp "$_config_extras" .config
	[ "$CLIBC" = musl ] && sed -i \
		-e "s/CONFIG_EXTRA_COMPAT=y/CONFIG_EXTRA_COMPAT=n/" \
		.config
	make -C "$builddir" O="$PWD" silentoldconfig
	make

	# build static
	cd "$_staticdir"
	msg "Building static busybox"
	# enable internal ssl_client for static build
	sed -e "s/.*CONFIG_PIE.*/\# CONFIG_PIE is not set/" \
		-e "s/.*CONFIG_STATIC.*/CONFIG_STATIC=y/" \
		-e "s/.*CONFIG_SSL_CLIENT.*/CONFIG_SSL_CLIENT=y/" \
		"$_config" > .config
	# musl does not support GNU regex
	[ "$CLIBC" = musl ] && sed -i \
		-e "s/CONFIG_EXTRA_COMPAT=y/CONFIG_EXTRA_COMPAT=n/" \
		.config
	make -C "$builddir" O="$PWD" silentoldconfig
	make
	mv busybox busybox.static
}

check() {
	cd "$_dyndir"
	SKIP_KNOWN_BUGS=1 make -C "$builddir" O="$PWD" V=1 check

	cd "$_dyndir_extras"
	SKIP_KNOWN_BUGS=1 make -C "$builddir" O="$PWD" V=1 check
}

package() {
	cd "$_dyndir"
	mkdir -p "$pkgdir"/usr/sbin "$pkgdir"/usr/bin "$pkgdir"/tmp \
		"$pkgdir"/var/cache/misc "$pkgdir"/bin "$pkgdir"/sbin
	chmod 1777 "$pkgdir"/tmp
	install -m755 busybox "$pkgdir"/bin/busybox || return 1
	# we need /bin/sh to be able to execute post-install
	ln -s /bin/busybox "$pkgdir"/bin/sh

	#ifupdown needs those dirs to be present
	mkdir -p \
		"$pkgdir"/etc/network/if-down.d \
		"$pkgdir"/etc/network/if-post-down.d \
		"$pkgdir"/etc/network/if-post-up.d \
		"$pkgdir"/etc/network/if-pre-down.d \
		"$pkgdir"/etc/network/if-pre-up.d \
		"$pkgdir"/etc/network/if-up.d
	install -m775 "$srcdir"/dad.if-up "$pkgdir"/etc/network/if-up.d/dad

	install -Dm644 "$srcdir"/acpid.logrotate \
		"$pkgdir/etc/logrotate.d/acpid"

	mkdir -p "$pkgdir"/var/lib/udhcpd
	install -Dm644 "$builddir"/examples/udhcp/udhcpd.conf \
		"$pkgdir"/etc/udhcpd.conf
	cat >"$pkgdir"/etc/securetty <<EOF
console
tty1
tty2
tty3
tty4
tty5
tty6
tty7
tty8
tty9
tty10
tty11
EOF
}

extras() {
	pkgdesc="Additional binaries of Busybox"
	depends="${pkgname}"
	install -Dm755 "$_dyndir_extras"/busybox "$subpkgdir"/bin/busybox-extras
}

suid() {
	pkgdesc="suid binaries of Busybox"
	depends="${pkgname}"

	cd "$_dyndir"
	mkdir -p "$subpkgdir"/bin
	install -m4111 bbsuid "$subpkgdir"/bin/bbsuid
}

static() {
	pkgdesc="Statically linked Busybox"
	mkdir -p "$subpkgdir"/bin
	install -m755 "$_staticdir"/busybox.static \
		"$subpkgdir"/bin/busybox.static
}

binsh() {
	pkgdesc="Provide /bin/sh with BusyBox ash"
	depends="${pkgname}"
	provides="/bin/sh"
	provider_priority=0

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

sha512sums="92471617fcf3c1e28b468f3de2c83a1041f5ba5106580cc791e9c4cd602b7ccffabc51ce0807881ed734a89c3089113048265d6659a4d595528bd9150288d2ed  busybox-1.28.4.tar.bz2
51d4d58baff825a51d476bd4594cb8980ec2aa4d0c864a0eec39ccbbadd1ae9f1cd1b20f492a735ffcdf7c925573594f3c4363b0561c8aa7b91ef534bfc7b2e0  0001-ash-add-support-for-command_not_found_handle-hook-fu.patch
5d2fd3e521ee29d970f377363e3a3144eaf9f7714bc57494d743ded9e39c1ad93ea8759b2febd9c3786968b41e61b8d01ce2361aa997df177b644d63718470ba  0002-fsck-resolve-LABEL-.-UUID-.-spec-to-device.patch
cc5e5ce7a822ef51eb6b8065e1b802bc9d690857b8123cb4decf51b09c4ef655784401a68dd26e0a681fbb64bd2c0fed4001be6e33cac9049e516587ea53c17d  0003-ash-exec-busybox.static.patch
5f0611d21d1dc106d43ba23234babd41a2167d7032f3b09e825ae3dc9f9aaeb8d1882f59341daff99adecdfb8ba52a5c9fb423c8df3168b2e2304c5bd0ac4991  0004-app-location-for-cpio-vi-and-lspci.patch
bd4bb1f29f0287aa2ae4e43d791072802ba4f8863ea968612410a2819f7afaec5c0c731b4d91f360461ebfe26942f21e9ff69cbd5fb7d9800e62ef59fe954ab2  0005-udhcpc-set-default-discover-retries-to-5.patch
f03f852b97f3875d3051b225e6ffe52ed02ae8a8550287b3e09c2ef4d63914e1ab045ba5e2bc2dc2f3c8bf643485de4ebb36b97c74a8a6e49b6ba0261f2ddb94  0006-ping-make-ping-work-without-root-privileges.patch
a7b4ddb838f51e86040246e11a4b0df7bce37ed5159634bd2a8f388cf57be0551e6e63887b71b530a8a7b86dbdda6bbb3a090173f040050ea864b5f07674395d  0007-fbsplash-support-console-switching.patch
f8655833f71715629a4a93997939ca295cacb35f17843a36cb6fda285213bdbd258b2f84e321f81859491971412a520f697748ebd6fb46808f830f4ccfa77af4  0008-fbsplash-support-image-and-bar-alignment-and-positio.patch
f69031d048aa5e087cb6597ad2f7b39b520a5ef3713731e9090c5b65680cd7075bdf3a9443725fac49dce4345bc3345dc702b95d618d45a6d3be8682844784f4  0009-depmod-support-generating-kmod-binary-index-files.patch
b558f3ceb63e39545c3219796da64a7962bb53515a4eedea2bf9d81057774096b429145f0cd98da2542e3bdadaf92fb11d710fb1879768c9980bf58ccd104b6e  0010-Add-flag-for-not-following-symlinks-when-recursing.patch
f81d7966133d40a5d79cab4a4edf695bf7cc1f06cf2382c4ed99eea3a72cd222fe36c7b92e427262f67a81fcc2e7f6bff5986b32362c38da3c5163a49fd1ecab  0011-sysklogd-add-Z-option-to-adjust-message-timezones.patch
a96aa81d2f0104b5c28f02e80b3f77dbce77af93c174c09015a34850474d69e42c160fc8061c62f03465b6f793ead109dde7b0cc367d1e87694805d823f19d7e  0012-udhcpc-Don-t-background-if-n-is-given.patch
40c125a2ba19bcfaec46084bef98acb775a7f2521d854df41411afcfbc3025a1bdd029b0baf74550923db2541c23c1e9df5d5ded799d1d46dd7cf86a495e4c57  0013-testsuite-fix-cpio-tests.patch
4cbd38a3c2730ae38e34c5973bb63e40609c32f700d4943cc0e922394e8ee522d1512eb19c7885f5cee49834ab22b2594cb07164cacffefa39964a3b863f4e50  0014-miscutils-microcom-Fixed-segfault.patch
832eb44c52d2caad4bf6ea79fb17f10c116de3e90ed79038dabe3736d8e74507d1e0cb6f4f7689b4dd506b92437d8df7862038fc0213ecda259e40baf9d9b3de  0015-ash-introduce-a-config-option-to-search-current-dire.patch
185f11578dc3c3637f1acd1285c71b9e31f4244c57cd85b0848912c085a7a8c833d4c935ab1cadcb9852cf3185c7ffb08db8ea728fb19ab6e6fa90d89f13c75b  0016-top-handle-much-larger-VSZ-values.patch
d90d6b3406760fe3df6dbed46a0f4d1c02a69d5184ebc86d8c1692bc4576532127283ba3ff9a81e64f3660c279b8ee324dac7a426350873c45957067648651c6  0017-ifupdown-do-not-fail-if-interface-disappears-during-.patch
0dbe3ee424c0a6e4aba4f551f6b6b9ee087655a03747a40906961b141d40b1cbb2345438f17887a1b78d880cb3a7ad0116936dd7c05e95160febfd299423e83b  0001-cat-fix-cat-e-and-cat-v-erroneously-numbering-1st-li.patch
90f9e95f6f953df2cf579f701b3135bad910f514e94b3e23b953acec12b10f33aa9200169dc7b454c6a04fbd992a370e6ca835406f9b5495951e0a8416137000  0001-wget-emit-a-message-that-certificate-verification-is.patch
a9b1403c844c51934637215307dd9e2adb9458921047acff0d86dcf229b6e0027f4b2c6cdaa25a58407aad9d098fb5685d58eb5ff8d2aa3de4912cdea21fe54c  acpid.logrotate
d65dc165488a179ab19482ad74e350df9dfdccf2363b26424d2d145e27ab0819cd0cfdfb79b4a2bd0bd7c6eda3b95ea61f3c264357986e78c4675df94d487aec  busyboxconfig
0efbe22e2fd56993d92b6542d4ccffb2b42d50495be085c98f417a71f503b4071e2f092afcec77f78064d33ffb0922c28daa3cb9958e6d7fb26d5a660abd90f4  busyboxconfig-extras
0becc2186d6c32fb0c401cf7bc0e46268b38ce8892db33be1daf40273024c1c02d518283f44086a313a2ccef34230a1d945ec148cc173f26e6aa9d88a7426e54  bbsuid.c
a1127c8a384294135e11500fde7ead33b73d24b11c21911b08447a4c4ef71d7a9965d6466f60f2da64e3b877213b0a3e924a5add3c5333ee3ecde8c2a91c5e02  dad.if-up
061f7417c1cbf0424a5fab77e2f5912aa1593f39b33ea294af4c03518ca712d793a77ea82ff1f36e9cb98751d9faacb9d0240cdf0894efd8f26c13c28a692404  nologin.c"