#!/bin/bash

###################################################
# Start Function
###################################################
SDK_BASIC_FOLDER="../../../../TZSDK/android-security-exynos"

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
###################################################
# Get script parameters
clear
echo ""
printRed    "##########################################################################"
printYellow "                      Build System for HDCP Exynos                        "
printRed    "##########################################################################"

PRODUCT_TRUSTZONE_TYPE=$1
PLATFORM=$2
DEBUG_MODE=$3

# Set according to build parameter
case "$PRODUCT_TRUSTZONE_TYPE" in
    # model : Star(G960)
    exynos9810)
        export CHIP=EXYNOS9810
        export USE_MC400=true
        export USE_4KMIRRORING=true
        export USE_64BIT_ADDR=true
        export USE_WFD_TS_MUX_HW=true
        export USE_PREDEFINED_STACK_CHECK=true
        ;;
    # model : Star(G960) P OS temp
    exynos9810_400C)
        export CHIP=EXYNOS9810
        export USE_MC400=true
        export USE_4KMIRRORING=true
        export USE_64BIT_ADDR=true
        export USE_WFD_TS_MUX_HW=true
        export USE_PREDEFINED_STACK_CHECK=true
        export USE_TEECLIENT_LIB=true
        export PRODUCT_TRUSTZONE_TYPE=exynos9810
        ;;
    # model : Star(G960) P OS temp
    exynos9810_500)
        export CHIP=EXYNOS9810
        export USE_MC500=true
        export USE_4KMIRRORING=true
        export USE_64BIT_ADDR=true
        export USE_WFD_TS_MUX_HW=true
        export USE_PREDEFINED_STACK_CHECK=true
        export USE_TEECLIENT_LIB=true
        export PRODUCT_TRUSTZONE_TYPE=exynos9810_500
        ;;
    # model : Deam(G950), Great(N950)
    exynos8895)
        export CHIP=EXYNOS8895
        export USE_MC400=true
        export USE_4KMIRRORING=true
        ;;
    # model : Grace(N930)
    exynos8890)
        export CHIP=EXYNOS8890
        export USE_HDCP2FLAG=true
        export USE_MC311=true
        ;;
    # model : Hero(G930)
    exynos8890_310B)
        export CHIP=EXYNOS8890
        export USE_HDCP2FLAG=true
        export USE_MC310=true
        ;;
    # model : Zero(G920), Noble(N920)
    exynos7420)
        export CHIP=EXYNOS7420
        export USE_HDCP2FLAG=true
        export USE_MC32=true
        #In exynos7xxx series, there is only one library file is exist without reference to tbase version
        export USE_SECMEM_IOC_CHUNK=true
        ;;
    # model : POS
    exynos7885_static)
        export CHIP=EXYNOS7885
        export USE_MC400=true
        export USE_HDCP2FLAG=true
        export USE_64BIT_ADDR=true
        export PRODUCT_TRUSTZONE_TYPE=exynos7885
        export USE_STAIC_MCCLIENT_LIB=true
        export USE_PREDEFINED_STACK_CHECK=true
        ;;
    # model : J737
    exynos7885)
        export CHIP=EXYNOS7885
        export USE_HDCP2FLAG=true
        export USE_MC400=true
        export USE_64BIT_ADDR=true
        ;;
    # model : A520, A720
    exynos7880)
        export CHIP=EXYNOS7880
        export USE_HDCP2FLAG=true
        export USE_MC310=true
        #In exynos7xxx series, there is only one library file is exist without reference to tbase version
        export USE_SECMEM_IOC_CHUNK=true
        ;;
    # model : A320
    exynos7870)
        export CHIP=EXYNOS7870
        export USE_HDCP2FLAG=true
        export USE_MC310=true
        #In exynos7xxx series, there is only one library file is exist without reference to tbase version
        export USE_SECMEM_IOC_CHUNK=true
        ;;
    exynos7580)
        export CHIP=EXYNOS7580
        export USE_HDCP2FLAG=true
        export USE_MC32=true
        #In exynos7xxx series, there is only one library file is exist without reference to tbase version
        export USE_SECMEM_IOC_CHUNK=true
        ;;
    exynos5433)
        export CHIP=EXYNOS5433
        export CONFIG_KERNEL_ARCH32=true
        export USE_HDCP2FLAG=true
        export USE_MC32=true
        export USE_SECMEM_IOC_CHUNK=true
        export WITHOUT_KEYMANDRV=true
        ;;
    exynos5430_tbase302)
        export CHIP=EXYNOS5430
        export CONFIG_KERNEL_ARCH32=true
        export USE_HDCP2FLAG=true
        export USE_MC32=true
        export USE_SECMEM_IOC_CHUNK=true
        export WITHOUT_KEYMANDRV=true
        ;;
    exynos3475)
        export CHIP=EXYNOS3475
        export USE_HDCP2FLAG=true
        export USE_MC32=true
        export USE_SECMEM_IOC_CHUNK=true
        ;;
    *)
        usage
        exit
        ;;
