#!/bin/sh

#install requirements

case "$(uname -s)" in
  CYGWIN*|MINGW32*|MSYS*|MINGW*)
    py.exe -m pip install --upgrade pip
    pip3 install grpcio-tools
  ;;

  *)
    python3 -m pip install --upgrade pip
    pip3 install grpcio-tools
  ;;
esac

# paths
MC_ROOT=../../../../..
MC_API=$MC_ROOT/api/proto
MC_ATTEST_API=$MC_ROOT/attest/api/proto
MC_FOG_API=$MC_ROOT/fog/api/proto
MCD_API=$MC_ROOT/mobilecoind/api/proto
CONSENSUS_API=$MC_ROOT/consensus/api/proto

# compile protobuf for python
case "$(uname -s)" in
  CYGWIN*|MINGW32*|MSYS*|MINGW*)
    py.exe -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/external.proto
    py.exe -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/blockchain.proto
    py.exe -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/quorum_set.proto
    py.exe -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/watcher.proto
    py.exe -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API --python_out=. $MC_ATTEST_API/attest.proto
    py.exe -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. $MC_FOG_API/fog_common.proto
    py.exe -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. $MC_FOG_API/ledger.proto
    py.exe -m grpc_tools.protoc -I$MC_API -I$CONSENSUS_API --python_out=. $CONSENSUS_API/consensus_common.proto
    py.exe -m grpc_tools.protoc -I$MCD_API -I$CONSENSUS_API -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. --grpc_python_out=. $MCD_API/mobilecoind_api.proto
  ;;

  *)
    python3 -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/external.proto
    python3 -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/blockchain.proto
    python3 -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/quorum_set.proto
    python3 -m grpc_tools.protoc -I$MC_API --python_out=. $MC_API/watcher.proto
    python3 -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API --python_out=. $MC_ATTEST_API/attest.proto
    python3 -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. $MC_FOG_API/fog_common.proto
    python3 -m grpc_tools.protoc -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. $MC_FOG_API/ledger.proto
    python3 -m grpc_tools.protoc -I$MC_API -I$CONSENSUS_API --python_out=. $CONSENSUS_API/consensus_common.proto
    python3 -m grpc_tools.protoc -I$MCD_API -I$CONSENSUS_API -I$MC_API -I$MC_ATTEST_API -I$MC_FOG_API --python_out=. --grpc_python_out=. $MCD_API/mobilecoind_api.proto
  ;;
esac

# the autogenerated files need to be modified for the module to work
# see: https://github.com/protocolbuffers/protobuf/issues/1491

case "$(uname -s)" in
  CYGWIN*|MINGW32*|MSYS*|MINGW*)
    echo "Detected Windows environment. Don't forget to manually change pb2 import path!"
    # change all `import a_pb2` to `from . import a_pb2`
  ;;

  *)
    sed -i -E 's/^import.*_pb2/from . \0/' *.py
  ;;
esac
