#!/bin/sh

UBI1="/dev/ubi1"
UBI1_0="/dev/ubi1_0"
MTD_ADD7="/dev/mtd7"
AMBARELLA_CONF=ambarella.conf
[ -r /etc/$AMBARELLA_CONF ] && . /etc/$AMBARELLA_CONF

load_dsplog()
{
    if [ -r /lib/modules/"$kernel_ver"/extra/dsplog.ko ]; then
        modprobe dsplog
    fi
}

install_audio()
{
    #Audio
    insmod /lib/modules/4.14.139/kernel/drivers/rtc/rtc-ambarella.ko
    echo "lyric Install Auido..."
    modprobe snd-soc-core pmdown_time=300
    modprobe snd-soc-ambarella
    modprobe snd-soc-ambarella-i2s
    modprobe snd-soc-alc5623
    modprobe snd-soc-ambdummy
    modprobe snd-soc-simple-card 
}

install_ethernet()
{
    #Ethernet
    modprobe libphy
    modprobe micrel
    modprobe ambarella_eth
}

install_usb()
{
    #Install USB module
    modprobe ehci-hcd
    modprobe ehci-ambarella
    modprobe ohci-hcd
    modprobe ambarella_udc
}

gc2053_1="/opt/app/bin/gc2053_aeb_param.bin"
gc2053_2="/opt/app/bin/gc2053_aliso_adj_param.bin"
wifi_setup_sh="/opt/app/bin/wifi_setup.sh"

set_patches()
{
    #SD/MMC
    if [ -e $gc2053_1 ];then
    	mv $gc2053_1 /etc/idsp/adj_params/
	echo "MV some patches gc2053_1 done ..."
    fi

    if [ -e $gc2053_2 ];then
    	mv $gc2053_2 /etc/idsp/adj_params/
	echo "MV some patches gc2053_2 done ..."
    fi

    if [ -e $wifi_setup_sh ];then
    	mv $wifi_setup_sh /usr/local/bin/wifi_setup.sh
	echo "MV some patches wifi_setup_sh done ..."
    fi
    echo "MV some patches ..."
}

install_mmc()
{
    #SD/MMC
    modprobe ambarella_sd
    modprobe mmc_block
}

install_input()
{
    #Install input module
    modprobe gpio_keys
}

misc_init()
{
    #Change USB mode to HOST
    echo host > /proc/ambarella/usbphy0

    #modprobe ambad
}

mount_ubi()
{
    if [ ! -e $MTD_ADD7 ];then
    	echo -e "+++++++++ There is no pba and add partition +++++++++"
        return 0;
    fi
    /usr/sbin/ubiattach /dev/ubi_ctrl -m 7

    if [ ! -e $UBI1 ];then
	echo -e "+++++++++ Must erase mtd 7 first +++++++++"
    	/usr/sbin/flash_eraseall /dev/mtd7
        /usr/sbin/ubiattach /dev/ubi_ctrl -m 7
    fi
    
    if [ ! -e $UBI1_0 ];then
    	/usr/sbin/ubimkvol /dev/ubi1 -N config -s 5000000
    fi
    /bin/mount -t ubifs /dev/ubi1_0 /opt/app/config
}


