#
# Copyright (C) 2017 The Android Open Source Project
#
# Licensed 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.

# usage: generate_vts_test.sh <tests>

set -Eeuo pipefail

NNAPI_VERSION="
V1_0
V1_1
V1_2
"

cd "$(dirname $0)"

VTS_PATH=`realpath ../`
function generate_one_testcase {
  # Generate one testcase
  BASENAME=`basename -s .mod.py $1`
  $VTS_PATH/../../tools/test_generator/vts_generator.py ./`basename $1`\
    -m $VTS_PATH/generated/vts_models/$BASENAME.model.cpp \
    -e $VTS_PATH/generated/examples/$BASENAME.example.cpp
}

function generate_wrapper {
  for ver in $NNAPI_VERSION;
  do
    VER_DIR=$VTS_PATH/specs/$ver
    [ ! -d $VER_DIR ] && continue
    pushd $VER_DIR > /dev/null
    OUTFILE=$VTS_PATH/generated/all_generated_${ver}_vts_tests.cpp
    echo "// clang-format off" > $OUTFILE
    echo "// DO NOT EDIT;" >> $OUTFILE
    echo "// Generated by ml/nn/runtime/test/specs/generate_vts_test.sh" >> $OUTFILE
    for f in $@;
    do
      if [ -f $(basename $f) ]; then
        generate_one_testcase $f >> $OUTFILE
        if [ $? -ne 0 ]; then
          echo "Failed processing $f"
          return $?
        fi
      fi
    done
    popd > /dev/null
  done
}

function generate_spec_dirs {
  for ver in $NNAPI_VERSION;
  do
    VER_DIR=$VTS_PATH/specs/$ver
    [ ! -d $VER_DIR ] && continue
    OUTFILE=$VTS_PATH/generated/all_generated_${ver}_vts_tests.cpp
    echo "// clang-format off" > $OUTFILE
    echo "// DO NOT EDIT;" >> $OUTFILE
    echo "// Generated by ml/nn/runtime/test/specs/generate_vts_test.sh" >> $OUTFILE
    $VTS_PATH/../../tools/test_generator/vts_generator.py $VER_DIR \
      -m $VTS_PATH/generated/vts_models -e $VTS_PATH/generated/examples >> $OUTFILE
    echo "Generated file in $VTS_PATH/generated/"`basename $OUTFILE`
  done
}

if [ $# -eq 0 ]; then
  generate_spec_dirs
else
  FILES="$@"
  generate_wrapper $FILES
fi