#! /bin/sh

export PATH=/bin:/sbin:/usr/bin:/usr/sbin:.

echo "Mounting virtual filesystems"
mkdir -p /sys /proc /dev
[ -f /proc/mounts ] || mount -t proc proc /proc
grep -q sysfs /proc/mounts || mount -t sysfs sys /sys
grep -q debugfs /proc/mounts || mount -t debugfs debugfs /sys/kernel/debug 2> /dev/null
grep -q devtmpfs /proc/mounts || mount -t devtmpfs none /dev
mount -t devtmpfs none /dev -o size=16m -o remount
mkdir -p /dev/pts
grep -q devpts /proc/mounts || mount -t devpts devpts /dev/pts 2> /dev/null
grep -q " /tmp " /proc/mounts || mount -t tmpfs -o mode=01777 tmpfs /tmp
mkdir -p /dev/shm
grep -q " /dev/shm " /proc/mounts || mount -t tmpfs -o noexec,nosuid,nodev,mode=1777 shm /dev/shm -o size=16m
grep -q " /var " /proc/mounts || mount -t tmpfs none /var -o size=16m
mkdir -p /var/log # Need this for syslogd
mkdir -p /var/run
mkdir -p /var/tmp

#Get current environment
. /etc/init.d/rcS.util

SUBDIRS=$(ls /mnt) #get the subdirs that exit in /mnt

# Squashfs: Remove the apps dir so that a new directory is not created.  We will create soft link later.
if check_for_squashfs_boot; then
	SUBDIRS=`echo $SUBDIRS | sed 's/apps//g'`
fi

mount -t tmpfs none /mnt -o size=16m # put a tmpfs there so we can add new subdirs
#recreate the old dirs
for DIR in $SUBDIRS
do
    mkdir -p /mnt/$DIR
done

# Create apps soft link
(cd /mnt && rm -rf apps && ln -s /usr/local apps)

# Password file for Linux console access
[ -f /etc/passwd.rg ] && cp /etc/passwd.rg /var/passwd
[ -f /etc/group.rg ] && cp /etc/group.rg /var/group

# copy the default mdev.conf for mdev/hotplug
[ -f /etc/mdev.conf.rg ] && cp /etc/mdev.conf.rg /var/mdev.conf

# Don't let SCHED_FIFO / SCHED_RR realtime threads get preempted
echo -1 > /proc/sys/kernel/sched_rt_runtime_us

# enable panic_on_oom
echo 1 > /proc/sys/vm/panic_on_oom

mount_local_storage

#If BA is to be started, it should run first
if [ "$START_BA" = "y" ]
then
    #Start syslogd as it is used by boot assist
    . /etc/init.d/rcS.ba
fi

#Start wdmd
wd_enable=0
if [ -z $WDMD_OFF ]; then
    get_env_var "WD_ENABLE" wd_enable
fi
if [ $wd_enable -eq 1 ]; then
    mkdir -p /var/run/wdmd
    if [ -f /etc/wdmd_tasks ]; then
        cp /etc/wdmd_tasks /var/run/wdmd
        /bin/wdmd -f /var/run/wdmd/wdmd_tasks
	/etc/init.d/cmon &
    else
        /bin/wdmd -d
    fi
fi

# Executing oopslog.sh
if [ -e /etc/oopslog.sh ]; then
   . /etc/oopslog.sh
fi

if [ "$START_RC" = "y" ]
then
    . /etc/init.d/rcS.rg
fi

# Start rmond
if [[ -f /usr/local/bin/rmond && -n "$FLASH_BOOT" ]]
then
        rmond -D 1 > /dev/console 2>&1 &
fi

# Start btracerunnerd
if [[ -f /usr/local/bin/btracerunnerd && -n "$FLASH_BOOT" ]]
then
        btracerunnerd > /dev/console 2>&1 &
fi

# Executing Lattice Generic Script
if [ -e /tmp/lattice_generic.sh ]; then
   echo "********Run lattice generic script*******"
   . /tmp/lattice_generic.sh
fi
