#!/bin/sh

FUNCTIONS="findfiles installconfig compile installlocal installroot"
COMPILE_OPTIONS="$BOOT_SERVER $QUIET_BOOT $TEST_CLIENT $SUPPORT_LBA"
[ "$WATCHDOG_TIMER" = 0 ] || BUSYBOXR="$BUSYBOXR reboot"
[ "$SET_ACTIVE" != wget ] || BUSYBOXR="$BUSYBOXR wget"
[ "$SET_ACTIVE" != ping ] || BUSYBOXR="$BUSYBOXR ping"

ml_required_package cramfs

function ml_compile() {
	cd $PACKAGE/src

	# compile set_boot and make_active for local tools
	$MAKE clean
	PATH=${ORIGPATH} $JMAKE GCC=gcc CC=gcc AS=as LD=ld CFLAGS= LDFLAGS= set_boot make_active
	mv set_boot set_boot.local
	mv make_active make_active.local

	# compile everything else for target
	$MAKE clean
	$JMAKE LD="${TARGETARCH}-ld" all
}

function ml_installlocal() {
	cd $PACKAGE/src
	# target stuff
	cp cmos make_active set_boot cramfsboot.bin newmbr.bin $TARGET
	# local stuff
	cp set_boot.local $TOOLS/bin/set_boot
	cp make_active.local $TOOLS/bin/make_active
}

function ml_installroot() {
        cd $PACKAGE
	cp src/set_boot $ROOT/sbin/set_boot
	cp src/make_active $ROOT/sbin/make_active
	[ "$BOOT_SERVER" = no ] || cp src/cmos.ml $ROOT/sbin/cmos
        # Install boot, ping, wget, and watchdog init scripts
        case $SET_ACTIVE in
        boot)
                ml_transpose init.d/boot $ROOT/etc/init.d/boot 755
                ;;
        ping)
                ml_transpose init.d/ping $ROOT/etc/init.d/ping 755
                cp -f pinger $ROOT/sbin
                ;;
        wget)
                ml_transpose init.d/wget $ROOT/etc/init.d/wget 755
                cp -f wgeter $ROOT/sbin
                ;;
        esac

        if [ "$WATCHDOG_TIMER" != 0 ] ; then
                ml_transpose init.d/watchdog $ROOT/etc/init.d/watchdog 755
        fi
}
