#!/bin/sh

# function ocs_inittab
ocs_inittab() {
  task=$1
  option=$2
  cat <<-EOF > /etc/inittab
# /etc/inittab: init(8) configuration.
# $Id: init,v 1.5 2003/11/30 08:23:16 klhaung Exp $

# The default runlevel.
id:1:initdefault:

# Boot-time system configuration/initialization script.
# This is run first except when booting in emergency (-b) mode.
si::sysinit:/etc/init.d/rcS

# What to do in single-user mode.
#~~:S:wait:/sbin/sulogin
~~:S:wait:/opt/drbl/sbin/drbl-ocs $task $option

# /etc/init.d executes the S and K scripts upon change
# of runlevel.
#
# Runlevel 0 is halt.
# Runlevel 1 is single-user.
# Runlevels 2-5 are multi-user.
# Runlevel 6 is reboot.

l0:0:wait:/etc/init.d/rc 0
l1:1:wait:/etc/init.d/rc 1
l2:2:wait:/etc/init.d/rc 2
l3:3:wait:/etc/init.d/rc 3
l4:4:wait:/etc/init.d/rc 4
l5:5:wait:/etc/init.d/rc 5
l6:6:wait:/etc/init.d/rc 6
# Normally not reached, but fallthrough in case of emergency.
z6:6:respawn:/sbin/sulogin

# What to do when CTRL-ALT-DEL is pressed.
ca:12345:ctrlaltdel:/sbin/shutdown -t1 -a -r now

# Action on special keypress (ALT-UpArrow).
#kb::kbrequest:/bin/echo "Keyboard Request--edit /etc/inittab to let this work."

# What to do when the power fails/returns.
pf::powerwait:/etc/init.d/powerfail start
pn::powerfailnow:/etc/init.d/powerfail now
po::powerokwait:/etc/init.d/powerfail stop

# /sbin/getty invocations for the runlevels.
#
# The "id" field MUST be the same as the last
# characters of the device (after "tty").
#
# Format:
#  <id>:<runlevels>:<action>:<process>
#
# Note that on most Debian systems tty7 is used by the X Window System,
# so if you want to add more getty's go ahead but skip tty7 if you run X.
#
1:2345:respawn:/sbin/getty 38400 tty1
2:23:respawn:/sbin/getty 38400 tty2
3:23:respawn:/sbin/getty 38400 tty3
4:23:respawn:/sbin/getty 38400 tty4
5:23:respawn:/sbin/getty 38400 tty5
6:23:respawn:/sbin/getty 38400 tty6

# Example how to put a getty on a serial line (for a terminal)
#
#T0:23:respawn:/sbin/getty -L ttyS0 9600 vt100
#T1:23:respawn:/sbin/getty -L ttyS1 9600 vt100

# Example how to put a getty on a modem line.
#
#T3:23:respawn:/sbin/mgetty -x0 -s 57600 ttyS3
EOF
}

# Don't do anything special if computer is already booted
if [ $$ -ne 1 ]
then
	exec /sbin/init.orig $*
	echo "FATAL ERROR: exec /sbin/init.orig failed" >&2
	exit 1
fi

# Don't do anything special if root is rw (ie master system).
if touch /test-rw
then
	rm /test-rw
	echo "WARNING: master system installed" >&2
	exec /sbin/init.orig $*
	echo "FATAL ERROR: exec /sbin/init.orig failed" >&2
	exit 1
