#!/bin/bash

###################################################
# Main Buildscript for MLDAP
###################################################

# USE_ENCAPSULATED_TID variable should be defined if you want to send encrypted TID to SKM 
# to prove that it is really TZapp request. SKM must be compiled with this flag too.
export USE_ENCAPSULATED_TID=FALSE

# USE_CRYPTOCORE variable must necessarily to be defined as TRUE(if CryptoCore must be used) or FALSE.
export USE_CRYPTOCORE=FALSE

# Using QSEE SFS if TRUE >< Default use KDF+AES without SFS
export USE_QSEE_WRAP_WITH_SFS=FALSE

# Using Change DRK path and Qsee tz location
export USE_NEW_POS_PATH=FALSE

# From P os, teegris TA uses 8k
export USE_NEW_STACK=FALSE


###################################################
# Start Function
###################################################
function printYellowBack() {
    echo -e "\e[1;43m## $1 ##\e[m"
}

function printYellow() {
    echo -e "\e[1;33m$1\e[m"
}

function printGreen() {
    echo -e "\e[1;32m$1\e[m"
}

function printRed() {
    echo -e "\e[1;31m$1\e[m"
}

function printCyan() {
    echo -e "\e[1;36m$1\e[m"
}

function printBlue() {
    echo -e "\e[1;94m$1\e[m"
}

###################################################
# Start Build Script
###################################################
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)

if [[ "${SCRIPT_DIR}" == *"Utility/MLDAP" ]]; then
    cd $SCRIPT_DIR
else
    printRed "Script path error!"
    printRed "Please check script or path"
    exit
fi

clear
echo ""
printRed    "##########################################################################"
printYellow "                        MAIN Build System for MLDAP                        "
printRed    "##########################################################################"

## Select Debug mode or Release mode
if [ "$1" == "--release" ]; then
    echo "Release mode"
    export NDK_MODE=release
    export MODE=Debug
else
    echo "Debug mode"
    export NDK_MODE=debug
    export MODE=Debug
fi

## Select Main Chipset
echo ""
PS3='Please select your main chipset : '
options=("Exynos" "Snapdragon" "TEEGris" "Quit")

select opt in "${options[@]}"
do
    case $opt in
        "Quit")
            printRed "Your selecttion is [Quit]!!!"
            exit
            ;;
        *)
            CHIPSET="${opt}"
        ;;
    esac

    if [ "${CHIPSET}" == "" ]; then
        echo ""
        printRed "Your selection is error!!!"
        printRed "Please select again !!!"
        echo ""
        exit
    else
        echo ""
        echo ""
        break
    fi
done

## Select ABI
PS3='Please select your ABI : '
options=("32bit" "64bit" "Quit")

select opt in "${options[@]}"
do
    case $opt in
         "32bit")
            export M_ABI="armeabi"
            ;;
         "64bit")
            export M_ABI="arm64-v8a"
            ;;
         "Quit")
            printRed "Your selecttion is [Quit]!!!"
            exit
            ;;
    esac

    if [ "${M_ABI}" == "" ]; then
        echo ""
        printRed "Your selection is error!!!"
        printRed "Please select again !!!"
        echo ""
        exit
    else
        echo ""
        echo ""
        break
    fi
done

PS3='Please select your os : '
options=("First Pos" "Upgrade" "Quit")

select opt in "${options[@]}"
do
    case $opt in
         "First Pos")
            export USE_NEW_POS_PATH=TRUE
            ;;
         "Upgrade")
            export USE_NEW_POS_PATH=FALSE
            ;;
         "Quit")
            printRed "Your selecttion is [Quit]!!!"
            exit
            ;;
    esac

    echo ""
    echo ""
    break
done

