#!/bin/sh
### Copyright 2010 b-rad.cc
### http://wdlxtv.com & http://b-rad.cc & http://nextdimension.cc
### GPLv3 - full license located @ /usr/share/LICENSE
### Stipulations:
### - the full copyright notices in /osd/setup_about.xml must be left intact, as it is the publicly displayed copyright portion of this software as dictated by the GPLv3
### - this entire header must be left intact

. ./sysconfig 

echo "===================ROOTFS==================================="
cat ./sysconfig
echo "============================================================"

PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
mount -t proc none /proc
mount -t sysfs sysfs /sys
mount -t tmpfs mdev /dev
mdev -s
mount -t tmpfs none /tmp
config_tool -l
eval `egrep 'ROOT_UUID|ROOT_TAG|ROOT_NETBOOT|ROOT_NB' /conf/config`

mount -t usbfs usb /proc/bus/usb
chmod 777 /tmp
mkdir -p /tmp/.root/etc
chmod 666 /dev/null

mkdir -p /tmp/.root/lib/modules/2.6.22.19-19-4
cd /lib/modules/2.6.22.19-19-4-orig/
for i in `find .` ; do
    bn=`basename "$i"`
    if [ -f $i -a "${bn#*.}" == "ko" ] ; then
        echo $bn
        mkdir -p /lib/modules/2.6.22.19-19-4/`dirname $i`
        ln -sf /lib/modules/2.6.22.19-19-4-orig/$i /lib/modules/2.6.22.19-19-4/`dirname $i`
    fi