install_sensor_dsp()
{
    /usr/local/bin/amba_debug -g 99  -d 1
    /usr/local/bin/amba_debug -g 100 -d 1

    ln -s /opt/app/bin/zoneinfo /usr/share/zoneinfo

    #/sbin/modprobe ambcma
    /sbin/modprobe ambcma pm_buf_size=0 warp_buf_size=0

    /usr/local/bin/init.sh --na

    #/sbin/modprobe imx307_mipi 
    /sbin/modprobe gc2053 
    #这里改了
    #/usr/local/bin/test_tuning -a &
    /opt/app/bin/image -i 0 &

    /sbin/modprobe cavalry

    /bin/mknod /dev/cavalry c 248 247

    /usr/local/bin/cavalry_load -f /lib/firmware/cavalry.bin

    /usr/local/bin/test_encode --resource-cfg /opt/app/bin/cv25_vin0_2M_linear_4_streams.lua --hevc-off 0 -f 30 --enc-mode 5
    #/usr/local/bin/test_encode -A -b 0 -h1080p --bitrate 1280000 --bc cbr -N 30 --au-type 0  -e -B -b 1 -H1080p --bitrate 1280000 --bc cbr -N 30 --au-type 0 --gop 5 --fast-seek-intvl 10 --force-pskip-repeat 0 --deblocking-enable 1 --deblocking-alpha -6 --deblocking-beta -6 -e -C -b 2 -H360p --bitrate 512000 --bc cbr -N 30 --au-type 0 --gop 5 --fast-seek-intvl 10 --force-pskip-repeat 0 --deblocking-enable 1 --deblocking-alpha -6 --deblocking-beta -6 -e -D -b 3 -H720p --bitrate 768000  --bc cbr -N 30 --au-type 0  --gop 5 --fast-seek-intvl 10 --force-pskip-repeat 0 --deblocking-enable 1 --deblocking-alpha -6 --deblocking-beta -6 -e 
    /usr/local/bin/test_encode -A -b 0 -h1080p --bitrate 1280000 --bc cbr -N 30 --au-type 0 --mv-statis-enable 1 -e -B -b 1 -H1080p --bitrate 2048000 --bc cbr -N 30 --au-type 0 -e -C -b 2 -H360p --bitrate 512000 --bc cbr -N 30 --au-type 0 -e -D -b 3 -H720p --bitrate 768000  --bc cbr -N 30 --au-type 0 -e 

    chmod -R a+x /opt/app

    #/sbin/modprobe bcmdhd
    #/bin/mount /dev/mmcblk0p1 /sdcard/

    /opt/app/bin/cv25_check_upgrade.sh  ##add for SD upgrade.
    echo CST-8 > /etc/TZ
    export TZ='CST-8'
    sleep 1
    #/opt/app/bin/test_textinsert -C -a 0 -x 16 -y 8 -w 165 -h 24 -s 16 -c 3 --outline 0 --time -D -a 0 -x 32 -y 16 -w 325 -h 48 -s 32 -c 3 --outline 0 --time -A -a 0 -x 48 -y 24 -w 495 -h 72 -s 48 -c 3 --outline 0 --time -B -a 0 -x 48 -y 24 -w 495 -h 72 -s 48 -c 3 --outline 0 --time &
    /opt/app/bin/textinsert -C -a 0 -x 8 -y 0 -w 129 -h 20 -s 12 -c 3 --outline 5 --time -D -a 0 -x 16 -y 0 -w 290 -h 40 -s 24 -c 3 --outline 4 --time -B -a 0 -x 24 -y 0 -w 418 -h 60 -s 36 -c 3 --outline 5 --time -A -a 0 -x 24 -y 0 -w 418 -h 60 -s 36 -c 3 --outline 5 --time &
    cd /opt/app/bin
    export LD_LIBRARY_PATH=/opt/app/lib:$LD_LIBRARY_PATH

    #/usr/local/bin/rtsp_server &
    
    /opt/app/bin/ircut.sh &
    
    #/opt/app/bin/test_ssd_live -b ssd_yuface_fx8_2.5.bin -z MobileNetSSD_fx8_2.5.bin --onet onet_fx16_2.5.bin --facereg mobilefacenet_fx16_data8_2.5.bin --in data --out mbox_loc --out mbox_conf_flatten --width 240 --height 320 --class 2 --background_id 0 --top_k 50 --nms_top_k 100 --nms_threshold 0.45 --pri_threshold 0.7 --priorbox yuface_priorbox_fp32.bin --priorbox_2 MobileNetSSD_priorbox_fp32.bin -i 2 -t 1 &
    /opt/app/bin/cv_alg_va 2>&1 | tee "/var/log/tasscam.log" &
    echo 1 > /proc/sys/vm/panic_on_oom
    sleep 3
    /opt/app/bin/simcam_debug &
    sleep 3
    sh /opt/app/bin/watchdog.sh &
}

start()
{
    #install_ethernet
    #install_input
    set_patches
    install_mmc
    install_audio
    mount_ubi
    install_sensor_dsp
    #install_usb
    #misc_init
    
}

start
