#!/bin/sh

app_name=dtu_t536

app_flash=/app/"$app_name"
app_flash_bak=/app/"$app_name".bak

env_file=/tmp/env.dat

if [ $? -eq 1 ]; then
    cat /proc/kmsg >> /tmp/kmsg &
fi
stty cols 168

mode=${1:-start}

# lsmod | grep "pps_driver" > /dev/null
# if [ $? != 0 ]
# then    
#     insmod /app/pps_driver.ko
# fi

# lsmod | grep "sc1161y" > /dev/null
# if [ $? != 0 ]
# then    
#     insmod /app/sc1161y.ko
# fi

if [ "$mode" = "stop" ]
then
    echo stop E907....
    echo stop > /sys/class/remoteproc/remoteproc0/state

    PIDS=$(pidof "dtu_t536")
    if [ -n "$PIDS" ]; then
        echo "killing dtu_t536..."
        kill $PIDS
    fi    
fi

if [ "$mode" = "start" ]
then
    # show all kernel log messages
    echo 8 >  /proc/sys/kernel/printk
    
    #
    echo 1 > /proc/sys/net/ipv4/conf/eth0/arp_ignore
    echo 1 > /proc/sys/net/ipv4/conf/eth1/arp_ignore
    echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
    
    #time zone
    export TZ="CST-8"

    #env file
    echo ethact=`printenv ethact` > $env_file
    echo ipaddr=`printenv ipaddr` >> $env_file
    echo netmask=`printenv netmask` >> $env_file
    echo gatewayip=`printenv gatewayip` >> $env_file
    
    echo ver_u_boot=`printenv version` >> $env_file
    echo "ver_kernel= Linux $(uname -r) $(cat /proc/version | grep -oE '[A-Z][a-z]{2} [A-Z][a-z]{2} [0-9]{1,2} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{2,4} [0-9]{4}')" >> $env_file
    echo ver_root_fs=`cat /etc/rootfs-release` >> $env_file
    echo auth=`printenv auth 2>/dev/null` >> $env_file
    echo mt_port_on=`printenv mt_port_on 2>/dev/null` >> $env_file
#    echo mt_port_on=1 >> $env_file
    echo pid=`printenv pid 2>/dev/null` >> $env_file
    echo net1mac=`printenv net1mac 2>/dev/null` >> $env_file
    echo net2mac=`printenv net2mac 2>/dev/null` >> $env_file
    echo net3mac=`printenv net3mac 2>/dev/null` >> $env_file
    echo goose_enable=1 >> $env_file
        
    ifconfig eth1 192.168.64.240
    echo Load $app_flash from flash...

    #check crc
    crc_target -c $app_flash
    if [ $? = 0 ]
    then
        echo CRC Check OK!
        #δ¼ʱ
        echo stop > /sys/class/remoteproc/remoteproc0/state
        echo start E907...
        echo start > /sys/class/remoteproc/remoteproc0/state

        cp -rf $app_flash /tmp
        chmod +x /tmp/$app_name

        # sunxi 20191113 koļ
        if [ ! -e $app_flash_bak ]; then
            cp $app_flash $app_flash_bak
        fi
        cd /tmp
        export LD_LIBRARY_PATH=/usr/xenomai/lib/:/lib:/usr/lib:$LD_LIBRARY_PATH
        /tmp/$app_name >/dev/console 2>&1 &
        #./$app_name 2>&1 | tee /tmp/app_start.log &
    else
        echo CRC Check Error,Stop Load!
# sunxi 20191113 ָkoļ
        if [ -e $app_flash_bak ]; then
            crc_target -c $app_flash_bak
            if [ $? == 0 ]; then
                echo "Ӧóָ, 벻Ҫϵ......"
                cp $app_flash_bak $app_flash
                echo "ָɹ, Զ......"
                sleep 1
                reboot -f
            fi
        fi
    fi
fi