# SliTaz package receipt.

PACKED_SIZE="292.0K"
UNPACKED_SIZE="760.0K"
PACKAGE="busybox"
VERSION="1.28-20170822"
GIT_TAG="753c4045e4f287fc4b4788afd94f58738a7f04b4"
CATEGORY="base-system"
SHORT_DESC="Busybox combines tiny versions of many common UNIX utilities."
MAINTAINER="pascal.bellard@slitaz.org"
LICENSE="GPL2"
[ -n "$TARGET" ] || TARGET="i486"
DEPENDS=""
BUILD_DEPENDS="bzip2 uclibc-cross-compiler-$TARGET wget"
TARBALL="$PACKAGE-$VERSION.tar.bz2"
WEB_SITE="http://www.busybox.net/"
WGET_URL="https://git.busybox.net/busybox/snapshot/busybox-$GIT_TAG.tar.bz2"
CONFIG_FILES="/etc/rcS.conf /etc/hostname /etc/fstab /etc/init.d/local.sh"
AUTO_SELECTION="always"

apply_bb_patchs()
{
    cd $src
    while read file; do
    	[ -f done.$file ] && continue
    	echo "Apply $file..."
    	patch -p1 < $stuff/$PACKAGE-git-$file || return 1
	touch done.$file
    done <<EOT
stat.u
ris.u
zmodules.u
cmdline.u
fbvnc.u
cpio.u
scriptreplay.u
ash.u
EOT
    [ $(. $WOK/linux/receipt; printf "%d%02d%02d" ${VERSION//./ }) -le 20626 ] &&
    	echo apply 0001-mdev-create-devices-from-sys-dev.patch &&
	patch -p1 -R < $stuff/0001-mdev-create-devices-from-sys-dev.patch
    cp $stuff/$PACKAGE-git.config .config
    var="CONFIG_CROSS_COMPILER_PREFIX"
    sed -i "s/.*$var.*/$var=\"uclibc-$TARGET-\"/" .config
}

# Rules to configure and make the package.
compile_rules()
{
    { apply_bb_patchs && 
    make oldconfig &&
    make &&
    make install
    } || return 1
    echo "Chmod 4755 on busybox binary..."
    chmod 4755 _install/bin/busybox
    mkdir -p _install/lib
    LD_LIBRARY_PATH=/usr/share/uclibc-cross-compiler-$TARGET/lib \
	uclibc-$TARGET-ldd busybox 2> /dev/null | \
	awk '/=>/ { print $3 }' | while read file ; do
	cp -a $file _install/lib
	while [ -L "$file" ]; do
		dir="$(dirname $file)/"
		file="$(readlink $file)"
		case "$file" in
		/*) ;;
		*)  file="$dir$file";;
		esac
		cp -a "$file" _install/lib
	done
    done
    chown 0.0 _install/lib/*
}

# Rules to gen a SliTaz package suitable for Tazpkg.
genpkg_rules()
{
    mkdir $fs/boot $fs/bin $fs/sbin
    cp -a $WOK/base-tiny/stuff/. $fs/
    cp -a $src/_install/lib $fs/
    cp -a $src/_install/bin/busybox $fs/bin
    for i in /bin/sh /bin/login /bin/false /sbin/mdev /init ; do
	ln $fs/bin/busybox $fs$i
    done
    mkdir -p $fs/etc/init.d
    # Busybox config files.
    cp $stuff/busybox.conf $fs/etc
    chmod 600 $fs/etc/busybox.conf
    touch $fs/etc/resolv.conf
    cp $stuff/daemon $fs/etc/init.d
    for i in crond klogd syslogd ; do
	grep -qi config_$i=y $src/.config &&
	ln -s daemon $fs/etc/init.d/$i
    done
    sed -i 's|network.sh ||' $fs/etc/rcS.conf
    sed -i 's|.*Executing all initialization scripts.*|echo -n "Setting hostname..."\
hostname -F /etc/hostname\
status\n&|' $fs/etc/init.d/rcS
    rm -f $fs/etc/init.d/network.sh $fs/etc/network.conf \
	$fs/etc/services $fs/etc/networks $fs/etc/inetd.conf \
	$fs/etc/host.conf $fs/etc/hosts
	
    sed -i '2,$s/^#.*//;/^$/d' $fs/etc/init.d/rc* $fs/etc/init.d/daemon \
	$fs/etc/init.d/*.sh
    sed -i 's/^#.*//;/^$/d' $fs/etc/*.conf $fs/etc/*tab \
	$fs/etc/profile $fs/etc/securetty $fs/etc/shells
    find $fs -exec touch -r $fs {} \;
    cp $stuff/busybox-git.config $fs/boot/config-busybox
}

config_form()
{
	if [ -z "$BBHOST" ]; then
		[ -n "$PASSWORD"   ] || PASSWORD=root
		[ -n "$BBHOST"     ] || BBHOST=slitaz
		[ -n "$FSTAB"      ] || FSTAB='/dev/hda1       /mnt         ext3    defaults          0       2'
		[ -n "$RC_LOCAL"   ] || RC_LOCAL='[ -x /mnt/boot/init ] && /mnt/boot/init'
	fi
	cat <<EOT
<table>
<tr>
<td>Root password (empty=disable)</td>
<td><input type="text" name="PASSWORD" value="$PASSWORD" /></td>
</tr>
<tr>
<td>Host name</td>
<td><input type="text" name="BBHOST" value="$BBHOST" /></td>
</tr>
<tr>
<td>Filesystems</td>
<td><textarea name="FSTAB" cols="60" wrap="off">
$FSTAB
</textarea></td>
</tr>
<tr>
<td>Additional boot commands</td>
<td><textarea name="RC_LOCAL" cols="60" wrap="off">
$RC_LOCAL
</textarea></td>
</tr>
</table>
<input type="checkbox" name="KEEP_MODULES" ${KEEP_MODULES:+checked="checked" }/>
Do not remove the modules files after loading modules.
<p>
<input type="checkbox" name="STUPID" ${STUPID:+ckecked="ckecked" }/>
Read floppy sector by sector instead of track by track (slow, stupid but safe).
EOT
}

post_install()
{
	chmod 4755 $1/bin/busybox
	ln -fs /proc/mounts $1/etc/mtab
	cat >> $1/boot/mkrootfs.pre_exec <<EOT
if [ -s modules ]; then
	tac modules | while read mod; do
		sed -i "s/LOAD_MODULES=./&\$mod /" etc/rcS.conf
	done
	grep ^LOAD_MODULES= etc/rcS.conf >> etc/packages.conf
	cp etc/packages.conf boot/packages.conf
	rm -f modules
fi
EOT
	[ "$STUPID" ] && cat >> $1/boot/mkrootfs.pre_exec <<EOT
for i in boot/bzImage boot/memtest boot/ipxe boot/plop boot/x86test; do
	[ -s \$i ] || continue
	dd if=\$i bs=512 count=1 | \\
	sed "s|\$(echo -en '\x64\x66\x8F\x47\x78')|\$(echo -en '\x5E\x5E\x90\x90\x90')|" | \\
	sed "s|\$(echo -en '\x66\x8F\x47\x78')|\$(echo -en '\x5E\x5E\x90\x90')|" | \\
	sed "s|\$(echo -en '\x77\x02\xB0\x01')|\$(echo -en '\x90\x90\xB0\x01')|" | \\
	sed "s|\$(echo -en '\x98\xCD\x13')|\$(echo -en '\x98\x90\x90')|" | \\
	sed "s|\$(echo -en '\x60\xCD\x13')|\$(echo -en '\x60\x90\x90')|" | \\
	sed "s|\$(echo -en '\xB0\x31\x1C\x03\xB4\x0E')|\$(echo -en '\xB0\x31\x14\xFD\xB4\x0E')|" | \\
	dd of=\$i conv=notrunc
done
EOT
	sed -i	"s/^KEEP_MODULES=\"\"/KEEP_MODULES=\"$KEEP_MODULES\"/" \
			$1/etc/rcS.conf
	if [ -n "$PASSWORD" ]; then
		case "$PASSWORD" in
		\$1\$*)
			sed -i "s|^root:[^:]*|root:$PASSWORD|" $1/etc/shadow ;;
		*)
			mkdir $1/fs
			cp -a $1/bin $1/lib $1/etc $1/fs
			echo "root:$PASSWORD" | chroot $1/fs/ busybox chpasswd -m
			cp $1/fs/etc/passwd $1/etc/
			rm -rf $1/fs
		esac
	else
		sed -i 's/^root:[^:]*:/root::/' $1/etc/passwd
		mkdir $1/root 2> /dev/null
		cat > $1/root/.profile <<EOT
grep -qs ^root:: /etc/passwd /etc/shadow && passwd
EOT
	fi
	[ -n "$BBHOST" ] && echo $BBHOST > $1/etc/hostname
	[ -n "$FSTAB" ] && dos2unix >> $1/etc/fstab <<EOT
$FSTAB
EOT
	[ -n "$RC_LOCAL" ] && dos2unix >> $1/etc/init.d/local.sh <<EOT
$RC_LOCAL
EOT
}
