#!/bin/bash

set -e

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

python=${python:-python}
pip=${pip:-pip}

identifier=keras
repository=https://github.com/keras-team/${identifier}.git

mkdir -p ${third_party}

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 "Install Keras"
export PYTHONUSERBASE=${build}/third_party/pypi/${identifier}
export PATH=${PATH}:${build}/third_party/pypi/${identifier}/bin
rm -rf ${PYTHONUSERBASE}
${pip} install --user tensorflow
${pip} install --user ${third_party}/${identifier}

echo "Update '../src/keras-metadata.json'"
pushd ${tools} > /dev/null
${python} keras-metadata.py
popd > /dev/null
