# SliTaz package receipt.

PACKED_SIZE="412.0K"
UNPACKED_SIZE="972.0K"
PACKAGE="busybox"
VERSION="1.22.1"
CATEGORY="base-system"
SHORT_DESC="Busybox combines tiny versions of many common UNIX utilities."
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="GPL2"
TARBALL="$PACKAGE-$VERSION.tar.bz2"
WEB_SITE="http://www.busybox.net/"
WGET_URL="http://www.busybox.net/downloads/$TARBALL"
HOST_ARCH="i486 arm"

DEPENDS="slitaz-base-files glibc-base ncurses-common"
BUILD_DEPENDS="bzip2 pam pam-dev uclibc-cross-compiler-i486 \
musl-libc-dev dietlibc"

CONFIG_FILES="/etc/dnsd.conf /etc/inetd.conf /etc/udhcpd.conf \
/etc/resolv.conf /etc/httpd.conf"
AUFS_NOT_RAMFS="uclibc-cross-compiler-i486 is not compatible with aufs+tmpfs 8("

# Handle cross compilation
case "$ARCH" in
	arm) BUILD_DEPENDS="bzip2" ;;
esac

# Busybox patches
apply_bb_patchs()
{
    cd $src
    while read file; do
    	[ -f done.$file ] && continue
    	echo "Apply $file..."
    	patch -p1 < $stuff/$PACKAGE-${VERSION%.*}-$file || return 1
	touch done.$file
    done <<EOT
tar.u
stat.u
ris.u
zmodules.u
printable.u
cmdline.u
diff.u
diet.u
losetup.u
EOT
    cp $stuff/$PACKAGE-${VERSION%.*}.config .config
}

# Rules to configure and make the package.
compile_rules()
{
	case "$ARCH" in
		arm)
			echo "cook: CROSS_COMPILE=$CROSS_COMPILE"
			apply_bb_patchs &&
			cp $stuff/arm/$PACKAGE.config .config
			make oldconfig &&
			make && make install || return 1
			chmod 4755 $src/_install/bin/busybox ;;
		x86_64) echo "TODO" ;;
		i?86)
			apply_bb_patchs &&
			make oldconfig &&
			make && make install || return 1
		    strip --strip-unneeded -R .eh_frame -R .eh_frame_hdr \
				$src/_install/bin/busybox

			# prepare busybox-pam package
		    sed -i 's/# CONFIG_PAM is not set/CONFIG_PAM=y/' .config
		    make oldconfig && make || return 1
		    strip --strip-unneeded -R .eh_frame -R .eh_frame_hdr busybox
		    mv busybox busybox-pam

		    # prepare busybox-static package
		    if [ -x /usr/bin/uclibc-i486-gcc ]; then
			cp $stuff/$PACKAGE-${VERSION%.*}.config-static .config
			make oldconfig && make || return 1
			cp busybox busybox-static
			mv busybox busybox-uclibc
		    fi

		    if [ -x /usr/bin/musl-gcc ]; then
			# prepare busybox-musl package
			cp $stuff/$PACKAGE-${VERSION%.*}.config-static .config
			sed -i 's|uclibc-i486-||' .config
			make oldconfig && make CC=musl-gcc || return 1
			mv busybox busybox-musl
		    fi

		    if [ -x /usr/lib/diet/bin/diet ]; then
			# prepare busybox-diet package
			cp $stuff/$PACKAGE-${VERSION%.*}.config-static .config
			sed -i 's|uclibc-i486-||;s|CFLAGS="|&-D_BSD_SOURCE |;s|LDFLAGS="|&-Wl,--allow-multiple-definition |;s|LDLIBS="|&compat rpc |' .config
			make oldconfig && make CC="/usr/lib/diet/bin/diet gcc" || return 1
			mv busybox busybox-diet
		    fi

		    # prepare ssfs-busybox package
		    rootfs=$src/ssfs-busybox/usr/share/ssfs/rootfs
		    mkdir -p $rootfs/etc
		    cp $stuff/$PACKAGE-${VERSION%.*}.config-ssfs .config
		    make oldconfig && make busybox &&
		    make CONFIG_PREFIX=$rootfs install || return 1
		    cp $stuff/busybox.conf-ssfs $rootfs/etc/busybox.conf
		    chown -R 0.0 $rootfs/etc
		    chmod 0600 $rootfs/etc/busybox.conf
		    chmod 4755 $rootfs/bin/busybox ;;
	esac
}

