#!/bin/sh

export LD_LIBRARY_PATH=/tmp/lib:$LD_LIBRARY_PATH:

cd /tmp/

chmod 777 web/images/*
chmod 777 web/css/*
chmod 777 web/js/*
chmod 777 web/*
chmod 777 ko/extdrv/*
chmod 777 ko/*
chmod 777 lib/*
chmod 777 *
chmod 777 dropbear/*

./env_1024_to_512

log2file_value=$(grep "^log2file:" "/box/box.ini" 2>/dev/null | cut -d ":" -f2 | tr -d '[:space:]')
if [ ! -z "$log2file_value" ] && [ "$log2file_value" != "0" ]; then
    LOG_DIR="/tmp/logs"
    CURRENT_LOG="$LOG_DIR/console_output.log"

    mkdir -p "$LOG_DIR"

    exec 1>>"$CURRENT_LOG"
    exec 2>>"$CURRENT_LOG"

    ./log_rotation &
fi

#trtc clog
rm -f /.cache/*.clog

edid_config=$(cat /box/box.ini | grep 'edid_index:')
edid=${edid_config:11:1}
edid_size=`echo "$edid" | wc -c`
if [ "$edid_size" = "2" ]
then
	cp /tmp/edid/$edid.01.* /tmp/edid01.bin
	cp /tmp/edid/$edid.23.* /tmp/edid23.bin
	echo edid is index $edid
fi

CONFIG_FILE="/box/box.ini"

if [ -f "$CONFIG_FILE" ]; then
    samba_enable=$(grep "^samba_enable:" "$CONFIG_FILE" | cut -d ":" -f2 | tr -d '[:space:]')
    if [ ! -z "$samba_enable" ]; then
        if [ "$samba_enable" = "0" ] && [ -e "/tmp/samba" ]; then
            mv /tmp/samba /tmp/samba.1
        fi
    fi
fi

cd /tmp/ko
./load528v100_master -i -sensor0 bt1120

#telnetd&

if [ -x "/tmp/dropbear/run" ]; then
    /tmp/dropbear/run &
fi

#LED MUX GPIO1_2 GPIO1_3 GPIO0_0 ~ GPIO0_3

bspmm 0x10ff0068 0x1201
bspmm 0x10ff006c 0x1201

bspmm 0x102f0000 0x1200
bspmm 0x102f0004 0x1200
bspmm 0x102f0008 0x1200
bspmm 0x102f000c 0x1200
bspmm 0x102f0010 0x1200
bspmm 0x102f0014 0x1200
bspmm 0x102f0018 0x1200
bspmm 0x102f001c 0x1200

#LED DIR
bspmm 0x11091400 0x8c
bspmm 0x11090400 0xff

#LED OFF
bspmm 0x11091030 0x0c
bspmm 0x110903fc 0xff

#POWER
bspmm 0x11090010 0x0

#RESET BUTTON
bspmm 0x102f00a0 0x1200

#RJ45 LED
bspmm 0x102903c4 0xd04
bspmm 0x102903c0 0x11011f
bspmm 0x102903c4 0x17b
bspmm 0x102903c0 0x110110

echo 20480000 > /proc/sys/net/core/wmem_default
echo 40960000 > /proc/sys/net/core/wmem_max

mtu_config=$(cat /box/box.ini | grep 'mtu_size:')
mtu=${mtu_config:9}
mtu_size=`echo "$mtu" | wc -c`
if [ "$mtu_size" = "4" ] || [ "$mtu_size" = "5" ]
then
        ifconfig eth0 mtu $mtu
        echo mtu size is $mtu
fi

ifconfig lo up

eth_s_config=$(cat /box/box.ini | grep 'eth_speed:')
eth_s=${eth_s_config:10}
if [ "$eth_s" = "100" ]
then
	/tmp/ethtool -s eth0 speed 100 duplex full autoneg off
	echo eth speed is 100M now
fi

cd /tmp/
./factory
./reset &
./box.ss528x4
