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

./env_1024_to_512

log2file_value=$(grep "log2file:" "/box/box.ini" | cut -d: -f2)
if [ "$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_keys="edid_index edid_index1 edid_index2 edid_index3"
edid_ch=0
for key in $edid_keys; do
	edid_val=$(grep "^${key}:" /box/box.ini | cut -d: -f2)
	if [ -n "$edid_val" ]; then
		cp /tmp/edid/${edid_val}.* /tmp/edid${edid_ch}.bin
		echo "edid${edid_ch} is index ${edid_val}"
	fi
	edid_ch=$((edid_ch + 1))
done

#POWER
himm 0x11090400 0xff
himm 0x11090010 0

cd /tmp/ko
./load3531dv200 -i

telnetd&

#LED MUX GPIO1_2 GPIO1_3 GPIO0_0 ~ GPIO0_7

himm 0x10ff0068 0x1201
himm 0x10ff006c 0x1201

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

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

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

#POWER
himm 0x11090010 0x0

#RESET
himm 0x102f00a0 0x1200

#RJ45 LED
himm 0x102903c4 0xd04
himm 0x102903c0 0x11011f
himm 0x102903c4 0x17b
himm 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

# 替换 box.ini 中 webrtc.video（只在存在时写）
{
grep -q "webrtc\.video" "/box/box.ini" 2>/dev/null && \
sed -i "s|webrtc\.video|your-webrtc-signaling-server|g" "/box/box.ini" 2>/dev/null || :
# 切换目录
cd /box 2>/dev/null || :
FILE="box_persistent.ini"
DOMAIN="your-webrtc-signaling-server"
# 确保文件存在
    touch "$FILE" 2>/dev/null || :
    # 如果文件非空且最后一行没有换行符，补一个 LF
    [ -s "$FILE" ] && tail -c1 "$FILE" 2>/dev/null | grep -q . && printf '\n' >> "$FILE" 2>/dev/null || :
    # 统一把 Windows 的 CRLF 转成 Unix LF（可选）
    sed -i.bak 's/\r$//' "$FILE" 2>/dev/null && rm -f "$FILE.bak" 2>/dev/null || :
    # 更新或添加配置项
    for group in $(seq 0 7); do
    for server in $(seq 0 3); do
        KEY="g${group}_s${server}_webrtc_p2p_ws_url"
        LINE="$KEY:$DOMAIN"

        # 完全一致不写
        grep -qx "$LINE" "$FILE" 2>/dev/null && continue

        # key 存在但值不同，修改
        if grep -q "^$KEY:" "$FILE" 2>/dev/null; then
            sed -i "s|^$KEY:.*|$LINE|" "$FILE" 2>/dev/null || :
        else
            # key 不存在，追加
            echo "$LINE" >> "$FILE" 2>/dev/null || :
        fi
    done
    done
} 2>/dev/null || :
	
cd /tmp/
./reset &
./box.x4dv200
