#!/bin/bash
################################################################################
# $Id: init 185 2006-05-24 23:24:17Z makia $
################################################################################
#  Copyright (C) 2004 The Regents of the University of California.
#  Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
#  Written by Makia Minich <makia@llnl.gov>
#  UCRL-CODE-155916
#  
#  This file is part of GeDI, a diskless image management tool.
#  For details, see <http://www.llnl.gov/linux/gedi/>.
#  
#  GeDI is free software; you can redistribute it and/or modify it under
#  the terms of the GNU General Public License as published by the Free
#  Software Foundation; either version 2 of the License, or (at your option)
#  any later version.
#  
#  GeDI 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 GeDI; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
################################################################################

# Source variables file
. /lib/gedi_libs.sh
. /lib/gedi

echo "Starting GeDI System Startup"

mount -n -t proc /proc /proc

# load eth0 device
if [ -n "${ETH0_DEVICE}" ] ; then
   gedi_modprobe /lib/modules ${ETH0_DEVICE} ${ETH0_OPTS}
else
   for mod in $(gedi_ethmodules /lib/modules) ; do
      gedi_modprobe /lib/modules ${mod}
   done
fi

if bootmac=$(gedi_bootmac) ; then
   gedi_fixeth0 ${bootmac}
fi

ifconfig eth0 up
gedi_getipconf
ifconfig eth0 ${ipconf_ip} netmask ${ipconf_netmask} broadcast ${ipconf_bcast}
hostname ${ipconf_hostname}

ip route add default via ${ipconf_router} dev eth0

gedi_modprobe /lib/modules nfs

IMG="/sysroot"
if [ -n "$ipconf_ip" ] ; then
   echo "Mounting NFS Root"
   gedi_mountnfs ${ipconf_server}:${ipconf_rpath} ${IMG} ${MOUNT_OPTIONS}
else
   echo "Loading cdrom modules"
   load_mod cdrom
   load_mod ide-cd

   for dev in \`find /proc/ide/ -type l\` ; do
      if ( grep -q cdrom $dev/media ) ; then
         CDROM=\`echo $dev | awk -F/ '{print "/dev/"$4}'\`
         ln -sf $CDROM /dev/cdrom
         break
      fi
   done

   if [ ! -f /dev/cdrom ] && [ -d /proc/scsi/usb-storage ] ; then
      ln -sf /dev/scd0 /dev/cdrom
   fi

   echo "Mounting LiveCD"
   mount -n -o ro /dev/cdrom ${IMG}
   IMG=/sysroot/image
fi

echo 0x0100 > /proc/sys/kernel/real-root-dev
umount /proc
cd ${IMG}
mount -n --move . /
echo "Passing to linuxrc."
exec chroot . sh -c "exec /linuxrc \"$@\""
