#!/usr/bin/env bash
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
#
# 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.
# ==============================================================================

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT_DIR="${SCRIPT_DIR}/../../../../../.."
ROOT_DIR=$(cd "${ROOT_DIR}" && pwd)
echo BASH_SOURCE=${BASH_SOURCE[0]} SCRIPT_DIR=${SCRIPT_DIR} ROOT_DIR=${ROOT_DIR}

source "${ROOT_DIR}"/tensorflow/lite/micro/tools/ci_build/helper_functions.sh

# All output files go into ${TEMP_DIR}
TEMP_DIR="$(mktemp -d)"
echo TEMP_DIR=${TEMP_DIR}
cd "$TEMP_DIR"

# setup the python virtual environment
# must use python3 here
readable_run python3 -m venv train.venv
source train.venv/bin/activate

# Run the model training scripts for each example
# Compare the quantized model generated by the script with the repository model
EXAMPLES=( hello_world )

for i in "${EXAMPLES[@]}"
do
  mkdir "${i}"
  cd "${i}"
  EXAMPLE_DIR="${ROOT_DIR}/tensorflow/lite/micro/examples/${i}"
  readable_run python "${EXAMPLE_DIR}/train/train_${i}_model.py"
  cmp model_quant.tflite "${EXAMPLE_DIR}/${i}.tflite"
  cd ..
done