fi
# Try and find configuration file
if [ -f /etc/diskless-image/config ]
then
  # mount /var/log/ksymoops as tmpfs
  # marked by Blake, Kuo-Lien Huang for Demo CD
  #mount -t tmpfs -o nr_inodes=24576,size=2M none /var/log/ksymoops
  # Get IP address, need to mount /proc first
  mount -n none /proc -t proc
  #marked by Blake, Kuo-Lien Huang to detect multiple network cards
  #IP=`ifconfig | grep -A1 eth0 | grep -v eth0 | sed 's/^.*inet addr:\([0-9\.]\+\).*$/\1/'`
  NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
  for DEVICE in $NETDEVICES
  do
    IP=`ifconfig | grep -A1 $DEVICE | grep -v $DEVICE | grep "inet" | sed 's/^.*inet addr:\([0-9\.]\+\).*$/\1/'`
    echo "Client IP address is $IP [$DEVICE]"
    if [ "$IP" != "" ]; then

      # 2003/08/18 Blake, Kuo-Lien Huang: for demoCD, mount /usr first
      NFSSERVER=`/sbin/route -n | grep -e "^0.0.0.0" | awk '{ print $2; }'`
      #echo "mount -t nfs -o nolock,rsize=8139,wsize=8139 $NFSSERVER:/usr /usr"
      mount -t nfs -o nolock,rsize=8192,wsize=8139 $NFSSERVER:/usr /usr

      #NETWORK=`echo $IP | awk -F. '{print $1"."$2"."$3".0"; }'`
      NETMASK=`ifconfig $DEVICE | grep "Mask:" | awk '{ print $4; }' | cut -d: -f2`
      NETWORK=`/usr/bin/netmask -n $IP/$NETMASK | cut -d/ -f1 | awk '{print $1}'`
      if [ "$NETWORK" = "" ]; then 
        NETWORK=`echo $IP | awk -F. '{print $1"."$2"."$3".0"; }'`
      fi

      # public ip alias
      if [ -e /etc/diskless-image/public_ip.$NETWORK ]; then
        PUBLIC_IP=`grep "$IP" /etc/diskless-image/public_ip.$NETWORK | awk '{ print $2" netmask "$3; }'`
        PUBLIC_GW=`grep "$IP" /etc/diskless-image/public_ip.$NETWORK | awk '{ print $4; }'`
        if [ "$PUBLIC_IP" = "" ]; then
          PUBLIC_IP=`grep -i "$HWADDR" /etc/diskless-image/public_ip.$NETWORK | awk '{ print $2" netmask "$3; }'`
          PUBLIC_GW=`grep -i "$HWADDR" /etc/diskless-image/public_ip.$NETWORK | awk '{ print $4; }'`
        fi
        
        if [ "$PUBLIC_IP" != "" ]; then
          ifconfig $DEVICE:1 $PUBLIC_IP
          GW=`route -n | grep -e "^0\.0\.0\.0" | awk '{ print $2; }'`
          route del default gw $GW
          route add default gw $PUBLIC_GW dev $DEVICE
        fi
      fi

      # Blake, 2003/11/30, drbl-client-switch
      MODE=""
      if [ -f /etc/drblcd/drbl-client-switch ]; then
        MODE=$(cat /etc/drblcd/drbl-client-switch)
      fi

      # marked by Blake, Kuo-Lien Huang for Demo CD
      #exec /sbin/$NETWORK/init $*
      #echo "WARRNING: exec /sbin/$NETWORK/init failed" >&2
	  #echo "mount -t tmpfs -o size=4M none /tmp"
      mount -t tmpfs -o size=4M none /tmp

      #echo "mount -t tmpfs -o size=8M none /etc"
      mount -t tmpfs -o size=8M none /etc
      tar -C /etc -xzmf /sbin/$IP/etc.tar.gz
      rm /etc/fstab; touch /etc/fstab

      #echo "mount -t tmpfs -o size=8M none /var"
      mount -t tmpfs -o size=8M none /var
      tar -C /var -xzmf /sbin/$IP/var.tar.gz

      #echo "mount -t tmpfs -o size=8M none /home"
      mount -t tmpfs -o size=8M none /home
	  tar -C /home -xzmf /sbin/$IP/home.tar.gz

      #echo "mount -t tmpfs -o size=4M none /root"
      mount -t tmpfs -o size=4M none /root

      #echo "mount -t nfs -o nolock,rsize=8139,wsize=8139 $NFSSERVER:/opt /opt"
      mount -t nfs -o nolock,rsize=8192,wsize=8139 $NFSSERVER:/opt /opt

      # Blake, 2003/11/30, drbl-client-switch
      case "$MODE" in
        "remote-linux-graphic")
          ;;
        "remote-linux-text")
          rm -f /etc/rc2.d/S20xfs
          rm -f /etc/rc2.d/S98setupx
          rm -f /etc/rc2.d/S99xdm
          rm -f /etc/rc2.d/S99kdm
          ;;
        "remote-linux-thin")
          for login_user in `ls /home` do;
            echo "fvwm95" > /home/$login_user/.wmrc
          done
          ;;
        "remote-linux-www")
           for login_user in `ls /home` do;
            echo "wm2" > /home/$login_user/.wmrc
          done
          ;;
        "clonezilla-save-disk")
          option=$(cat /etc/drblcd/clonezilla-save-disk)
          ocs_inittab savedisk "$option"
          ;;
        "clonezilla-restore-disk")
          option=$(cat /etc/drblcd/clonezilla-restore-disk)
          ocs_inittab restoredisk "$option"
          ;;
        "clonezilla-save-hda1")
          option=$(cat /etc/drblcd/clonezilla-restore-hda1)
          ocs_inittab save "$option" 
          ;;
        "clonezilla-restore-hda1")
          option=$(cat /etc/drblcd/clonezilla-restore-hda1)
          ocs_inittab restore "$option"
          ;;
      esac

      rm -f /etc/mtab~ /etc/nologin
      : > /etc/mtab
    fi
  done
fi

echo "WARRNING: DRBL setup incomplete or network failed" >&2
exec /sbin/init.orig $*
echo "FATAL ERROR: exec /sbin/init.orig failed" >&2

