######################################################
#
# BYLD: Build Your Linux Disk
# License: GPL2 (see LICENSE)
#
# (C) 1999-2000, Erich Roncarolo <erich@roncarolo.eu.org>
#

######################################################
#
# This is the Buildfile for the 'BusyBox' package.
#

BUSYBOX=$PACK_WD/busybox-0.47	# BusyBox home

MAKE_BB=true			# If true, BusyBox is made (if needed) when
				# this script try to link it in root
				# distribution

function bf_log { echo "$@" >> $RLOG ; }

if [ ! -d $BUSYBOX -o ! -f $BUSYBOX/Config.h ] ; then
	( cd $PACK_WD ; rm -R $BUSYBOX &> /dev/null ; \
	/bin/sh ${BUSYBOX}.shar >> $RLOG 2>> $RLOG ; \
	find . -empty -exec rm {} \; )
fi

if [ ! -f $BUSYBOX/busybox ] ; then
	if [ "$MAKE_BB" = "true" ] ; then
		bf_log "Making BusyBox... "
		( cd $BUSYBOX ; \
		make distclean &> /dev/null || exit $? ; \
		make &> /dev/null || exit $? )
		bf_log "done."
		MADE=true
	else
		echo
		echo "You should build busybox first:"
		echo "# cd $BUSYBOX"
		echo "# make distclean ; make "
		exit 1 
	fi
fi

if [ ! -f $BUSYBOX/busybox.links ] ; then
	if [ "$MAKE_BB" = "true" ] ; then
		bf_log "Making busybox.links... "
		( cd $BUSYBOX ; \
		make clean &> /dev/null || exit $? ; \
		make &> /dev/null || exit $? )
		bf_log "done."
		MADE=true
	else
		echo
		echo "You should build busybox first:"
		echo "# cd $BUSYBOX/"
		echo "# make distclean ; make "
		exit 1 
	fi
fi

if [ "$MADE" != "true" ] ; then
	bf_log "Making BusyBox... "
	( cd $BUSYBOX ; make &> /dev/null || exit $? )
	bf_log "done."
fi

#bf_log -n "Copying busybox... "
#cp $BUSYBOX/busybox sbin/
#chmod 755 sbin/busybox
#chown 0.0 sbin/busybox
#bf_log "done."

#bf_log -n "Linking busybox... "
#while read bblink ; do
#	ln -s -i sbin/busybox ./$bblink < /dev/tty
#	chown 0.0 ./$bblink
#done < $BUSYBOX/busybox.links
#bf_log "done."
#bf_log

bf_log "Installing BusyBox... "
INSTALL_ROOT=$(pwd)
bf_log "( cd $BUSYBOX ; make install PREFIX=$INSTALL_ROOT >> $RLOG 2>> $RLOG || exit $? )"
( cd $BUSYBOX ; make install PREFIX=$INSTALL_ROOT >> $RLOG 2>> $RLOG || exit $? )
bf_log "done."

##############################
#
# Needed sbin files
#

if [ -x sbin/insmod ] ; then
	# This command look for a file list and put it in directory sbin
	bf_log "source Take root.root sbin modprobe"
	source Take root.root sbin modprobe
fi

##############################
#
# Needed etc files
#

# This command look for a file list and put it in directory etc
bf_log "source Take root.root etc inittab fstab"
source Take root.root etc inittab fstab

##############################
#
# Needed rc files
#

# This command look for a file list and put it in directory etc/rc.d
bf_log "source Take root.root etc/rc.d rc.sysinit rc.stop rc.ctrlaltdel"
source Take root.root etc/rc.d rc.sysinit rc.stop rc.ctrlaltdel

##############
#
# Profiles
#

source Take root.root etc/profile.d/ profile.busybox
test -f etc/profile.d/.profiles && PBB=$(grep profile.busybox etc/profile.d/.profiles)
if [ -z "$PBB" ] ; then
	echo "" >> etc/profile.d/.profiles
	echo "# BusyBox profile" >> etc/profile.d/.profiles
	echo "profile.busybox" >> etc/profile.d/.profiles
fi
chown 0.0 etc/profile.d/.profiles
bf_log "BusyBox .profiles"
bf_log $(cat etc/profile.d/.profiles)

exit 0

