#!/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 lib/*
chmod 777 *
chmod 777 samba/bin/*
chmod 777 /tmp/sys/bin/*
chmod 777 dropbear/*

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

/usr/local/bin/amba_debug -g 49 -d 0x0 # led

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.* /tmp/edid.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

mount -t debugfs debugfs /debug
/usr/local/bin/amba_debug -g 66 -d 0x0 # it6661 & it66021 reset

/usr/local/bin/init.sh --yuv1080p30
/usr/local/bin/test_mempart -m 2 -s 0x4000000
modprobe alpu
modprobe rtc-ambarella

/usr/local/bin/amba_debug -g 66 -d 0x1 # # it6661 & it66021 set
modprobe it66021
insmod /tmp/sys/modules/5.4.148/extra/oled64.ko
insmod /tmp/sys/modules/5.4.148/extra/icp204.ko

/usr/local/bin/amba_debug -g 41 -d 0x0 # audio
/usr/local/bin/amba_debug -g 5 -d 0x1  # hdmi audio
#/usr/local/bin/amba_debug -g 47 -d 0x0 # red led
/usr/local/bin/amba_debug -g 52 -d 0x1 # 3.3v wifi&hdmi out

#modprobe bcmdhd

telnetd&
#killall telnetd

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

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

# Record Button
echo 51 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio51/direction

# Reset Button
echo 46 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio46/direction

# HDMI HPD
echo 64 > /sys/class/gpio/export
echo in > /sys/class/gpio/gpio64/direction


ifconfig lo up
/tmp/samba/bin/smbd -D &

#sdi loopout need the dely;otherwise not find wifi
sleep 2s

cd /tmp/
./factory
./reset &
./box.s6l55m -w 1 &

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