#!/bin/bash

echo "Start `pwd`/ : $0"

. ./functions.sh

if [ $# -eq 0 ]
then
    echo "Please choose platform"
    exit -1
fi

check_platform()
{
    print_red_msg " --- Chip $1 defined as target platform --- "
    export TZ_CHIPSET=$1
    print_red_msg "Note: This build configuration will only succeed if you can sign TA locally on your PC! "
    if [[ "${TZ_PLATFORM}" =~ "qsee" ]]; then
        export TZ_PROVIDER=qsee
        #export SIGN_RUNTYPE=qc_secimg30_softsim
        export SIGN_RUNTYPE=qc_secimg40_softsim
        ./build-qsee.sh || { print_red_msg '[SoftSim] QSEE $1 build failed';  exit 1; }
    elif [[ "${TZ_PLATFORM}" =~ "blowfish" ]]; then
        export RUNTYPE=ss_bf_ta
        ./build-bf.sh || { print_red_msg '[SoftSim] Blowfish $1 build failed';  exit 1; }
    elif [[ "${TZ_PLATFORM}" =~ "mobicore" ]]; then
        export RUNTYPE=gd_mobicore32_trustlet
        ./build-mobicore.sh || { print_red_msg '[SoftSim] MobiCore $1 build failed';  exit 1; }
    else
        print_red_msg '[SoftSim] chipset not defined yet...';  exit 1;
    fi
}

clean_platform(){
    print_red_msg " --- Chip $1 defined as target platform --- "
    export TZ_CHIPSET=$1

    print_green_msg "--- Clean intemediate files of TA---"

    if [[ "${TZ_PLATFORM}" =~ "qsee" ]]; then
        #export SIGN_RUNTYPE=qc_secimg30_softsim
        export SIGN_RUNTYPE=qc_secimg40_softsim
        ./build-qsee.sh -c || { print_red_msg "[SoftSim] QSEE ${TZ_CHIPSET} clean failed";  exit 1; }
    elif [[ "${TZ_PLATFORM}" =~ "blowfish" ]]; then
        ./build-bf.sh -c || { print_red_msg "[SoftSim] Blowfish ${TZ_CHIPSET} clean failed";  exit 1; }
    elif [[ "${TZ_PLATFORM}" =~ "mobicore" ]]; then
        ./build-mobicore.sh || { print_red_msg '[SoftSim] MobiCore $1 build failed';  exit 1; }
    else
        print_red_msg '[SoftSim] chipset not defined yet...';  exit 1;
    fi
}

while getopts ":h:p:rc:a" o; do
    case "${o}" in
        p)
            check_platform $OPTARG
            ;;
        c)
            clean_platform $OPTARG
            exit 0;
            ;;

        *)
            echo "not support..."
            ;;
    esac
done

echo "Stop `pwd`/ : $0"
