#!/bin/sh

if [ $$ -ne 1 ]
then
	exec /sbin/init.orig $*
	echo "FATAL ERROR: exec /sbin/init.orig failed"
	exit 1
fi

if [ -f /etc/diskless-image/config ]
then
	mount -n none /proc -t proc
	#IP=`cat /proc/cmdline | sed 's/^.*nfsaddrs=\([0-9\.]\+\).*$/\1/'`
	IP=`ifconfig | grep -A1 eth0 | grep -v eth0 | sed 's/^.*inet addr:\([0-9\.]\+\).*$/\1/'`
	echo IP address is $IP

	. /etc/diskless-image/config
fi

if touch /test-rw
then
	rm /test-rw
	echo "WARNING: master system installed"

	exec /sbin/init.orig $*
	echo "FATAL ERROR: exec /sbin/init.orig failed"
	exit 1
fi

if [ -f /etc/diskless-image/config ]
then
	echo -n "Mounting NFS-root directories..."
	mount -n $nfsserver:/$nfshostsdir/$IP/etc /etc -orw,nolock
	rm -f /etc/mtab~ /etc/nologin
	: > /etc/mtab

	mount -o remount /
	mount -o remount /etc
	mount -o remount /proc
	echo "done."
else
	echo "Not mounting NFS-root directories"
fi

exec /sbin/init.orig $*
echo "FATAL ERROR: exec /sbin/init.orig failed"
exit 1