## Select Sub Chipset
case "$CHIPSET" in
    Exynos)
        export TZ_PROVIDER=MOBICORE

        PS3='Please select your sub chipset : '
        options=("exynos9810" "exynos9810_400C" "exynos9810_500" "exynos8895_311A" "exynos8895" "exynos8890_tbase311" \
                 "exynos8890_310B" "exynos7885" "exynos7880" "exynos7870" "exynos7580" "exynos7570" \
                 "exynos7420" "Quit")

        select opt in "${options[@]}"
        do
            case $opt in
                "Quit")
                    printRed "Your selecttion is [Quit]!!!"
                    exit
                    ;;
                *)
                    SUB_CHIPSET="${opt}"
                ;;
            esac

            if [ "${SUB_CHIPSET}" == "" ]; then
                printRed "Your selection is error!!!"
                printRed "Please select again !!!"
            else
                break
            fi
        done

        clear
        echo ""
        printRed    "##########################################################################"
        printYellow "                    Build System for MLDAP Exynos                         "
        printRed    "##########################################################################"

        case $SUB_CHIPSET in
            exynos7420)
                export CHIP=EXYNOS7420
                export USE_MC32=true
                export USE_FCDRV=true
                ;;
            exynos7570)
                export CHIP=EXYNOS7570
                export USE_MC310=true
                export USE_FCDRV=true
                ;;
            exynos7580)
                export CHIP=EXYNOS7580
                export USE_MC32=true
                export USE_FCDRV=true
                ;;
            exynos7870)
                export CHIP=EXYNOS7870
                export USE_MC310=true
                export USE_FCDRV=true
                ;;
            exynos7880)
                export CHIP=EXYNOS7880
                export USE_MC310=true
                export USE_FCDRV=true
                ;;
            exynos7885)
                export CHIP=EXYNOS7885
                export USE_MC400=true
                export USE_FCDRV=true
                export USE_TEECLIENT_LIB=true #for model exynos7885 in //PEACE									
                ;;
            exynos8890_310B)
                export CHIP=EXYNOS8890
                export USE_MC310=true
                export USE_FCDRV=true
                ;;
            exynos8890_tbase311)
                export CHIP=EXYNOS8890
                export USE_MC311=true
                export USE_FCDRV=true
                ;;
            exynos8895)
                export CHIP=EXYNOS8895
                export USE_MC400=true
                export USE_FCDRV=true
                export USE_DRKV2=TRUE
                ;;
            exynos9810)
                export CHIP=EXYNOS9810
                export USE_MC400=true
                export USE_FCDRV=true
                export USE_DRKV2=TRUE
                export USE_PREDEFINED_STACK_CHECK=true
                ;;
            exynos9810_400C)
                export CHIP=EXYNOS9810
                export USE_MC400=true
                export USE_FCDRV=true
                export USE_DRKV2=TRUE
                export USE_PREDEFINED_STACK_CHECK=true
                export SUB_CHIPSET=exynos9810
                export USE_TEECLIENT_LIB=true
                ;;
            exynos9810_500)
                export CHIP=EXYNOS9810
                export USE_MC500=true
                export USE_FCDRV=true
                export USE_DRKV2=TRUE
                export USE_PREDEFINED_STACK_CHECK=true
                export SUB_CHIPSET=exynos9810_500
                export USE_TEECLIENT_LIB=true
                ;;
            exynos8895_311A)
                export CHIP=EXYNOS8895
                export USE_MC311=true
                export USE_FCDRV=true
                export USE_DRKV2=true
                ;;
        esac

        # Build TZ App
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building TZ application"
        export SUB_CHIPSET
        source ./build_config.sh
        cd ./swd/kinibi/build
        ./build.sh
        cd - 1>/dev/null
        printYellowBack "End building TZ application"
        echo ""

        # Build Normal world component
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building Normal world component"
        cd ./nwd

        ./build.sh
        cd - 1>/dev/null
        printYellowBack "End building Normal world component"
        echo ""

        printRed "##########################################################################"
        ;;

    Snapdragon)
        export TZ_PROVIDER=QSEE
        ## Select QSEECOM HIDL
        PS3='Please select QSEECOM HIDL supported : '
        options=("Yes" "No" "Quit")
        select opt in "${options[@]}"
        do
            case $opt in
                "Yes")
                    printRed "USE_QSEELIB_HIDL=true"
                    export USE_QSEELIB_HIDL=true
                    ;;
                "No")
                    printRed "USE_QSEELIB_HIDL=false"
                    export USE_QSEELIB_HIDL=false
                    ;;
                "Quit")
                    printRed "Your selecttion is [Quit]!!!"
                    exit
                ;;
            esac
            if [ "${USE_QSEELIB_HIDL}" == "" ]; then
                printRed "Your selection is error!!!"
                printRed "Please select again !!!"
            else
                echo ""
                echo ""
                break
            fi
        done

        PS3='Please select your sub chipset : '
        options=("SM8250" "SM8150" "SM7150" "SM6150" "SDM845" "msm8998" "msm8996" "msm8976pro" "msm8976" "msm8953" \
                 "msm8952" "msm8937" "msm8916" "SDM710" "SDM660" "Quit")

        select opt in "${options[@]}"
        do
            case $opt in
                "Quit")
                    printRed "Your selecttion is [Quit]!!!"
                    exit
                    ;;
                *) 
                    export SUB_CHIPSET="${opt}"
                    echo ""
                    echo ""
                ;;
            esac

            if [ "${SUB_CHIPSET}" == "" ]; then
                printRed "Your selection is error!!!"
                printRed "Please select again !!!"
            else
                break
            fi
        done

        printCyan "Please input your model name of test device "
        export MODEL_NAME=""
        read -e -p "MODEL : " MODEL_NAME
        clear
        echo ""
        printRed    "##########################################################################"
        printYellow "                    Build System for MLDAP Snapdragon                      "
        printRed    "##########################################################################"

        case $SUB_CHIPSET in
            "msm8998")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8998"
                LOCAL_BUILD_ID="RAGAANAA"
                ;;
            "msm8996")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8996"
                LOCAL_BUILD_ID="IADAANAA"
                ;;
            "msm8976pro")
                export REAL_BUILD_CHIPSET="msm8956"
                NHLOS_PATH="msm8976pro"
                LOCAL_BUILD_ID="MAYAANAA"
                ;;
            "msm8976")
                export REAL_BUILD_CHIPSET="msm8956"
                NHLOS_PATH="msm8976"
                LOCAL_BUILD_ID="MAYAANAA"
                ;;
            "msm8953")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8953"
                LOCAL_BUILD_ID="SANAANAA"
                ;;
            "msm8952")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8976"
                LOCAL_BUILD_ID="MAKAANAA"
                ;;
            "msm8937")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8953"
                LOCAL_BUILD_ID="ZALAANAA"
                ;;
            "msm8916")
                export REAL_BUILD_CHIPSET=$SUB_CHIPSET
                NHLOS_PATH="msm8939"
                LOCAL_BUILD_ID="MAVAANAA"
                ;;
            "SDM710")
                export REAL_BUILD_CHIPSET="sdm670"
                NHLOS_PATH="SDM710"
                LOCAL_BUILD_ID="XAWAANAA"
                ;;
            "SDM660")
                export REAL_BUILD_CHIPSET="sdm660"
                NHLOS_PATH="SDM6xx"
                LOCAL_BUILD_ID="KAJAANAA"
                ;;
            "SDM845")
                export REAL_BUILD_CHIPSET="sdm845"
                NHLOS_PATH="SDM845"
                LOCAL_BUILD_ID="WAXAANAA"
                export USE_DRKV2=TRUE
                ;;
            "SM6150")
                export REAL_BUILD_CHIPSET="sm6150"
                NHLOS_PATH="SM6150"
                LOCAL_BUILD_ID="PAZAANAA"
                export USE_DRKV2=TRUE
                ;;
            "SM7150")
                export REAL_BUILD_CHIPSET="sm7150"
                NHLOS_PATH="SM6150"
                LOCAL_BUILD_ID="QAOAANAA"
                export USE_DRKV2=TRUE
                ;;
            "SM8150")
                export REAL_BUILD_CHIPSET="sm8150"
                NHLOS_PATH="SM8150"
                LOCAL_BUILD_ID="YAQAANAA"
                export USE_DRKV2=TRUE
                ;;
            "SM8250")
                export REAL_BUILD_CHIPSET="sm8250"
                NHLOS_PATH="SM8250"
                LOCAL_BUILD_ID="DARAANAA"
                export USE_DRKV2=TRUE
                ;;
        esac

        # Build TZ App
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building TZ application"
        source ./build_config.sh $NHLOS_PATH $LOCAL_BUILD_ID
        cd ./swd/qsee/build
        ./build.sh
        cd - 1>/dev/null

        printYellowBack "End building TZ application"
        echo ""

        # Build Normal world component
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building Normal world component"
        cd ./nwd
        ./build.sh
        cd - 1>/dev/null
        printYellowBack "End building Normal world component"
        echo ""

        printRed "##########################################################################"
        ;;

    TEEGris)
        export TZ_PROVIDER=BLOWFISH

        PS3='Please select your sdk version : '
        options=("V4.0" "V3.0" "V2.0" "Quit")

        select sdk in "${options[@]}"
        do
            case $sdk in
                "Quit")
                    printRed "Your selecttion is [Quit]!!!"
                    exit
                    ;;
                *) 
                    export TG_SDK_VER="${sdk}"
                    ;;
                esac
                break
        done

        # Choose RUNTYPE
        echo ""
        echo ""
        PS3='Please select TA type: '
        options=("ss_bf_ta" "ss_teegris_ta" "Quit")

        select runtype in "${options[@]}"
        do
            case $runtype in
                "Quit")
                    printRed "Your selecttion is [Quit]!!!"
                    exit
                    ;;
                "ss_bf_ta")
                    export MLDAP_UUID="00000000-0000-0000-0000-000000000046"
                    export MLDAP_RUNTYPE="${runtype}"
                    export MLDAP_MODEL="BF_SAMSUNG_TA"
                    break
                    ;;
                "ss_teegris_ta")
                    # Choose UUID
                    echo ""
                    echo ""
                    PS3='Please select MLDAP UUID: '
                    options=("00000000-0000-0000-0000-004d4c444150" "00000000-0000-0000-0000-000000000046" "Quit")

                    select uuid in "${options[@]}"
                    do
                        case $uuid in
                            "Quit")
                                printRed "Your selecttion is [Quit]!!!"
                                exit
                                ;;
                            *)
                                export MLDAP_UUID="${uuid}"
                            ;;
                        esac

                        if [ "${MLDAP_UUID}" == "" ]; then
                            printRed "Your selection is error!!!"
                            printRed "Please select again !!!"
                        else
                            echo ""
                            echo ""
                            ## Select chipset
                            PS3='Please select chipset: '
                            options=("EXYNOS9830" "EXYNOS9820" "EXYNOS9630" "EXYNOS9610" "EXYNOS7904" "Quit")

                            select chip in "${options[@]}"
                            do
                                case $chip in
                                    "Quit")
                                        printRed "Your selecttion is [Quit]!!!"
                                        exit
                                        ;;
                                    "EXYNOS9830")
                                        export MLDAP_MODEL="TEEGRIS_${chip}_SAMSUNG_TA"
                                        export USE_DRKV2=TRUE
                                        export USE_NEW_STACK=TRUE
                                        ;;
                                    "EXYNOS9820")
                                        export MLDAP_MODEL="TEEGRIS_${chip}_SAMSUNG_TA"
                                        export USE_DRKV2=TRUE
                                        export USE_NEW_STACK=TRUE
                                        ;;
                                    "EXYNOS9630")
                                        export MLDAP_MODEL="TEEGRIS_${chip}_SAMSUNG_TA"
                                        export USE_DRKV2=TRUE
                                        export USE_NEW_STACK=TRUE
                                        ;;
                                    "EXYNOS9610")
                                        export MLDAP_MODEL="TEEGRIS_${chip}_SAMSUNG_TA"
                                        export USE_DRKV2=TRUE
                                        export USE_NEW_STACK=TRUE
                                        ;;
                                    "EXYNOS7904")
                                        export MLDAP_MODEL="TEEGRIS_${chip}_SAMSUNG_TA"
                                        export USE_DRKV2=TRUE
                                        export USE_NEW_STACK=TRUE
                                        ;;
                                esac

                                if [ "${chip}" == "" ]; then
                                    printRed "Your selection is error!!!"
                                    printRed "Please select again !!!"
                                else
                                    break
                                fi
                            done
                            break
                        fi
                    done
                    export MLDAP_RUNTYPE="${runtype}"
                    ;;
                esac
                break
        done

        echo ""
        echo ""

        clear
        echo ""
        printRed    "##########################################################################"
        printYellow "                    Build System for MLDAP Blowfish                     "
        printRed    "##########################################################################"

        # Build TZ App
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building TZ application"
        source ./build_config.sh
        cd ./swd/teegris/build
        ./build.sh
        cd - 1>/dev/null

        printYellowBack "End building TZ application"
        echo ""

        # Build Normal world component
        # source ./build_config.sh
        echo ""
        printYellow "-------------------------------------------------------------------------"
        printYellowBack "Start building Normal world component"
        cd ./nwd
        ./build.sh
        cd - 1>/dev/null
        printYellowBack "End building Normal world component"
        echo ""

        printRed "##########################################################################"
        ;;
    *)
        printRed "Don't arrive this porint !!!"
        exit
        ;;
esac

ls -Rfal ./bin
exit
