#!/bin/bash

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

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

venv() {
    env_dir=./third_party/env/pytorch
    [ -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() {
    echo "pytorch clean"
    rm -rf "./third_party/env/pytorch"
    rm -rf "./third_party/source/pytorch"
}

sync() {
    echo "pytorch sync"
    [ -d "./third_party/source/pytorch" ] || git clone --quiet https://github.com/pytorch/pytorch.git "./third_party/source/pytorch"
    git -C "./third_party/source/pytorch" pull --quiet --prune
}

install() {
    echo "pytorch install"
    venv
    ${python} -m pip install --quiet --upgrade future protobuf
    ${python} -m pip install --quiet --upgrade six pybind11 cython pyyaml numpy
    ${python} -m pip install --quiet --upgrade --pre torch -f https://download.pytorch.org/whl/nightly/cpu/torch_nightly.html
    deactivate
}

schema() {
    echo "caffe2 schema"
    [[ $(grep -U $'\x0D' ./source/caffe2-proto.js) ]] && crlf=1
    node ./tools/protoc.js --text --root caffe2 --out ./source/caffe2-proto.js ./third_party/source/pytorch/caffe2/proto/caffe2.proto
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/caffe2-proto.js ./source/caffe2-proto.js
    fi
}

metadata() {
    echo "pytorch metadata"
    venv
    export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
    if [[ $(grep -U $'\x0D' ./source/pytorch-metadata.json) ]]; then crlf=1; else crlf=; fi
    ${python} ./tools/pytorch-script.py metadata
    if [[ -n ${crlf} ]]; then
        unix2dos --quiet --newfile ./source/pytorch-metadata.json ./source/pytorch-metadata.json
    fi
    deactivate
}

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