#! /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

# Get the subdirs that exist in /mnt
SUBDIRS=$(ls /mnt)
# Put a tmpfs there so we can add new subdirs
mount -t tmpfs none /mnt -o size=16m
# 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 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
mount_astra_storage
mount_bp3lic_storage

# Feed entropy
rngd -r /dev/urandom

# Set sampling rate for ondemand policy to 10ms
echo 10000 > /sys/devices/system/cpu/cpufreq/policy0/ondemand/sampling_rate

# 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

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

if [ "$START_RC" = "y" ]; then
    echo "/sbin/stbhotplug" > /proc/sys/kernel/hotplug

    echo "Configuring private network"
    . /etc/init.d/rcS.net

    BOOT_ROOT=$(cat /proc/cmdline | sed -n 's/.*root=\([a-zA-Z0-9:/-]*\).*/\1/p')
    if [ -f /mnt/apps/etc/init.d/rc.apps ] && [ ! -z "$BOOT_ROOT" ]; then
        echo
        echo "******************************"
        read -t 5 -n 1 -r -p $'Starting user apps services...\n******************************\n' tmp_in
        if [[ $tmp_in =~ [[:cntrl:]] ]]; then
            echo -e "\nBypassing user apps startup\n"
        else
            echo -e "\nContinuing with user apps startup\n"
            export PATH=$PATH.:/usr/local/bin:/usr/local/sbin:/usr/local/usr/bin:/var:/bin:/sbin
            export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib/public:/usr/local/lib/private:/usr/local/lib/gpl:/lib
            export BP3ATA_PATH=/mnt/ata
            export BP3LIC_PATH=/mnt/bp3lic
            /bin/sh /mnt/apps/etc/init.d/rc.apps
        fi
    fi
fi

# Notify READY state to SMC
if [ -f /proc/driver/ba/cmd ]; then
    echo notifyrunstate READY > /proc/driver/ba/cmd
else
    echo "Unable to report READY state to SMC"
fi

# Start Lattice generic script
if [ -e /tmp/lattice_generic.sh ]; then
   echo "******** Starting Lattice generic script *******"
   . /tmp/lattice_generic.sh
fi