esac

echo ""
TITLE="Build for ${PRODUCT_TRUSTZONE_TYPE}"
printYellow     "-------------------------------------------------------------------------"
printYellowBack "${TITLE}"
echo ""

if [ "$PLATFORM" = "--debug" -o "$DEBUG_MODE" = "--debug" ]; then
    printRed "Enable debug option for build"
    export DEBUG=true
else
    printRed "Disable debug option for build"
    export DEBUG=false
fi

# Export global parameters
export PRODUCT_TRUSTZONE_TYPE
export USE_MOBICORE=true
export LANG=C

echo ""
printYellow "Cleaning outputs of pre-build"
rm -rf ../swd/kinibi/Out
rm -rf ../nwd/Out
rm -rf ../bin
mkdir ../bin

########################################################################################
# Build TZ Application
echo ""
printYellow "-------------------------------------------------------------------------"
printYellowBack "Start building TZ application"
cd ../swd/kinibi/build
rm -rf ../../Out/debug
rm -rf ../../Out/Release
./build.sh
cd - > /dev/null
printYellowBack "End building TZ application"
echo ""

# Build Normal world component
echo ""
printYellow "-------------------------------------------------------------------------"
printYellowBack "Start building Normal world component"
cd ../nwd
rm -rf Out
if [ "$PLATFORM" = "tizen" ]; then
    printGreen "This build for Tizen Platform"
    make -f Tizen.mk
