### this file is sourced not run
PKGVER=1.36.0
PKGBUILD=1
PKGARCH=x86_64

# source: james
TARBALL=busybox-$PKGVER.tar.bz2
MD5SUM=835e3ac11e8a5ca13756eb1573719566
SRC_URL=https://busybox.net/downloads/$TARBALL
BUNDLE=

SLACKREQ=
SLACKDESC="busybox: busybox $PKGVER (All-in-one utilities)  
busybox:  
busybox: BusyBox combines tiny versions of many common UNIX utilities into a 
busybox: single small executable. It provides replacements for most of the 
busybox: utilities you usually find in GNU fileutils, shellutils, etc. The 
busybox: utilities in BusyBox generally have fewer options than their 
busybox: full-featured GNU cousins; however, the options that are included 
busybox: provide the expected functionality and behave very much like their 
busybox: GNU counterparts. BusyBox provides a fairly complete environment for 
busybox: any small or embedded system.  
busybox:
"

STATIC_CC="" # cross or musl or gcc or leave empty fo standard dynamic build
CROSS_COMPILER_PATH=/opt/abo64/bin
CROSS_COMPILE=x86_64-
PKGBUILD="${PKGBUILD}${STATIC_CC}"

### default pkg_download
### prepare
pkg_prepare() {
	pkg_prepare_default &&
	
	# Fatdog patches and default config
	cd tmp/busybox* &&
	patch -Np1 -i ../pkg/ours/busybox-1.22-20130821-guess_fstype-applet.patch &&
	patch -Np1 -i ../pkg/ours/fatdog-ntpd-script.patch &&
	patch -Np1 -i ../pkg/ours/busybox-fix-ntfs-warning.patch &&
	patch -Np1 -i ../pkg/ours/busybox-fix-conspy.patch &&

	# upstream patches
	for p in ../pkg/upstream/*; do
		patch -Np1 -i $p || return 1
	done &&

	# end
	true	
}

### default pkg_package
### build
pkg_build() {
	cd /tmp/busybox*
	if [ "$STATIC_CC" ]; then
		echo
		echo '=================================================='
		echo '================= STATIC BUILD ==================='
		echo '=================================================='
		#cp ../pkg/bb-config-initrd-1.27.0.git.2017.03 .config
		
		case "$STATIC_CC" in
			musl)
				echo "===== BUILD USING MUSL ====="
				MAKEFLAGS="$MAKEFLAGS CC=\"musl-gcc -I/usr/musl/kernel/include\""
				cp ../pkg/bb-config-initrd-1.36-$STATIC_CC .config				
				;;
			
			cross)
				echo "===== BUILD USING CROSS COMPILER: $CROSS_COMPILE ====="
				PATH=$PATH:$CROSS_COMPILER_PATH
				MAKEFLAGS="$MAKEFLAGS CROSS_COMPILE=$CROSS_COMPILE"
				cp ../pkg/bb-config-initrd-1.36-$STATIC_CC .config
				;;
			
			gcc)
				echo "===== BUILD USING GCC ====="
				MAKEFLAGS="$MAKEFLAGS CC=gcc"
				cp ../pkg/bb-config-initrd-1.36-$STATIC_CC .config
				;;
			*)
				echo "===== ERROR: UNSUPPORTED STATIC COMPILER ====="
				echo
				return 1
				;;
		esac
		echo

	else
		#cp ../pkg/bb-config-fatdog-1.27.0.git.2017.03 .config
		cp ../pkg/bb-config-fatdog-1.36 .config
	fi
	
	make oldconfig && # make defconfig
	eval make $MAKEFLAGS &&

	# install binary and udhcpc script; note: NO SYMLINK CREATED
	cp busybox /bin &&
	cp docs/busybox.1 /usr/share/man/man1 &&
	cp docs/BusyBox.txt /usr/share/doc &&
	mkdir -pv /usr/share/udhcpc /usr/share/bbscript &&
	install -m755 examples/udhcp/simple.script /usr/share/udhcpc/default.script &&
	install -m755 examples/zcip.script examples/var_service/ntpd/ntp.script /usr/share/bbscript &&	
	gcc -o /usr/share/bbscript/index.cgi networking/httpd_indexcgi.c &&
	gcc -o /usr/share/bbscript/httpd_ssi.cgi networking/httpd_ssi.c &&
	
	# samples
	cp -v examples/udhcp/udhcpd.conf /etc &&
	cp -v examples/inetd.conf /etc &&
	cp -v examples/dnsd.conf /etc &&
	cp -v examples/mdev_fat.conf /etc &&
	cp -v examples/mdev.conf.change_blockdev.sh	/etc &&
	sed -n '/\* httpd.conf/,/\/\*/ p' networking/httpd.c  > /etc/bb-httpd.conf &&
	cat >> /etc/bb-httpd.conf << EOF &&
/*
 * Remove comment to support SSI. It processes server-side includes:
 * <!--#include file="file.html" -->
 *
 * *.html:/usr/share/bbscript/httpd_ssi
 * *.htm:/usr/share/bbscript/httpd_ssi
 */
EOF
	#sh &&
	
	# applet list for further symlinking during SFS building later
	./busybox --list-full > /bin/busybox-applet-list &&
	pkg_build_slackdesc &&
	file busybox
}
   

