#!/bin/sh

echo "Mounting kernel file systems"
mkdir /proc                2>/dev/null
mkdir /sys                2>/dev/null
mount -t proc none /proc
mount -t sysfs none /sys
echo 0x100 > /proc/sys/kernel/real-root-dev

module_load() {
   if find /lib/modules/`uname -r`/kernel/ -type f | egrep "$1.[k]?o" >/dev/null ; then
      echo "Installing kernel module $1"
      modprobe $* 2>/dev/null
   fi
}

do_reboot() {
   echo "*** REBOOTING ***"
   reboot
}

inturrupt_reboot() {
   echo "Press [ENTER] to inturrupt reboot and get a shell"
   read foo
   touch /noreboot
   /bin/sh
   do_reboot
}

reboot_30() {
   echo "Waiting 30 seconds, and rebooting..."
   sleep 30
   if [ ! -f "/noreboot" ]; then
      do_reboot
   fi
}

reboot_error() {
   reboot_30 &
   echo
   sleep 1
   inturrupt_reboot
}

cat /etc/load_modules | while read i; do
   module_load $i
done

echo "Begining hardware probe..."

/sbin/detect 2>/dev/null | egrep -v -e '(PCI|DISPLAY)' | cut -d ' ' -f 2 | while read i; do
   module_load $i
done

echo "Parsing the network configuration"

warewulf=`sed -n -e 's/.*warewulf=\([^ ]\+\).*/\1/p' /proc/cmdline`
if [ "x$warewulf" = "x" ]; then
   echo "/proc/cmdline is missing the warewulf network info!"
fi

HOSTNAME=`echo $warewulf | sed -n -e 's/\([^,]\+\),.*/\1/p'`
rest=`echo $warewulf | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`

if [ $HOSTNAME = "undef" ]; then
   MCOUNT=10
   # In case your wondering... Here we initialize all of the devices so that
   # we can pull the right mac address for each device.
   ifconfig eth0 up >/dev/null 2>&1
   ifconfig eth1 up >/dev/null 2>&1
   ifconfig eth2 up >/dev/null 2>&1
   ifconfig eth3 up >/dev/null 2>&1
   ifconfig eth4 up >/dev/null 2>&1
   ifconfig eth5 up >/dev/null 2>&1
   ifconfig eth6 up >/dev/null 2>&1
#   while true; do
      for i in 0 1 2 3 4 5 6 ; do
         if ifconfig eth$i up 2>/dev/null; then
            echo "Initialized device: eth$i!"
            GOTNET=1
            MAC_ADDR=`mac-addr eth$i`
            echo "Waiting for device initalization..."
            sleep 6
            echo "trying to reach '$rest' through eth$i (addr: $MAC_ADDR)..."
            udhcpc --now -i eth$i -s /sbin/dhcp-script >/dev/null 2>&1
            if wget http://$rest:9875/$MAC_ADDR >/dev/null 2>&1; then
               echo
               echo "*** SUCCESS ***"
               echo
               echo "Got a new node configuration!"
               echo
               do_reboot
#            else
#               break
            fi
         fi
      done
      if [ "x$GOTNET" = "x" ]; then
         echo
         echo "*** ERROR ***"
         echo
         echo "I don't see any supported network interfaces to boot on! Check"
         echo "the included kernel drivers in master:/etc/warewulf/config and"
         echo "confirm that this system has a supported network card."
         echo
         reboot_error
      fi
      echo "*** ERROR ***"
      echo
      echo "Couldn't get a new node configuration..."
      echo "Check to make sure that wwnewd is running on the master node, and"
      echo "that you have added this node to a node group."
      echo
      reboot_error
#   done
fi

MASTER=`echo $rest | sed -n -e 's/\([^,]\+\),.*/\1/p'`
rest=`echo $rest | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`
GATEWAY=`echo $rest | sed -n -e 's/\([^,]\+\),.*/\1/p'`
rest=`echo $rest | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`
VNFS=`echo $rest | sed -n -e 's/\([^,]\+\),.*/\1/p'`
rest=`echo $rest | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`
#STORAGE_DEV=`echo $rest | sed -n -e 's/\([^,]\+\),.*/\1/p'`
#rest=`echo $rest | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`
while [ "x$rest" != "x" ]; do
   dev=`echo $rest | sed -n -e 's/\([^=]\+\).*/\1/p'`
   IPADDR=`echo $rest | sed -n -e 's/[^=]\+=\([^:]\+\).*/\1/p'`
   NETMASK=`echo $rest | sed -n -e 's/[^=]\+=[^:]\+:\([^,]\+\).*/\1/p'`
   rest=`echo $rest | sed -n -e 's/[^,]\+,\(.*\)/\1/p'`
   echo "Configuring network on $dev:$IPADDR/$NETMASK"
   /sbin/ifconfig $dev $IPADDR netmask $NETMASK up
