#!/bin/sh

# (C) Copyright 2003 by Gerd v. Egidy <gve@intra2net.com>
#
# PCI autodetect Copyright 2001-2003 by Diego Torres Milano
# PCI autodetect adapted from PXES by Gerd v. Egidy
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as 
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#

autodetect_pci() # PCICLASS MODCLASS
{
	local PCICLASS NOTFOUND line

	PCICLASS=$1
	NOTFOUND=1

	if [ -z "$PCICLASS" ]; then
		echo "$PROGNAME: autodetect_pci missing parameter" >&2
		exit 1
	fi

	if [ ! -r /proc/bus/pci ]; then
		return $NOTFOUND
	fi

	# I can't make a pipe of this because ash treats it as a subshell,
	# that's why the temp file
	echo "Running lspci..."
	/sbin/lspci_simple |grep "$PCICLASS"|awk '{print $1}' >/tmp/pci.$$
#	echo "Lspci_simple returns."
#	sleep 20
	while read line
	do
		set -- $line
#		echo "In loop (autodetect_pci) line = $line"
		grep -i $line /lib/modules/modmap.list
		mod=`grep -i $line /lib/modules/modmap.list |awk '{print $1}'`
#		echo "In loop (autodetect_pci) mod = $mod"
#		sleep 20
		if [ "$mod" != "UNKNOWN" ]; then
#		    echo "loading $mod..."
#		    sleep 60
		    /sbin/modprobe $mod
		    NOTFOUND=0
		fi
	done < /tmp/pci.$$
	/bin/rm -f /tmp/pci.$$

	return $NOTFOUND
}

echo "Remount / read-write"
mount -n -o remount,rw /

echo "Mounting /proc filesystem"
mount -n -t proc /proc /proc

echo "Autodetecting network devices..."
autodetect_pci 'Class 02..'
if [ $? -ne 0 ]; then
    echo "WARNING: no network interface found"
fi


#echo "Loading e100 module (network driver)"
#modprobe e100
#sleep 20

echo "Loading nfs modules"
modprobe nfs
#sleep 20

echo "Initializing network loopback device"
ifconfig lo 127.0.0.1 up
    
echo "Configuring eth0"
udhcpc --now --quit --interface=eth0 --script=/bin/udhcpc.script
if [ $? -ne 0 ]; then
    echo "ERROR: can't get DHCP lease"
    exit 0
fi

# load DHCP parameter
. /etc/udhcpc-eth0.info

#cat /etc/udhcpc-eth0.info

#echo "ROOTPATH is ${ROOTPATH}"
#sleep 10

#ROOTPATH=${NEXTSERVER}:/nfsroot

echo "Mounting nfs root filesystem"
if ! echo $ROOTPATH | grep -q ":/" ; then
	# we haven't got a full path, use next-server
	ROOTPATH="${NEXTSERVER}:${ROOTPATH}"
fi

if echo $ROOTPATH | grep -q "," ; then
	# we have options
	NFSOPTIONS=`echo $ROOTPATH | sed -e "s/\(.*\)\(,.*\)/\2/"`
	ROOTPATH=`echo $ROOTPATH | sed -e "s/\(.*\)\(,.*\)/\1/"`
fi

echo "Mounting root filesystem"
mount -rw -t tmpfs none /sysroot/

echo "Mounting NFS root-base: $ROOTPATH"
mkdir /sysroot/nfsroot
mount -n -r -v -o "nolock${NFSOPTIONS}" -t nfs "$ROOTPATH" /sysroot/nfsroot
if [ $? -ne 0 ]; then
    echo "ERROR: can't mount root filesystem via NFS"
    exit 0
fi

echo "Setting root symlinks"
cd /sysroot
#find ./nfsroot -maxdepth 1 -mindepth 1 -exec ln -s \{\} \;
for f in ./nfsroot/* ./nfsroot/.??*; do
#echo "*** Setting root symlinks: $f = $f"
ln -s $f ./
done
cd /

#sleep 10

echo "Handling root's HOME"

rm -f /sysroot/root
mkdir /sysroot/root
cp -a /sysroot/nfsroot/root /sysroot

echo "Handling special directories"
rm -f /sysroot/initrd
mkdir /sysroot/initrd
rm -f /sysroot/tmp
mkdir /sysroot/tmp
chmod 1777 /sysroot/tmp
rm -f /sysroot/proc
mkdir /sysroot/proc
rm -f /sysroot/sys
mkdir /sysroot/sys
rm -f /sysroot/usr
mkdir /sysroot/usr
rm -f /sysroot/home
mkdir /sysroot/home
rm -f /sysroot/selinux
mkdir /sysroot/selinux
rm -f /sysroot/media
mkdir /sysroot/media
rm -f /sysroot/opt
ln -s /usr/opt /sysroot/opt

echo "Copying /var"
rm -f /sysroot/var
mkdir /sysroot/var
cp -a /sysroot/nfsroot/var /sysroot
cp /etc/resolv.conf /sysroot/var/resolv.conf
echo "Linking /etc/modprobe.conf and /etc/sysconfig"
echo "HOSTNAME = ${HOSTNAME}"
ln -s modprobe.conf.${HOSTNAME} /sysroot/var/etc/modprobe.conf
ln -s sysconfig.${HOSTNAME} /sysroot/var/etc/sysconfig
#sleep 20

# /dev handling
rm -f /sysroot/dev
mkdir /sysroot/dev
#RPH: probably don't need *another* tmpfs on the root for /dev...
#mount -t tmpfs --bind /dev /sysroot/dev
mknod /sysroot/dev/console c 5 1
mknod /sysroot/dev/tty0 c 4 0
mknod /sysroot/dev/null c 1 3  
mknod /sysroot/dev/zero c 1 5
mkdir /sysroot/dev/pts
mkdir /sysroot/dev/shm

echo 0x0100 > /proc/sys/kernel/real-root-dev

echo "Unmounting temporary mounts"
umount /proc
#umount /sys

echo "Changing to new NFS root"
pivot_root /sysroot /sysroot/initrd
/bin/mount -t proc /proc /proc
/bin/mount -t sysfs none /sys
#echo Starting udev 
#/sbin/start_udev
#echo -n "/sbin/hotplug" > /proc/sys/kernel/hotplug
echo "Args are: $*"
#/bin/sh
/bin/chmod 755  /
exec /sbin/init $*
