#!/bb/bin/sh

export PATH=/bb/bin:/bb/sbin:/bb/usr/bin:/bb/usr/sbin:$PATH

. /bb/config

# determine the IP
ifconfig $IFACE up
sleep 3
udhcpc -i $IFACE -q -s /bb/dhcpdc.sh
IP=`cat /bb/MYIP`
HOSTNAME=`cat /bb/HOSTNAME`
if [ "$IP" = "" ]
then
    echo "Cannot get IP!"
    ifconfig -a
    sleep 1000
fi
echo "Got IP $IP, hostname $HOSTNAME"

# make the directories where to mount
mkdir -p /dev/hopeless/clients
mkdir -p /dev/hopeless/roots

# mount the client specific dir
mount -o nolock,tcp,rw $SERVER:$CDIR /dev/hopeless/clients
if [ ! -e /dev/hopeless/clients/$IP/hopeless.root ]
then
    echo '!!! Cannot mount client dir or missing file hopeless.root in it'
    sleep 1000
fi
if [ ! -e /dev/hopeless/clients/$IP/hopeless.overlay ]
then
    echo '!!! Missing file hopeless.overlay in client dir'
    sleep 1000
fi

# mount the generic root dir and the cluster wide overlay
export MY_ROOT=`cat /dev/hopeless/clients/$IP/hopeless.root`
export MY_OVERLAY=`cat /dev/hopeless/clients/$IP/hopeless.overlay`
mount -o nolock,tcp,rw $SERVER:$RDIR /dev/hopeless/roots

# do the union and check that it looks healthly
mount -t unionfs -o dirs=/dev/hopeless/clients/$IP=rw:/dev/hopeless/roots/$MY_OVERLAY=ro:/dev/hopeless/roots/$MY_ROOT=ro none /sysroot
#sleep 3

# assure we don't try to fsck the remote fs
rm /sysroot/.autofsck
echo fastboot > /sysroot/fastboot