done

echo "Setting hostname: $HOSTNAME"
hostname $HOSTNAME

echo "Setting default route: $GATEWAY"
route add default gw $GATEWAY

# Exporting some vars in case $VNFS.wwinitrc needs them.
export HOSTNAME
export MASTER
export VNFS
#export STORAGE_DEV
export IPADDR
export NETMASK

if [ -x /sbin/custom_net ]; then
   echo "Starting custom_net"
   /bin/sh /sbin/custom_net
   echo "custom_net has left the building"
fi

echo "Getting the wwinitrc"
wget http://$MASTER:9874/$VNFS/wwinitrc 

if [ $? != "0" ]; then
   echo
   echo "*** ERROR ***"
   echo
   echo "Could not get $VNFS/wwinitrc from $MASTER (that is important)!"
   echo "Make sure that you have the VNFS '$VNFS' installed, configured"
   echo "and built as well as restart the vnfsd service."
   echo
   reboot_error
fi

echo "   Starting wwinitrc"
/bin/sh ./wwinitrc


cd /newroot/tmp
echo "Checking for post script (wwpostrc)"
wget http://$MASTER:9874/$VNFS/wwpostrc >/dev/null 2>&1
if [ $? = "0" ]; then
   echo "   Starting wwpostrc"
   mount -t proc none /newroot/proc
   chroot /newroot/ /bin/sh /tmp/wwpostrc
   umount /newroot/proc
fi

echo "Checking for node specific post script (wwinitrc-$HOSTNAME)"
wget http://$MASTER:9874/$VNFS/wwpostrc-$HOSTNAME >/dev/null 2>&1
if [ $? = "0" ]; then
   echo "   Starting wwpostrc-$HOSTNAME"
   mount -t proc none /newroot/proc
   chroot /newroot/ /bin/sh /tmp/wwpostrc-$HOSTNAME
   umount /newroot/proc
fi
cd /


echo "Migrating the Warewulf tools to new root"
mkdir /newroot/wwtools
cp /bin/busybox /newroot/wwtools
ln -s busybox /newroot/wwtools/wget
ln -s busybox /newroot/wwtools/tar
ln -s busybox /newroot/wwtools/gzip
cat <<EOF > /newroot/wwtools/syncnode
#!/bin/sh
if [ \$UID != "0" ]; then
   echo "Got root?"
   exit 1
fi
cd /
/wwtools/wget -O - http://$MASTER:9874/$VNFS/synctree.tar.gz | \
   /wwtools/tar xzf - 2>/dev/null
if [ \$? = "0" ]; then
   echo "READY" > /.nodestatus
   echo "   `hostname`: ready"
else
   echo "ERROR" > /.nodestatus
   echo "   `hostname`: error"
fi

chmod 755 /

exit
EOF
cat <<EOF > /newroot/wwtools/syncvnfs
#!/bin/sh
if [ \$UID != "0" ]; then
   echo "Got root?"
   exit 1
fi
cd /
BACKUPS="/etc/fstab /etc/mtab /var/run /var/lock"

for i in \$BACKUPS; do
   /bin/cp -rap \$i \$i.bak
done

/wwtools/wget -O - http://$MASTER:9874/$VNFS/vnfs.tar.gz | \
   /wwtools/tar xvzf - > /var/log/syncfs.log

for i in \$BACKUPS; do
   /bin/rm -rf \$i
   /bin/mv  \$i.bak \$i
done

chmod 755 /

exit
EOF
chroot /newroot chmod +x /wwtools/syncnode
chroot /newroot chmod +x /wwtools/syncvnfs
if [ ! -f "/.nodestatus" ]; then
   chroot /newroot /wwtools/syncnode
fi

echo "Copying over dev tree to the VNFS"
(cd /dev; find . ) | while read i; do
   echo -n "."
   cp -ap /dev/$i /newroot/dev/$i   2>/dev/null
done
echo

echo "Unmounting kernel file systems"
umount /sys                         2>/dev/null
umount /proc                        2>/dev/null

echo "Doing the pivot_root"
cd /newroot
mkdir initrd                        2>/dev/null
pivot_root . initrd

echo "WWINITRD is handing over control to the VNFS..."
