#!/bin/bash
set -e
set -o pipefail
export CROSS_COMPILE=`pwd`/../gcc-arm-none-eabi-10.3-2021.10/bin/arm-none-eabi-
export LANG=C

_no_clean=0
_loglevel=1
_secure="SECURE"
_default_b="LSI=V4H FORCE_115200=1 EMMC_TRANS_MODE=HS400 \
OPTEE_LOAD_ENABLE=1 \
BL2_LOAD_ENABLE=1 \
QNX_OS_LOAD_ENABLE=1 \
"
_echo="@"
while getopts "sl:vcph" opt; do
  case $opt in
  s) _secure="NORMAL"
    ;;
  l) _loglevel=$OPTARG
    ;;
  p) _build_param="${_build_param} MEASURE_TIME=1"
    ;;
  v) _echo=""
    ;;
  c) _no_clean=1
    ;;
  h)
    echo "usage: $0 [option]"
    echo "  -s                 toggle boot_mode (default: $_secure)"
    echo "  -l                 set loglevel (default: $_loglevel)"
    echo "  -p                 enable MEASURE_TIME"
    echo ""
    echo "  -c                 do not clean  before build"
    echo "  -v                 build verbosely"
    exit 0
  esac
done
shift $((OPTIND-1))

if [ $_no_clean -ne 1 ]; then
    make clean
fi
BUILD_PARAM="${BUILD_PARAM} ${_build_param} LOG_LEVEL=$_loglevel"

make ${_default_b} ${BUILD_PARAM} BOOT_MODE=$_secure V=$_echo $*
