#!/bin/sh

export CONSOLE=/dev/ttyS0
export PATH=/bin:/sbin:/usr/bin:/usr/sbin:/mnt/root/bin:/mnt/root/sbin:/mnt/root

let x=`devmem 0x79000040`
sec=$((x / 100000))
tenths=$((x % 100000 / 1000))
export BOOTTIME=`printf "%d.%02d" $sec $tenths`

mount -t proc proc /proc
mount -t sysfs sysfs /sys
hostname ts4700
setconsole $CONSOLE

(
        mount -t tmpfs tmpfs /lib/modules
        tar -x -z -f /modules.tar.gz -C /
        modprobe scsi_mod
        modprobe sd_mod
        modprobe usb_storage
        modprobe smsc95xx
        umount /lib/modules
) &

(
  ts4700ctl --greenledoff --getrtc --resetswitchon --autofeed 2
  ifconfig lo up
  udhcpc -T 5 -A 0 -t 15 -n
  if [ $? -eq 1 ]; then
	ts4700ctl --reboot
  fi
  mount -t tmpfs tmpfs /tmp
  mkdir /mnt2
  mount -o nolock,vers=2,rsize=1024,wsize=1024 192.168.0.11:/u/x /mnt2
  mount -o bind /tmp /mnt2/tmp
  export PRIMORDIAL_VERSION=1
  chroot /mnt2 /usr/local/bin/ts4700init >$CONSOLE 2>&1
  umount /mnt2/tmp
  umount /mnt2
  killall udhcpc
  ifconfig eth0 down
) </dev/null >/dev/null 2>&1 &

(
  export ENV=/shinit
  stty ospeed 115200 >/dev/null 2>&1
  exec setsid cttyhack /bin/sh -i
) <$CONSOLE >$CONSOLE 2>&1
wait

ts7500ctl --reboot
