#!/usr/bin/env bash

# Copyright 2022 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Update the Rust protobuf files on netsim-dev branch

# Prerequisites:
# - protobuf-compiler
# Linux: sudo apt-get install protobuf-compiler
# Mac:   brew install protobuf

# Absolute path to tools/netsim using this scripts directory
REPO_NETSIM=$(dirname $(readlink -f "$0"))/..
CARGO_MANIFEST=$REPO_NETSIM/rust/proto/Cargo.toml

# -- Step 1. Generate gRPC protobuf files
# NOTE: the files can not be generated by proto/build.rs because protoc-grpcio doesn't support protobuf v3 yet.
# https://github.com/mtp401/protoc-grpcio/issues/41

# Install compilers since the crates are not in AOSP
# TODO: Add required crate mappings to work in netsim-dev
export CARGO_HOME=""
# Specify versions to use the correct protobuf version.
cargo install protobuf-codegen --version 3.2.0
cargo install grpcio-compiler --version 0.13.0

PROTOC_CMD="protoc --rust_out=./rust/proto/src --grpc_out=./rust/proto/src\
 --plugin=protoc-gen-grpc=$(which grpc_rust_plugin)\
 -I./proto -I../../external/protobuf/src\
 -I../../packages/modules/Bluetooth/tools/rootcanal/proto"
$PROTOC_CMD ./proto/netsim/frontend.proto
$PROTOC_CMD ./proto/netsim/packet_streamer.proto

# Revert the generate proto files to ensure they are re-generated by proto/build.rs.
git checkout $REPO_NETSIM/rust/proto/src/packet_streamer.rs
git checkout $REPO_NETSIM/rust/proto/src/frontend.rs
rm $REPO_NETSIM/rust/proto/src/mod.rs

# --- Step 2. Generate protobuf files using proto/build.rs
# Uncomment lines starting with `##`
OS=$(uname | tr '[:upper:]' '[:lower:]')
if [[ "$OS" == "linux" ]]; then
    sed -i 's/^##//g' $CARGO_MANIFEST
fi
if [[ "$OS" == "darwin" ]]; then
    sed -i '' 's/^##//g' $CARGO_MANIFEST
fi

if [ ! -d "$REPO_NETSIM/objs/rust/.cargo" ]; then
    python3 $REPO_NETSIM/scripts/build_tools.py
fi

# Use Rust dependency crates available on netsim-dev branch
export CARGO_HOME=$REPO_NETSIM/objs/rust/.cargo

# For grpcio-sys
export GRPCIO_SYS_GRPC_INCLUDE_PATH="$REPO_NETSIM/../../external/grpc/include"

cd $REPO_NETSIM
cargo build --manifest-path $CARGO_MANIFEST

# Restore original Cargo.toml
git checkout $CARGO_MANIFEST

# Find the most recent rustfmt installed
RUSTFMT=$(ls -d ../../prebuilts/rust/$OS-x86/*/bin/rustfmt | tail -1)

# Format Rust code
# Need to format manually because it's not supported in build.rs
find $REPO_NETSIM/rust/proto -name '*.rs' -exec $RUSTFMT --files-with-diff {} \;

rm rust/Cargo.lock
