#!/bin/sh
echo ""
echo "Starting /etc/rc"
PATH=/bin:/sbin:/usr/bin

# Set up the hostname
# /bin/hostname BRECIS

echo "Set up loopback interface"
/sbin/ifattach >/dev/null 2>&1 || echo "  No loopback interface."

# /bin/echo "Set up ethernet interfaces"
# /sbin/ifattach --addr 199.86.15.98 --mask 255.255.255.240 --net 199.86.15.96 --gw 199.86.15.99 eth0
# /sbin/ifattach --addr 192.168.2.88 --mask 255.255.255.0 --net 192.168.2.0 eth1

echo "Expand the ramdisk"
/sbin/expand /ramfs.img /dev/ram0

echo "Mount ramdisk as /var"
/bin/mount -n -t ext2 /dev/ram0 /var

echo "Mount /proc"
/bin/mount -n -t proc proc /proc || echo "  No /proc to mount."

#-----------------------------------------------------------------------------
# NOTE THIS IS POTENTIALLY DANGEROUS!  How well do you know sh?
cat /proc/mtd >/dev/null 2>&1 && \
(echo "/flash stuff" && \
  /bin/mount -n -t jffs2 /dev/mtdblock0 /flash || \
  (eraseall /dev/mtd0 && /bin/mount -n -t jffs2 /dev/mtdblock0 /flash))
cat /proc/mtd >/dev/null 2>&1 && \
  if [ -d /flash/etc ]; then
  # Copy all new files, that are not in /etc/flash.
    for i in /etc/*; do 
      if [ -e $i -a ! -e /flash$i ]; then
	echo "copying $i to /flash$i"
	(cd /flash; tar cf - $i | tar xf -)
      fi
    done
  else
    mkdir /flash/etc
    (cd /flash; tar cf - /etc | tar xf -)
  fi
  # Use following if your mount has long options, else if -b works instead...
cat /proc/mtd >/dev/null 2>&1 && \
  if [ -d /flash/etc ]; then
    mount -n --bind /flash/etc /etc >/dev/null 2>&1 || mount -n -b /flash/etc /etc
  fi
#-----------------------------------------------------------------------------

echo "Create /var/tmp/"
/bin/mkdir /var/tmp
/bin/chmod 777 /var/tmp

echo "Create /var/run/utmp"
/bin/mkdir /var/run
/bin/touch /var/run/utmp
/bin/chmod 777 /var/run /var/run/utmp

# NFS
# echo "Starting portmap" 
# /sbin/portmap &
# echo "Mounting /nfs" 
# /bin/mount -n -t nfs 192.168.1.102:/nfs  /home/m4/nfs &

echo "Enabling IP forwarding"
# /bin/cp /etc/one /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/ip_forward || echo "  /proc not available"

echo "Starting inetd"
/sbin/inetd 2>/dev/null || echo "  No inetd to start"

# run rc.local if it exists ('[', aka test, might not exist, so isn't used)
cat /etc/rc.local >/dev/null 2>&1 && /etc/rc.local

# Completed /etc/rc, let init start agetty.
/bin/date 2>/dev/null || echo ""

# Boot to single user if $? == FALSE, so
/bin/true

exit
# End of file /etc/rc
