#!/bin/bash

set -e

root=$(cd $(dirname ${0})/..; pwd)
src=${root}/src
tools=${root}/tools
third_party=${root}/third_party

mkdir -p ${third_party}

identifier=flatbuffers
repository=https://github.com/google/${identifier}.git

if [ -d "${third_party}/${identifier}" ]; then
    git -C "${third_party}/${identifier}" fetch -p
    git -C "${third_party}/${identifier}" reset --hard origin/master
else
    echo "Clone ${repository}..."
    git -C "${third_party}" clone --recursive ${repository}
fi

pushd "${third_party}/${identifier}" > /dev/null
cmake -G "Unix Makefiles"
make
popd > /dev/null

identifier=tensorflow
repository=https://github.com/tensorflow/${identifier}.git

if [ -d "${third_party}/${identifier}" ]; then
    git -C "${third_party}/${identifier}" fetch -p
    git -C "${third_party}/${identifier}" reset --hard origin/master
else
    echo "Clone ${repository}..."
    git -C "${third_party}" clone --recursive ${repository}
fi

echo "Generate '../src/tflite.js'"
${third_party}/flatbuffers/flatc --js ${third_party}/tensorflow/tensorflow/contrib/lite/schema/schema.fbs 
mv ./schema_generated.js ${src}/tflite.js