done
cd /
depmod
touch /etc/blkid.tab
mkdir -p /tmp/.root/lib/firmware 
cp -r -s -f /lib/firmware-orig/* /lib/firmware
ifconfig lo 127.0.0.1
. /etc/init.d/host-config

### test for ext3-boot on /dev/sda1 ###
mountpoint -q /boot/mnt
if [ $? -ne 0 -a "$1" != "upgrade" -a "$1" != "rootFS" ] ; then
    echo "Searching for root.bin or active filesystem..."
    ROOTDEV=/dev/sda1
    NETBOOT=0
    if [ "$ROOT_NB" != "0" -a -n "`echo $ROOT_NETBOOT | awk -F'\.' ' $0 ~ /^([0-9]{1,3}\.){3}[0-9]{1,3}:.*$/ && $1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 '`" ] ; then
        /etc/init.d/S01ipup forcestart && /etc/init.d/S10portmap forcestart && mount -v $ROOT_NETBOOT /mnt -o soft,intr
        if [ -x /mnt/.rootFS/init -o -x /mnt/.rootFS-$ROOT_TAG/init -o -f /mnt/root.bin -o -f /mnt/root.bin-$ROOT_TAG ] ; then
            NETBOOT=NETBOOT
            echo "NETBOOTING $ROOT_NETBOOT!"
        else
            umount /mnt
        fi
    fi
    if [ "$NETBOOT" == "0" ] ; then
        if [ -n "$ROOT_UUID" ] ; then
            ROOTDEV=`blkid | grep $ROOT_UUID | cut -d':' -f1`
            [ -z "$ROOTDEV" ] && ROOTDEV=/dev/sda1 || "ROOT_UUID: $ROOT_UUID"
        fi
        mount -v $ROOTDEV /mnt -o nodiratime,noatime,sync
        if [ -f /mnt/root.bin -o -f /mnt/root.bin-$ROOT_TAG -o -x /mnt/.rootFS/init -o -x /mnt/.rootFS-$ROOT_TAG/init ] ; then
            export NAND_VERSION=$FW_VERSION
            umount /mnt
            echo "Checking disk $ROOTDEV..."
            fsck -y $ROOTDEV
            mount -v $ROOTDEV /mnt -o nodiratime,noatime,sync
        fi
    fi

    if [ -f /mnt/root.bin -o -f /mnt/root.bin-$ROOT_TAG ] ; then
        [ -f /mnt/root.bin-$ROOT_TAG ] && ROOTFS=root.bin-$ROOT_TAG || ROOTFS=root.bin
        echo "Mounting root.bin image: $ROOTFS"
        mount -o loop,nodiratime,noatime,rw /mnt/$ROOTFS /boot 
        umount /proc/bus/usb
        umount /proc
        umount /sys
        umount /dev 2>/dev/null || umount -l /dev
        umount /tmp
        pivot_root /boot /boot/boot
        cd /
        exec /init
    elif [ -x /mnt/.rootFS/init -o -x /mnt/.rootFS-$ROOT_TAG/init ]; then
        [ -x /mnt/.rootFS-$ROOT_TAG/init ] && ROOTFS=.rootFS-$ROOT_TAG || ROOTFS=.rootFS
        echo "Active filesystem ($ROOTFS) detected."
        mount -o move /mnt /boot
        umount /proc/bus/usb
        umount /proc
        umount /sys
        umount /dev 2>/dev/null || umount -l /dev
        umount /tmp
        PATH=/$ROOTFS/bin:/$ROOTFS/sbin:/$ROOTFS/usr/bin:/$ROOTFS/usr/sbin:$PATH
        cd /boot
        ln -s $ROOTFS/lib
        pivot_root /boot/ /boot/$ROOTFS/boot
        cd /
        exec busybox chroot /$ROOTFS /init rootFS $NETBOOT
    else
        echo "Not found, umounting and continuing..."
        umount /mnt 2>/dev/null
    fi
fi
#########################################

syslogd -O /tmp/messages.txt
klogd
VER=`cat /conf_src/version`
logger "${VER}"

mountpoint -q /boot/mnt
if [ $? == 0 ]; then
    # move around some mountpoints...
    busybox mount -o move /boot/mnt /mnt/boot
    busybox mount -o move /boot /mnt/fwup
    busybox umount /mnt/fwup/tmp/media/usb/* 2>/dev/null
    busybox umount /mnt/fwup
    busybox mount -o move /mnt/boot /boot
    busybox mount -o remount,noatime,nodiratime,rw /
    busybox logger -t init "root.bin detected"
elif `mountpoint -q /boot` ; then
    busybox umount /boot 2>/dev/null || umount -l /boot
    if [ "$2" != "NETBOOT" ] ; then
        busybox mount /dev/sda1 /boot
    else
        busybox mount $ROOT_NETBOOT /boot -o soft,intr
        touch /tmp/NETBOOT
    fi
    unlink /boot/lib
    busybox logger -t init "active filesystem / .rootFS detected"
fi

cp /etc/mdev.conf /tmp/.root/etc/mdev.conf
busybox mount -o bind /tmp/.root/etc/mdev.conf /etc/mdev.conf
cp /etc/blkid.tab.orig /etc/blkid.tab

echo /sbin/mdev > /proc/sys/kernel/hotplug
mkdir /dev/pts
busybox mount -t devpts devpts /dev/pts
ln -s /proc/self/fd /dev/fd
mkdir /dev/input
MINOR=0
while [ $MINOR -lt 17 ]; do
    busybox mknod /dev/input/event$MINOR c 13 `expr $MINOR + 64`;
    MINOR=`busybox expr $MINOR + 1`;
done

ln -s $SYSCONF_BOOTLOADER_MTD_PARTITION /dev/mtdblock_bootloader
ln -s $SYSCONF_KERNEL_MTD_PARTITION /dev/mtdblock_kernel
ln -s $SYSCONF_FIRMWARE_MTD_PARTITION /dev/mtdblock_romfs

mkdir -p /tmp/.root/var/cache /tmp/.root/var/log /tmp/.root/var/run /tmp/.root/var/utmp /tmp/.root/var/lock /tmp/.root/var/pcmcia /tmp/.root/var/spool /tmp/.root/var/tmp /tmp/.root/var/lib/nfs /tmp/.root/var/lib/pcmcia /tmp/.root/var/lib/php5
chmod -R 777 /tmp/.root/var/*

cp /etc/securetty.ori /tmp
/sbin/ldconfig -C /tmp/ld.so.cache

if [ "$1" == "upgrade" ]; then
    touch /tmp/STOP_DMARENDER
    touch /tmp/STOP_PICRENDER
fi

if [ "x$1" != "xf1f1" ]; then
    if [ x$SYSCONF_LAST_PARTITION_NODE_NAME != "x" ]; then
        dd if=$SYSCONF_LAST_PARTITION_NODE_NAME of=/tmp/lastblock bs=1024
        mkdir $SYSCONF_STATIC_CONFIG_MOUNT_POINT
        mount -t minix -o loop /tmp/lastblock $SYSCONF_STATIC_CONFIG_MOUNT_POINT
    fi
fi


export EM8XXX_SERVER=":0"

mknod /dev/mum0 c 126 0
mknod /dev/em8xxx0 c 127 0
mknod /dev/mtdblock3 b 254 6
mknod /dev/mtdblock2 b 254 3
mknod /dev/mtdblock0 b 254 0
mknod /dev/sda1 b 8 1

for lonum in `seq 8 31` ; do
    mknod /dev/loop$lonum b 7 $lonum 2>/dev/null
done

unload_imat.sh;
xlu_unload_t3.sh -i -u;

ln -s /dev/ir /dev/irda

if [ -f /etc/directfbrc ]; then
    cp /etc/directfbrc /tmp/directfbrc
fi

if [ -f /etc/sigmadfbrc ]; then
    cp /etc/sigmadfbrc /tmp/sigmadfbrc
fi

if [ -f /usr/sbin/directfbrc_builder ]; then
    /usr/sbin/directfbrc_builder
fi

if [ -d /etc/stagecraft-data ]; then
    mkdir -p /tmp/stagecraft-data/shared-objects
fi

[ -f /bin/set-nls-codepage ] && . /bin/set-nls-codepage
mkdir -p /tmp/media/usb /tmp/export.adds /tmp/sheetQueue
[ -n "$NAND_VERSION" -a "$FW_VERSION" != "$NAND_VERSION" ] && echo '127.0.0.1 releases.wdlxtv.com' >> /etc/hosts && busybox logger -t init 'cross device firmware loading detected!'

if [ "$1" == "f1f1" ]; then
    echo "enter f1 test ..."
    /bin/sh
fi

if [ "$1" == "sisi" ]; then
    echo "enter si test ..."
    mount /dev/sda /tmp/media/usb
    if [ $? != 0 ]; then
        echo "Mounting /dev/sda fail, try to mount /dev/sda1 to /tmp/media/usb"
        mount /dev/sda1 /tmp/media/usb
        if [ $? != 0 ]; then
            echo "Mounting /dev/sda1 still fail, abort!!!"
            /bin/sh
        fi
    fi
    /tmp/media/usb/runsit.sh
    /bin/sh
fi

lsusb | logger &
mkdir -p /tmp/media/usb

[ "$FW_VERSION" != "WDLXTV_G2" ] && modprobe cifs CIFSMaxBufSize=64512
[ -f /bin/crazymount ] && . /bin/crazymount BOOT

# bring up network devices
[ -e /conf/net.config ] && dos2unix /conf/net.config && . /conf/net.config
[ -e /tmp/net.config ] && . /tmp/net.config

[ ! -f "/conf/shadow" ] && cp /etc/shadow.orig /conf/shadow && chmod 600 /conf/shadow
[ ! -f "/conf/passwd" ] && cp /etc/passwd.orig /conf/passwd
[ ! -f "/conf/group" ] && cp /etc/group.orig /conf/group
chmod 755 /tmp/conf
[ -f /conf_src/devcerttemplate.dat ] && cp /conf_src/devcerttemplate.dat /tmp -f

if [ -f /tmp/emergency.flash ]; then
        killall config_tool
        /sbin/config_tool -l &
        touch /tmp/STOP_DMARENDER
        touch /tmp/STOP_PICRENDER
        sh /etc/init.d/rcS `cat /tmp/emergency.flash` &
#elif [ "$1" == "upgrade" ]; then  # debug
#       sh /etc/init.d/rcS &
else
    [ -f /etc/init.d/S70dmaosd ] && ( sh /etc/init.d/rcS $1 $2 $3 $4 $5 $6 & ) || ( run_all $1 $2 $3 $4 $5 $6 & )
fi

while [ 1 ]; do 
    /bin/sh ;
done

