#!/usr/bin/env sh
set -eu

# Run from the WAProto directory regardless of the caller's working directory.
cd "$(dirname "$0")"
PACKAGE_ROOT=".."
PBJS="$PACKAGE_ROOT/node_modules/.bin/pbjs"
PBTS="$PACKAGE_ROOT/node_modules/.bin/pbts"

if [ ! -x "$PBJS" ] || [ ! -x "$PBTS" ]; then
    echo "protobufjs-cli is missing. Run npm install (or yarn install) in the package root first." >&2
    exit 1
fi

"$PBJS" -t static-module --no-beautify -w es6 --no-bundle --no-delimited --no-verify --no-comments -o ./index.js ./WAProto.proto
"$PBJS" -t static-module --no-beautify -w es6 --no-bundle --no-delimited --no-verify ./WAProto.proto | "$PBTS" --no-comments -o ./index.d.ts -
node ./fix-imports.js
