#!/usr/bin/env bash
set -ex -o pipefail

OUT_DIR=src/proto

if [ -z "$TIRO_PROTOS_DIR" ]; then
  mkdir -p ./.tmp
  git clone --depth 1 \
      https://github.com/tiro-is/tiro-speech-core.git \
      .tmp/tiro-speech-core
  TIRO_PROTOS_DIR=.tmp/tiro-speech-core/proto
fi

protoc \
  --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
  --js_out=import_style=commonjs,binary:${OUT_DIR} \
  --ts_out=service=grpc-web:src/proto -I${TIRO_PROTOS_DIR} tiro/speech/v1alpha/speech.proto

GOOGLE_PROTOS=(
  google/rpc/status.proto
  google/api/http.proto
  google/api/client.proto
  google/api/annotations.proto
  google/api/field_behavior.proto
)

protoc \
  --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts \
  --js_out=import_style=commonjs,binary:${OUT_DIR} \
  -I${TIRO_PROTOS_DIR} "${GOOGLE_PROTOS[@]}"
