#!/bin/bash

set -e
pushd $(cd $(dirname ${0})/..; pwd) > /dev/null

case "${OSTYPE}" in
    msys*) python="winpty python";;
    *) python=python3;;
esac

bold() {
    echo "$(tty -s && tput bold)$1$(tty -s && tput sgr0)" 
}

venv() {
    env_dir=./third_party/env/tensorflow
    [ -d "${env_dir}" ] || ${python} -m venv ${env_dir}
    case "${OSTYPE}" in
        msys*) source ${env_dir}/Scripts/activate;;
        *) source ${env_dir}/bin/activate;;
    esac
    ${python} -m pip install --quiet --upgrade pip
}

clean() {
    bold "tf clean"
    rm -rf "./third_party/env/tensorflow"
    rm -rf "./third_party/src/tensorflow"
}

sync() {
    bold "tf sync"
    [ -d "./third_party/src/tensorflow" ] || git clone --quiet --recursive https://github.com/tensorflow/tensorflow.git "./third_party/src/tensorflow"
    pushd "./third_party/src/tensorflow" > /dev/null
    git pull --quiet --prune
    git submodule sync --quiet
    git submodule update --quiet --init --recursive
    popd > /dev/null
}

install() {
    bold "tf install"
    venv
    ${python} -m pip install --quiet --upgrade protobuf
    deactivate
}

schema() {
    bold "tf schema"
    [[ $(grep -U $'\x0D' ./src/tf-proto.js) ]] && crlf=1
    npx pbjs -t static-module -w closure --no-encode --no-delimited --no-comments --no-convert --no-verify --no-create --keep-case --decode-text -r tf -o ./src/tf-proto.js \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/saved_model.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/meta_graph.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/saver.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/graph.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/op_def.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/tensor_shape.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/types.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/node_def.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/versions.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/function.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/attr_value.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/tensor.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/variable.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/resource_handle.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/saved_object_graph.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/trackable_object_graph.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/struct.proto \
        ./third_party/src/tensorflow/tensorflow/core/protobuf/tensor_bundle.proto \
        ./third_party/src/tensorflow/tensorflow/core/framework/tensor_slice.proto \
        ./third_party/src/tensorflow/tensorflow/core/util/saved_tensor_slice.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/tf-proto.js ./src/tf-proto.js
    fi
}

metadata() {
    bold "tf metadata"
    [[ $(grep -U $'\x0D' ./src/tf-metadata.json) ]] && crlf=1
    venv
    case "${OSTYPE}" in
        linux*)
            [ -x "$(command -v protoc)" ] || sudo apt install -y protobuf-compiler libprotoc-dev
            ;;
        darwin*)
            brew list protobuf > /dev/null 2>&1 || brew install protobuf > /dev/null
            ;;
        msys*)
            [ -x "$(command -v protoc)" ] || $(choco install --yes protoc) > /dev/null
            ;;
    esac
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/attr_value.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/tensor.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/types.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/tensor_shape.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/resource_handle.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/api_def.proto --python_out=./tools
    protoc --proto_path ./third_party/src/tensorflow ./third_party/src/tensorflow/tensorflow/core/framework/op_def.proto --python_out=./tools
    touch ./tools/tensorflow/__init__.py
    touch ./tools/tensorflow/core/__init__.py
    touch ./tools/tensorflow/core/framework/__init__.py
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    ${python} ./tools/tf-script.py metadata
    rm -rf ./tools/tensorflow
    deactivate
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./src/tf-metadata.json ./src/tf-metadata.json
    fi
}

while [ "$#" != 0 ]; do
    command="$1" && shift
    case "${command}" in
        "clean") clean;;
        "sync") sync;;
        "install") install;;
        "schema") schema;;
        "metadata") metadata;;
    esac
done
