#!/bin/sh

PATH=/sbin:/bin

echo Busybox /linuxrc starting

echo Mounting /proc filesystem
mount -t proc none /proc

echo=echo
if grep '\bquiet\b' /proc/cmdline > /dev/null; then
  echo=true
  quiet=1
fi

$echo Creating root device
mknod /dev/root b 1 0 2>/dev/null
chmod 700 /dev/root
echo 0x100 > /proc/sys/kernel/real-root-dev

$echo Inserting modules
if [ -z "$quiet" ]; then
  /bin/insert-modules
else
  /bin/insert-modules >/dev/null
fi

$echo Bringing up loopback interface
ifconfig lo 127.0.0.1 up
route add -net 127.0.0.0 netmask 255.0.0.0 lo

# Hack required for prism2 cards
# It is not yet possible to use iwconfig to configure these cards,
# so we need wlanctl.
if ifconfig wlan0 down 2> /dev/null; then
  $echo Setting up wireless link
  wlanctl wlan0 lnxreq_ifstate ifstate=enable
  wlanctl wlan0 lnxreq_autojoin ssid= authtype=opensystem
fi

$echo Obtaining IP address via DHCP
$echo Trying to obtain IP address via wired link [eth0]
if udhcpc -i eth0 -f -n -q -s /bin/udhcpc-post; then
  $echo Successfully obtained IP address via wired link [eth0]
else
  if udhcpc -i eth0 -f -n -q -s /bin/udhcpc-post; then
     $echo Successfully obtained IP address via wired link [eth0]
  else
     if udhcpc -i eth0 -f -n -q -s /bin/udhcpc-post; then
        $echo Successfully obtained IP address via wired link [eth0]
     else
        $echo Failed to obtain IP address via wired link [eth0]
        $echo Trying to obtain IP address via wireless link [wlan0]
        udhcpc -i wlan0 -f -n -q -s /bin/udhcpc-post
     fi
  fi
fi

if [ -d /sysroot/initrd ]; then
  SIP=`cat etc/SIP`
  $echo "SIP: $SIP"
  IP=`cat etc/IP`
  $echo "IP: $IP"
  
  $echo Unmounting /proc prior to pivot_root
  umount /proc

  # unionfs setup
  if [ -d /sysroot/var/lib/terminal-server/common ]; then
    # unionfs module is huge - makes initrd too big
    $echo Inserting unionfs module from server root
    zcat /sysroot/lib/modules/`uname -r`/kernel/fs/unionfs/unionfs.ko.gz > unionfs.ko
    insmod unionfs.ko
    $echo Remounting root directory to prepare for unionfs join
    umount /sysroot > /dev/null 2>&1
    mount -t nfs -o nolock,rsize=8192,wsize=8192 $SIP:/ /sysroot/root 
    $echo Adding common nfs mount point
    mount -t nfs -o nolock,rsize=8192,wsize=8192 $SIP:/var/lib/terminal-server/common /sysroot/common
    if [ -d /sysroot/root/var/lib/terminal-server/clients/$IP ]; then
      $echo Adding client specific nfs mount point for $IP
      mount -t nfs -o nolock,rsize=8192,wsize=8192 $SIP:/var/lib/terminal-server/clients/$IP /sysroot/client
      # start out ro - otherwise early boot writes stuff here and fastboot
      # doesn't always show up
      # change it rw in the terminal-server init script
      # update - ro doesn't work, mount rw and use unionctl to change it
      MYDIR="/sysroot/client:"
      ROCLIENT="/usr/sbin/unionctl / --mode ro /initrd/sysroot/client"
      $echo Combining root, common, client nfs mount points into unionfs root
    else
      MYDIR=""
      ROCLIENT=""
      $echo Combining root, common nfs mount points into unionfs root
    fi
    mount -t unionfs -odirs=$MYDIR/sysroot/common:/sysroot/root=ro unionfs /unionroot
  fi

  $echo Pivoting root to /unionroot
  pivot_root /unionroot /unionroot/initrd
  cd /
  
  # now change to ro - ugly hack since unionfs refuses to mount ro
  $echo Remount base union branches ro
  mount -t proc none /proc
  $ROCLIENT
  /usr/sbin/unionctl / --mode ro /initrd/sysroot/common
  umount /proc
  
  mount -t tmpfs /dev /dev
  mknod /dev/console c 5 1
  mkdir /dev/pts
  mount -t devpts none /dev/pts

  $echo Mounting client init directories to mask server init
  mount -t nfs -o nolock,rsize=8192,wsize=8192 $SIP:/var/lib/terminal-server/nfs/etc/rc.d/rc3.d /etc/rc.d/rc3.d
  mount -t nfs -o nolock,rsize=8192,wsize=8192 $SIP:/var/lib/terminal-server/nfs/etc/rc.d/rc5.d /etc/rc.d/rc5.d
else
  # Failed to mount root: report error and hang
  echo FATAL ERROR: Failed to mount root filesystem
  echo Press Alt-SysRq-B or hit the reset switch to reboot
  while : ; do sleep 6000 ; done
fi


