#!/bin/bash

# this script builds two version of busybox in parallel,
# one for the root partition and one for the usr partition

BUSYBOX_ROOT="$BUSYBOX_ROOT $BR"
BUSYBOX_USR="$BUSYBOX_USR $BU"
BUSYBOX_NEW_SRC="halt.c reboot.c wget.c od.c keydown.c kill.c uplo.c su.c spawn.c backlight.c"
BUSYBOX_NEW="od keydown uplo su spawn backlight"

touch $IMAGE/busybox.root $IMAGE/busybox.usr
ROOT_TOOLS=`(echo $BUSYBOX_ROOT ; cat $IMAGE/busybox.root) | tr ' ' '\012' | sort -u`
USR_TOOLS=`(echo $BUSYBOX_USR ; cat $IMAGE/busybox.usr) | tr ' ' '\012' | sort -u`
USR_TOOLS=`(echo $BUSYBOX_ROOT $BUSYBOX_ROOT $USR_TOOLS; cat $IMAGE/busybox.root $IMAGE/busybox.root) | tr ' ' '\012' | sort | uniq -u`

CONFIGURE_OPTIONS="root $ROOT_TOOLS usr $USR_TOOLS forbidden $BUSYBOX_FORBIDDEN"
PATCHIT_FILES=`echo " $BUSYBOX_NEW_SRC" | sed "s@ @ $BUILD/$PACKAGE/@g"`

NEWOPTIONS=`echo $BUSYBOX_NEW | tr -s ' ' ','`

function ml_untar() {
        cd $PACKAGE
        rm -rf root usr
	tar xzf $PACKAGE.tar.gz
        mkdir root
        cd $PACKAGE
        cp Config.h Config.h.orig
        mv * ../root
        cd ..
        cp -a root usr
}

function ml_patchit() {
        cd $PACKAGE
        for d in root usr
        do
                cd $d
                for i in $PATCHIT_FILES
                do
                        n=`basename $i`
                        if ! cmp $i $n > /dev/null 2> /dev/null ; then
                                cp $i $n
                        fi
                done
		if [ "$NEWOPTIONS" != "" ] ; then
                        perl -pi -e 'if(/\{0,NULL,0,NULL\}/) { foreach $l ('$NEWOPTIONS') { $u=$l;$u=~tr/[a-z]/[A-Z]/;print "#ifdef BB_$u\n    {\"$l\",${l}_main,_BB_DIR_USR_BIN,${l}_usage},\n#endif\n"; }  }' busybox.c
                        perl -pi -e 'if(/yes_main/) { foreach $l ('$NEWOPTIONS') { print "extern int ${l}_main(int argc, char** argv);\nextern const char ${l}_usage[];\n"; }  }' busybox.h
                        perl -pi -e 'if(/BB_YES/) { foreach $l ('$NEWOPTIONS') { $u=$l;$u=~tr/[a-z]/[A-Z]/;print "#define BB_$u\n"; }  }' Config.h.orig
                        perl -pi -e 's/(.*STRIP.*)--remove-section=\.note --remove-section=\.comment(.*)/$1$2/' Makefile
                fi
                sed '/BusyBox Applications/,/End of Applications/s/^#/\/\/#/' Config.h.orig > Config.h.nothing
                cd ..
        done
}


function ml_configure() {
        cd $PACKAGE
        for d in usr root
        do
                if [ $d = root ] ; then
                        tools=$ROOT_TOOLS
                else
                        tools=$USR_TOOLS
                fi
                [ "$tools" != "" ] || continue

                cd $d
                if expr match "$tools" ".*fat"
                then
                        cp Config.h.orig Config.h.work
                else
                        cp Config.h.nothing Config.h.work
                        for i in $tools
                        do
                                perl -pi -e "s/.*#define BB_$i$/#define BB_$i/" Config.h.work
                        done
                fi
                for i in $BUSYBOX_FORBIDDEN
                do
                        perl -pi -e "s/.*#define BB_$i$/\/\/#define BB_$i/" Config.h.work
                done
                perl -pi -e "s/.*#define BB_FEATURE_TRIVIAL_HELP$/#define BB_FEATURE_TRIVIAL_HELP/" Config.h.work
                if ! cmp -s Config.h.work Config.h ; then
                        cp Config.h.work Config.h
                fi
                cd ..
        done
}

function ml_compile() {
        cd $PACKAGE
        for d in usr root
        do
                if [ $d = root ] ; then
                        tools=$ROOT_TOOLS
                else
                        tools=$USR_TOOLS
                fi
                [ "$tools" != "" ] || continue

                $JMAKE CC=$GCC BUILDTIME="" -C $d
        done
        if ml_is_package_installed mlconfig ; then
                ml_required_package mlconfig
                $JMAKE CC=$CC CFLAGS=$CFLAGS LDFLAGS="$LDFLAGS" -C mlconfig
        fi
}

function ml_installroot() {
        ml_installroot_default
        cd $PACKAGE

        [ "$BUSYBOX_MLCONFIG_REBOOT" = no ] || ml_mlconfig / mlconfig/reboot.menu mlconfig/reboot.cgi
        [ "$BUSYBOX_MLCONFIG_POWER_OFF" = no ] || ml_mlconfig / mlconfig/poweroff.menu mlconfig/poweroff.cgi
        [ "$BUSYBOX_MLCONFIG_SWITCH" = no ] || ml_mlconfig / mlconfig/switch.menu mlconfig/switch.cgi
        [ "$BUSYBOX_MLCONFIG_ROOT" = no ] || ml_mlconfig / mlconfig/root.menu mlconfig/root.cgi
        [ "$BUSYBOX_MLCONFIG_TIME" = no ] || ml_mlconfig /usr mlconfig/time.menu mlconfig/time.cgi
        [ "$BUSYBOX_MLCONFIG_TIMEZONE" = no ] || ml_mlconfig /usr mlconfig/timezone.menu mlconfig/timezone.cgi

        for d in usr root
        do
                if [ $d = root ] ; then
                        tools=$ROOT_TOOLS
                        dest=$ROOT/bin
                else
                        tools=$USR_TOOLS
                        dest=$ROOT/usr/bin
                fi
                [ "$tools" != "" ] || continue

                mkdir -p $dest
                cp -f $d/busybox $dest/b

                h=`sort $d/busybox.links | uniq`
                for i in $h ; do
                        ln -sf b $dest/`basename $i`
                done
        done
        ml_transpose init.d/busybox /usr/init.d/busybox 755
}
