#!/bin/bash
#
# ufw-init: helper script to be used by ufw itself
#
# Copyright 2008-2015 Canonical Ltd.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3,
#    as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

sub_array[1]="7 0x50"
sub_array[2]="4 0x54"
sub_array[3]="1 0x50"
sub_array[4]="6 0x54"
sub_array[5]="4 0x50"
sub_array[6]="2 0x54"
sub_array[7]="6 0x50"
sub_array[8]="7 0x54"
sub_array[9]="2 0x50"


BOARD_VERSION=`cat /sys/firmware/devicetree/base/model | awk -F ' ' {'print $1'}`

RESET_loader_board()
{
    rkdeveloptool ld | grep "not found" > /dev/null 2>&1

    # Sub board without loader or maskrom state
    if [ "$?" == "0" ]; then
        return 0
    fi

    LocationIDs=`upgrade_tool ld |grep Mode|awk -F ID= '{print $2}' | awk '{print $1}'`
    cat /etc/adb_alias | awk -F usb: '{print $2}' | sed 's/-/./g'

    for sub_usb_Lid in $LocationIDs
    do
        local loader_board=`cat /etc/adb_alias | awk -F usb: '{print $2}' | sed  's/-//g'| sed  's/\.//g'|grep $sub_usb_Lid|awk '{print $2}'`
        # Reset Sub board
        #/usr/bin/bmc $loader_board reset
        sub_num=`echo $loader_board | awk -F sub '{print $2}' | awk '{print int($0)}'`
        sleep 0.1
        {
            echo 1 > /sys/class/leds/RESET_SUB${sub_num}/brightness
            sleep 1.5
            echo 0 > /sys/class/leds/RESET_SUB${sub_num}/brightness
        } &
    done

    wait
}


R2_create_adb_alias()
{
    rm /etc/R2_adb_alias_dynamic -f
    rm /etc/adb_alias

    for i in {1..9};
    do
        sub_i2c_bus=`echo ${sub_array[$i]} | awk '{print $1}'`
        sub_i2c_addr=`echo ${sub_array[$i]} | awk '{print $2}'`
        i2cget -y $sub_i2c_bus $sub_i2c_addr > /dev/null 2&>1

        if [ "$?" == "0" ]; then
            echo find CS-R2-SUB-UE-SATA/sub$i
            input_file=/etc/firefly_adb_alias/CS-R2-SUB-UE-SATA/sub$i

        else
            echo find CS-R2-SUB-UE/sub$i
            input_file=/etc/firefly_adb_alias/CS-R2-SUB-UE/sub$i
        fi

        while read line
        do
            echo $line >> /etc/R2_adb_alias_dynamic
        done < $input_file
    done
    # 删除空行
    sed -i '/^$/d' /etc/R2_adb_alias_dynamic
}





IDENTIFY_VERSION_start()
{
    if [ "$BOARD_VERSION" == "CS-R1-3399JD4-MAIN" ]; then
        ln -fs /etc/CS-R1-3399JD4-MAIN_adb_alias /etc/adb_alias
        RESET_loader_board
    elif [ "$BOARD_VERSION" == "CS-R2-3399JD4-MAIN" ]; then
        #ln -fs /etc/CS-R2-3399JD4-MAIN_adb_alias /etc/adb_alias
        R2_create_adb_alias
        ln -fs /etc/R2_adb_alias_dynamic /etc/adb_alias
        RESET_loader_board
    fi
}

IDENTIFY_VERSION_stop()
{
    if [ "$BOARD_VERSION" == "CS-R1-3399JD4-MAIN" ]; then
        rm -rf /etc/adb_alias
    elif [ "$BOARD_VERSION" == "CS-R2-3399JD4-MAIN" ]; then
        rm -rf /etc/adb_alias
    fi
}

case "$1" in
start)
    IDENTIFY_VERSION_start
    ;;
stop)
    IDENTIFY_VERSION_stop
    ;;
*)
    echo "Usage: /lib/ufw/firefly-sr-control {start|stop|restart|force-reload|force-stop|flush-all|status}"
    exit 1
    ;;
esac
