#!/bin/sh

ETC=$TOPDIR/etc

FUNCTIONS="findfiles installconfig installroot"

[ "NETWORK_TIME_SERVER" = "" ] || BUSYBOXR=rdate
BUSYBOXR="$BUSYBOXR SED"

function ml_installroot() {
        cd $PACKAGE

        [ "$NETWORK_MLCONFIG_CONNECT" = no ] || ml_mlconfig / mlconfig/connect.cgi mlconfig/disconnect.cgi mlconfig/connect.menu
        [ "$NETWORK_MLCONFIG_CONFIGURE" = no ] || ml_mlconfig / mlconfig/network.menu  mlconfig/network.cgi
        [ "$NETWORK_MLCONFIG_SSID" = no ] || ml_mlconfig / mlconfig/ssid.menu mlconfig/ssid.cgi

        # install default config
        cp eth0.def $ETC/net.eth0.default
	[ -f $ETC/net.eth0 ] || cp eth0.def $ETC/net.eth0

	# check if they have a default configuration for their 
	# network
        cp network.def $ETC/network.default
	[ -f $ETC/network ] || cp network.def $ETC/network

	mkdir -p $ROOT$CONFIG_DIR/net
    
	mkdir -p $ROOT/etc

	ln -sf /tmp/resolv.conf $ROOT/etc

	for i in network ifup ifdown ; do 
            ml_transpose $i $ROOT/sbin/$i
            chmod 755 $ROOT/sbin/$i
	done

        # create hosts file with localhost entry
        # All other entries are supposed to come from DNS
        echo "127.0.0.1 localhost" > $ROOT/etc/hosts

	for i in protocols services pump.conf ; do
	    install --mode=644 $i $ROOT/etc
	done

	# iterate through the files in $NET_CONF_DIR and copy them
	# into the image 
	for i in $ETC/net.* ; do 
           if [ `basename $i` = `basename $i .default` ] ; then
                install --mode=644 $i $ROOT$CONFIG_DIR/net/`basename $i | sed -e 's/.*\.//'`
           fi
	done

	# install network config file

	# set the SSID properly
        if [ -n "$NETWORK_SSID" ] ; then
                echo "$NETWORK_SSID" > $ROOT$CONFIG_DIR/wlan.ssid
        else
                rm -f $ROOT$CONFIG_DIR/wlan.ssid
        fi

        ml_transpose $ETC/network $ROOT$CONFIG_DIR/network
        chmod 644 $ROOT$CONFIG_DIR/network

        ml_transpose init.d/network $ROOT/etc/init.d/network 755
}
