#!/bin/bash
# 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.

echo "Updating PIP, SetupTools and Wheel..."
pip install --upgrade pip               # ensure next packages get the latest versions
pip install --upgrade setuptools wheel  # bring basic tooling for other requirements
pip install --upgrade pip-tools pylint  # bring tooling for package compilation and code linting
printf "\n"

echo "Creating integrated requirements file..."
tee requirements.in >/dev/null <<EOF
grpcio-tools==1.43.0
EOF
printf "\n"

# List of all the components, not only deployed ones
# TODO: include here your components
COMPONENTS="compute context device service monitoring opticalcentralizedattackdetector opticalattackmitigator dbscanserving webui"

echo "Collecting requirements from components..."
for COMPONENT in $COMPONENTS
do
    diff requirements.in src/$COMPONENT/requirements.in | grep '^>' | sed 's/^>\ //' >> requirements.in
done
printf "\n"

echo "Compiling requirements..."
# Done in a single step to prevent breaking dependencies between components
pip-compile --quiet --output-file=requirements.txt requirements.in
printf "\n"

echo "Installing requirements..."
python -m pip install -r requirements.txt
printf "\n"

#echo "Removing the temporary files..."
rm requirements.in
rm requirements.txt
printf "\n"
