# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

export PATH=$PATH:$HOME/.cargo/bin

# cargo-optee owns CROSS_COMPILE, crate features, and cargo flags.
unset FEATURES CARGO_FLAGS CROSS_COMPILE_TA CROSS_COMPILE_HOST \
  CROSS_COMPILE32 CROSS_COMPILE64 RUST_TARGET_PATH

# Disable incremental compilation to avoid ICE with recent nightly
# error: internal compiler error: encountered incremental compilation error 
# with lints_that_dont_need_to_run(0-0)
export CARGO_INCREMENTAL=0

if [ -z "$OPTEE_DIR" ]
then
  echo -e "OPTEE_DIR has not set, use $PWD/optee/ as default\n"
  export OPTEE_DIR=$PWD/optee
fi

export OPTEE_OS_DIR="$OPTEE_DIR/optee_os"
export OPTEE_CLIENT_DIR="$OPTEE_DIR/optee_client"

# TARGET_* is the Make/OP-TEE interface. ARCH_TA/ARCH_HOST are shorthands that
# fill TARGET_* when it is unset, or when it still has the value this script
# last derived. An explicit TARGET_TA/TARGET_HOST is left alone. To force a
# fresh shorthand derivation: unset TARGET_TA TARGET_HOST
if [ -z "$TARGET_TA" ] || [ "$TARGET_TA" = "${_TEACLAVE_DERIVED_TARGET_TA:-}" ]
then
  if [ "$ARCH_TA" = "arm" ]
  then
    if [ "$STD" ]
    then
      export TARGET_TA="arm-unknown-optee"
    else
      export TARGET_TA="arm-unknown-linux-gnueabihf"
    fi
  else
    if [ "$STD" ]
    then
      export TARGET_TA="aarch64-unknown-optee"
    else
      export TARGET_TA="aarch64-unknown-linux-gnu"
    fi
  fi
  export _TEACLAVE_DERIVED_TARGET_TA="$TARGET_TA"
fi

case "$TARGET_TA" in
  arm-unknown-*)
    export TA_DEV_KIT_DIR="$OPTEE_OS_DIR/out/arm-plat-vexpress/export-ta_arm32"
    ;;
  *)
    export TA_DEV_KIT_DIR="$OPTEE_OS_DIR/out/arm-plat-vexpress/export-ta_arm64"
    ;;
esac

case "$TARGET_TA" in
  *-unknown-optee)
    echo "set TARGET_TA=$TARGET_TA (std)"
    export __CARGO_TESTS_ONLY_SRC_ROOT="$(pwd)/rust/rust/library"
    ;;
  *)
    echo "set TARGET_TA=$TARGET_TA (no-std)"
    unset __CARGO_TESTS_ONLY_SRC_ROOT
    ;;
esac

# check if libraries exist
if [ -d "$TA_DEV_KIT_DIR" ]
then
  echo "set TA_DEV_KIT_DIR=$TA_DEV_KIT_DIR"
else
  echo -e "Error: TA_DEV_KIT_DIR=$TA_DEV_KIT_DIR does not exist, please set the correct TA_DEV_KIT_DIR or run \"$ ./build_optee_libraries.sh optee/\" then try again\n"
  unset OPTEE_DIR
fi

if [ -z "$TARGET_HOST" ] || [ "$TARGET_HOST" = "${_TEACLAVE_DERIVED_TARGET_HOST:-}" ]
then
  if [ "$ARCH_HOST" = "arm" ]
  then
    export TARGET_HOST="arm-unknown-linux-gnueabihf"
  else
    export TARGET_HOST="aarch64-unknown-linux-gnu"
  fi
  export _TEACLAVE_DERIVED_TARGET_HOST="$TARGET_HOST"
fi

case "$TARGET_HOST" in
  arm-unknown-*)
    export OPTEE_CLIENT_EXPORT="$OPTEE_CLIENT_DIR/export_arm32"
    ;;
  *)
    export OPTEE_CLIENT_EXPORT="$OPTEE_CLIENT_DIR/export_arm64"
    ;;
esac

echo "set TARGET_HOST=$TARGET_HOST"

if [ -d "$OPTEE_CLIENT_EXPORT" ]
then 
  echo "set OPTEE_CLIENT_EXPORT=$OPTEE_CLIENT_EXPORT"
else
  echo -e "Error: OPTEE_CLIENT_EXPORT=$OPTEE_CLIENT_EXPORT does not exist, please set the correct OPTEE_CLIENT_EXPORT or run \"$ ./build_optee_libraries.sh optee/\" then try again\n"
  unset OPTEE_DIR
fi
