generate_account_gateway_sources() (
  account_repo=$1
  account_tools=$2
  log_prefix=$3

  if [ -f "$account_repo/idl/pbgen/proto/central/service.pb.gw.go" ] &&
    [ -f "$account_repo/idl/pbgen/proto/v1beta1/service.pb.gw.go" ]; then
    return
  fi

  mkdir -p "$account_tools"
  install_account_tool() {
    tool_name=$1
    tool_package=$2
    if [ ! -x "$account_tools/$tool_name" ]; then
      printf '%s\n' "$log_prefix: install $tool_name"
      GOBIN="$account_tools" go install "$tool_package"
    fi
  }
  install_account_tool buf github.com/bufbuild/buf/cmd/buf@v1.1.0
  install_account_tool protoc-gen-buf-breaking github.com/bufbuild/buf/cmd/protoc-gen-buf-breaking@v1.1.0
  install_account_tool protoc-gen-buf-lint github.com/bufbuild/buf/cmd/protoc-gen-buf-lint@v1.1.0
  install_account_tool protoc-gen-go google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
  install_account_tool protoc-gen-go-grpc google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
  install_account_tool protoc-gen-grpc-gateway github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.10.3
  install_account_tool protoc-gen-openapiv2 github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.10.3

  printf '%s\n' "$log_prefix: buf generate account gateways"
  (cd "$account_repo/idl/idl" && PATH="$account_tools:$PATH" "$account_tools/buf" generate)
  (cd "$account_repo/idl/openapi_idl" && PATH="$account_tools:$PATH" "$account_tools/buf" generate)
  mkdir -p "$account_repo/central/docs/gwopenapi"
  cp "$account_repo/idl/pbgen/proto/central/service.swagger.json" "$account_repo/central/docs/"
  cp "$account_repo/idl/pbgen/proto/v1beta1/service.swagger.json" "$account_repo/central/docs/gwopenapi/"
)
