#!/bin/bash

mkdir -p ../third_party

repository=https://github.com/caffe2/caffe2.git

if [ -d "../third_party/caffe2" ]; then
    pushd "../third_party/caffe2" > /dev/null
    echo "Fetch ${repository}..."
    git fetch -p
    echo "Reset ${repository}..."
    git reset --hard origin/master
    popd > /dev/null
else
    echo "Clone ${repository}..."
    pushd "../third_party" > /dev/null
    git clone --recursive ${repository}
    popd > /dev/null
fi

echo "Generate '../src/caffe2.js'"
../node_modules/protobufjs/bin/pbjs -t static-module -w closure -r caffe2 -o ../src/caffe2.js ../third_party/caffe2/caffe2/proto/caffe2.proto

echo "Install Caffe2 dependencies"
brew install automake cmake gflags glog
sudo -H pip install future numpy protobuf pydot python-gflags pyyaml scikit-image setuptools six
sudo -H pip3 install future numpy protobuf pydot python-gflags pyyaml scikit-image setuptools six

echo "Build Caffe2"
pushd "../third_party/caffe2" > /dev/null
mkdir -p build && cd build
cmake -DUSE_CUDA=OFF ..
sudo make install
popd > /dev/null
