#!/bin/bash -eu
#
# Copyright 2021-2023 H2020 TeraFlow (https://www.teraflow-h2020.eu/)
#
# 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.

# Make folder containing the script the root folder for its execution
cd $(dirname $0)

rm -rf proto/*.py
rm -rf proto/__pycache__
touch proto/__init__.py

python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto context.proto
python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto kpi_sample_types.proto
python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto interdomain.proto
python -m grpc_tools.protoc -I../../proto --python_out=proto --grpc_python_out=proto slice.proto

rm proto/context_pb2_grpc.py
rm proto/kpi_sample_types_pb2_grpc.py
rm proto/slice_pb2_grpc.py

sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/context_pb2.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/kpi_sample_types_pb2.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/interdomain_pb2.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/interdomain_pb2_grpc.py
sed -i -E 's/(import\ .*)_pb2/from . \1_pb2/g' proto/slice_pb2.py