else
    printGreen "This build for Android Platform"

    if [ "${SDK_BASIC_FOLDER}" != "" ]; then
        export SDK_PATH_ROOT="${SDK_BASIC_FOLDER}/tbase/${PRODUCT_TRUSTZONE_TYPE}"
    else
        cd ../../
        TEMP_PATH=$PWD
        cd - > /dev/null

        COMP_PATH_1="/tbase/${PRODUCT_TRUSTZONE_TYPE}"
        COMP_PATH_2=`echo "${TEMP_PATH}" | rev | cut -c1-${#COMP_PATH_1} | rev`

        if [ "${COMP_PATH_1}" == "${COMP_PATH_2}" ]; then
            export SDK_PATH_ROOT=$TEMP_PATH
        else
            printGreen "Finding Trust OS SDK folder..."
            if [ "${SDK_FIND_BASIC_FOLDER}" == "" ]; then
                SDK_FIND_BASIC_FOLDER="/"
            fi

            SDK_PATH_ROOT=`find "${SDK_FIND_BASIC_FOLDER}" -name "AndroidIntegration" -type d 2>/dev/null | egrep -e "/tbase/${PRODUCT_TRUSTZONE_TYPE}/" | sed "s/\/AndroidIntegration//"`

            if [ "${SDK_PATH_ROOT}" == "" ]; then
                printRed "There is no SDK, so stop build !!!"
                exit
            fi

            set $SDK_PATH_ROOT
            SDK_PATH_NUM=$#
            if [ $((SDK_PATH_NUM)) -gt 1 ] ; then
                echo ""
                printGreen "There are many SDK!!!"
                PS3='Please select your right SDK path : '
                options=($SDK_PATH_ROOT "Quit")
                select opt in "${options[@]}"
                do
                    case $opt in
                        "Quit")
                            printRed "Your selecttion is [Quit]!!!"
                            printRed "Stop TZ App build!!!"
                            exit
                            ;;
                        *) SDK_PATH_ROOT="${opt}"
                        ;;
                    esac

                    if [ "${SDK_PATH_ROOT}" == "" ]; then
                        printRed "Your selection is error!!!"
                        printRed "Please select again !!!"
                    else
                        break
                    fi
                done
            fi

            export SDK_PATH_ROOT
        fi
    fi

    printRed "    SDK PATH : $SDK_PATH_ROOT"
    echo ""

    if [ -z $COMP_PATH_ROOT ]; then
        printCyan "The build environment is not set!"
        printCyan "Trying to source setupDrivers.sh automatically!"
        source $SDK_PATH_ROOT/setupDrivers.sh || exit 1
        echo ""
    fi

    export ROOT_PATH=$(dirname $(readlink -f $BASH_SOURCE))

    # Android NDK path
    #if [ "${USE_STAIC_MCCLIENT_LIB}" == "true" ]; then
    NDK_BUILD="${ROOT_PATH}/../../../../TZSDK/ndk/android-ndk-r13/ndk-build APP_ABI=armeabi APP_PLATFORM=android-24"
    #else
    #NDK_BUILD=${COMP_PATH_AndroidNdkLinux}/ndk-build
    #fi
    PLATFORM=Generic

    # Needs to change due to limitations in NDK build
    export COMP_PATH_MobiCoreDriverLib=./Samsung_LSI/NormalWorldComponents/MobiCoreDriverLib/Out
    export COMP_PATH_LIB_HDCP2=$ROOT_PATH/libhdcp2/Out
    export HDCP2_ROOT=$ROOT_PATH/..

    echo -e "\e[1;94m    [NDK_BUILD Option]\e[m"
    echo -e "\e[1;94m       ${NDK_BUILD}\e[m"
    echo -e "\e[1;94m       -B\e[m"
    echo -e "\e[1;94m       NDK_DEBUG=1\e[m"
    echo -e "\e[1;94m       NDK_PROJECT_PATH=$ROOT_PATH/libhdcp2/kinibi\e[m"
    echo -e "\e[1;94m       NDK_APPLICATION_MK=$ROOT_PATH/libhdcp2/kinibi/Application.mk\e[m"
    echo -e "\e[1;94m       NDK_MODULE_PATH=$SDK_PATH_ROOT\e[m"
    echo -e "\e[1;94m       NDK_APP_OUT=$ROOT_PATH/Out\e[m"
    echo -e "\e[1;94m       APP_BUILD_SCRIPT=$ROOT_PATH/libhdcp2/kinibi/Android.mk\e[m"
    echo -e "\e[1;94m       APP_OPTIM=$MODE\e[m"
    echo ""

    # run NDK build
    ${NDK_BUILD} \
        -B \
        NDK_DEBUG=1 \
        NDK_PROJECT_PATH=$ROOT_PATH/libhdcp2/kinibi \
        NDK_APPLICATION_MK=$ROOT_PATH/libhdcp2/kinibi/Application.mk \
        NDK_MODULE_PATH=$SDK_PATH_ROOT \
        NDK_APP_OUT=$ROOT_PATH/Out \
        APP_BUILD_SCRIPT=$ROOT_PATH/libhdcp2/kinibi/Android.mk \
        APP_OPTIM=$MODE
fi
cd ..
printYellowBack "End building Normal world component"
echo ""

printBlue "-------------------------------------------------------------------------"
printBlue "Strip for library (libhdcp2.so)"
./strip nwd/Out/local/armeabi/libhdcp2.so

printBlue "Strip for library (libstagefright_hdcp.so)"
./strip nwd/Out/local/armeabi/libstagefright_hdcp.so
printBlue "-------------------------------------------------------------------------"
echo ""

printCyan "-------------------------------------------------------------------------"
printCyan "[RESULT]"
printCyan "    Binary List for   : $PRODUCT_TRUSTZONE_TYPE"
printCyan "    Debug             : $DEBUG"
echo ""
printCyan "-------------------------------------------------------------------------"

if [ "${DEBUG}" == "true" ]; then
    cp -f swd/kinibi/Out/Bin/Debug/ffffffff000000000000000000000005.tlbin bin
else
    cp -f swd/kinibi/Out/Bin/Release/ffffffff000000000000000000000005.tlbin bin
fi

rm -rfv swd/kinibi/Out
cp -f nwd/Out/local/armeabi/libhdcp2.so bin

cp -f nwd/Out/local/armeabi/libstagefright_hdcp.so bin
rm -rfv nwd/Out
rm -rfv nwd/libhdcp2/kinibi/libs

echo ""
ls -al bin

printRed "##########################################################################"
echo ""
echo ""