# Cross compilation check.
testsuite()
{
	readelf -h $src/_install/bin/busybox
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
	cp -a $src/_install/* $fs
	rm -f $fs/bin/bbconfig $fs/usr/bin/ar
	mkdir -p $fs/etc/init.d $fs/var/spool/cron/crontabs $fs/var/spool/lpd

	# Busybox config files.
	for f in busybox.conf dnsd.conf udhcpd.conf inetd.conf httpd.conf \
		syslog.conf zcip.script
	do
		cp $stuff/$f $fs/etc
	done
	chown -R 0.0 $fs/etc
	chmod 600 $fs/etc/busybox.conf
	touch $fs/etc/resolv.conf

	# Daemon scripts.
	cp $stuff/daemon $fs/etc/init.d
	DAEMON="crond dnsd ftpd httpd inetd lpd klogd ntpd syslogd telnetd tftpd udhcpd zcip"
	for i in $DAEMON; do
		grep -qi config_$i=y $stuff/$PACKAGE-${VERSION%.*}.config &&
		ln -s daemon $fs/etc/init.d/$i
	done
	rm $fs/linuxrc
	mkdir -p $fs/etc/modprobe.d

	# Udhcpc stuff.
	mkdir -p $fs/usr/share/udhcpc
	cp $stuff/udhcp.script $fs/usr/share/udhcpc/default.script
	chmod +x $fs/usr/share/udhcpc/default.script

	# Httpd stuff.
	ln -s /usr/lib/slitaz/httphelper.sh $fs/usr/bin/httpd_helper.sh
	cp -a $stuff/www $fs/var
}

# GNU utils stuff.
pre_install()
{
	local i
	cp -a /etc/resolv.conf /etc/resolv.conf-busybox-install
	answer=""
	for i in $(cat $1$INSTALLED/$PACKAGE/files.list); do
		[ -f $1$i ] || continue
		case "$i" in
			/bin/busybox) continue ;;
			*bin/*) ;;
			*) continue ;;
		esac
		if [ -z "$answer" ]; then
			echo -n "Keep installed GNU utilities ? "
			read -t 30 answer	# by default: keep
			case "$answer" in
			n*|N*) break;;
			*) answer="Y";;
			esac
		fi
		cp -a $1$i $1$i-busybox-install
	done
}

post_install()
{
	local i
	[ -f /etc/resolv.conf-busybox-install ] &&
	mv -f /etc/resolv.conf-busybox-install /etc/resolv.conf
	while read i ; do
		[ -f $1$i-busybox-install ] || continue
		mv $1$i-busybox-install $1$i
	done < $1$INSTALLED/$PACKAGE/files.list
	chmod 4755 $1/bin/busybox

	# /etc/daemons.conf (tftp + dnsd + httpd may not be present)
	if ! grep -q ^DNSD_OPTIONS $root/etc/daemons.conf; then
		echo '# Domain name server options.' >> $root/etc/daemons.conf
		echo 'DNSD_OPTIONS="-d"' >> $root/etc/daemons.conf
		echo '' >> $root/etc/daemons.conf
	fi
	if ! grep -q ^TFTPD_OPTIONS $root/etc/daemons.conf; then
		echo '# Tftp daemon options.' >> $root/etc/daemons.conf
		echo 'TFTPD_OPTIONS="-r /boot"' >> $root/etc/daemons.conf
		echo '' >> $root/etc/daemons.conf
	fi
}

pre_remove()
{
	# We can not remove this package !
	exit 1
}
