#!/bin/sh

#export CONSOLE=/dev/ttyS0
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/mnt/root/bin:/mnt/root/sbin:/mnt/root
export DISPLAY=:0.0
. /ts4700.subr


# Define script that starts GUI environment
export INITGUI=/init-xorgenv

mount -t proc proc /proc                                   
mount -t sysfs sysfs /sys                                  
mount -t devpts devpts /dev/pts

ifconfig lo 127.0.0.1 up
route add -net 127.0.0.0 netmask 255.0.0.0 lo
xuartctl --server

eval `ts4700ctl --info`
export bootdev model baseboard_model pclk dclk

eval `ts4700ctl --getdio`
let dio9="(dio >> 9) & 0x1"
XCONSOLE=0

if [ "$dio9" == "1" ]; then
  export CONSOLE=/dev/ttyS0
else
  eval `xuartctl --server --port 1 </dev/null 2>&1`

  if [ "$ttyname" == "" ]; then
    export CONSOLE=/dev/ttyS0
  else
    export CONSOLE=$ttyname
    XCONSOLE=1
    # If the RESET switch is being pushed, wait/feed wdog until released
    while [ "$dio9" == "0" ]; do
        eval `ts4700ctl --getdio --redledon --greenledoff`
        let dio9="(dio >> 9) & 0x1"
        peekpoke 16 0x80004006 0x02
    done
    ts4700ctl --greenledon --redledoff
  fi
fi
ln -sf $CONSOLE /dev/tsconsole

setconsole -r $CONSOLE
stty -F $CONSOLE ospeed 115200 sane > /dev/null 2>&1
hostname ts4700
ts4700ctl --setrng --resetswitchon --redledoff --autofeed 1 
ts4700ctl --loadfpga=ts4700_bitstream.vme.gz
check-usb-update 0x4700 < /dev/null > /dev/null 2>&1 &

(
  #If we are booted from NAND we want to boot to Debian on NAND, same with SD

  nandctl -X -z 131072 --nbdserver lun0:disc,lun0:part1,lun0:part2,lun0:part3,lun0:part4
  nbd-client 127.0.0.1 7525 /dev/nbd0
  nbd-client 127.0.0.1 7526 /dev/nbd1
  nbd-client 127.0.0.1 7527 /dev/nbd2
  nbd-client 127.0.0.1 7528 /dev/nbd3
  nbd-client 127.0.0.1 7529 /dev/nbd4
  if [ "$bootdev" == "0x1" ]; then
    x=0
    while [ "$x" -lt 150 -a ! -e /sys/block/nbd3/ ]; do
            x=$((x+1))
            sleep .1
    done
    mount -oro /dev/nbd3 /mnt/root.ro
  fi

  if [ "$bootdev" == "0x0" ]; then
    x=0
    while [ "$x" -lt 150 -a ! -e /sys/block/mmcblk0/mmcblk0p4/ ]; do
            x=$((x+1))
            sleep .1
    done
    mount -oro /dev/mmcblk0p4 /mnt/root.ro
  fi

  insmod /mnt/root.ro/lib/modules/2.6.29-ts4700-00/kernel/fs/unionfs/unionfs.ko
  mount -t tmpfs root.rw /mnt/root.rw
  mount -t unionfs -o dirs=/mnt/root.rw=rw:/mnt/root.ro=ro unionfs /mnt/root
  chmod 755 /mnt/root
  mkdir /mnt/root/ro /mnt/root/rw
  mount --move /mnt/root.ro /mnt/root/ro
  mount --move /mnt/root.rw /mnt/root/rw
  rm -f /mnt/root/etc/rcS.d/S07checkroot.sh

  SPLASH="/splash-800x480.gz"
  do_splash

  # Software auto-update
  if [ -x /mnt/root/tsinit ]; then
    /mnt/root/tsinit $XCONSOLE < $CONSOLE > $CONSOLE 2>&1
  fi


) </dev/null >/dev/null 2>&1 

if [ -e /mnt/root/notrootfs -o -e /mnt/root/fastboot -o ! -e /mnt/root/sbin/init ]; then
  if [ -e /mnt/root/fastboot ]; then
    echo ">> File 'fastboot' found. Booting to initrd instead..." > $CONSOLE
  else
    if [ "$bootdev" == "0x0" ]; then
      echo ">> SD Card failed. Booting to initrd..." > $CONSOLE
    else
      echo ">> NAND failed. Booting to initrd..." > $CONSOLE
    fi
  fi

  (
    mount -t tmpfs none /dev/shm
    ifconfig eth0 192.168.0.50
    telnetd -l /bin/sh
    mount -t tmpfs tmpfs /tmp
  ) </dev/null >/dev/null 2>&1

  (
    export ENV=/shinit
    setsid sh -c 'exec sh <$CONSOLE >$CONSOLE 2>&1'
  ) <$CONSOLE >$CONSOLE 2>&1
  wait
fi


if [ -e /mnt/root/sbin/init ]; then
  killall Xorg busybox telnetd >/dev/null 2>&1
  cd /mnt/root
  pivot_root . ./initrd
  ./bin/mount -n --move ./initrd/sys ./sys
  ./bin/mount -n --move ./initrd/proc ./proc
  ./bin/mount -n --move ./initrd/dev/pts ./dev/pts
  exec ./usr/sbin/chroot . ./sbin/init < .$CONSOLE > .$CONSOLE 2>&1
fi
