#!/bin/bash

IP=$(hostname -I)
DIR_PATH=$(pwd)
ENV_NAME='venv'
SECURE_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
AGENT_FILENAME='agent_params.txt'

sudo echo "IP=$IP" > $DIR_PATH/$AGENT_FILENAME
sudo echo "KEY=$SECURE_STRING" >> $DIR_PATH/$AGENT_FILENAME

create_and_activate_venv() {
    local venv_name=$1
    sudo python3 -m venv "$venv_name"
    source "$venv_name/bin/activate"
}

if [ -f /etc/lsb-release ]; then
    echo "Ubuntu"
    sudo apt-get -y install python3-pip python3-virtualenv python3-venv
    # Create and activate the virtual environment
    create_and_activate_venv $ENV_NAME
    sudo apt-get -y install python3-paramiko python3-dev libpq-dev python3-psycopg2 python3-pymongo python3-elasticsearch
    pip3.10 install -r tasks_requirements.txt
elif [ -f /etc/redhat-release ]; then
    echo "CentOS"
    sudo yum -y install python3-pip python3-virtualenv python3-venv
    pip3.10 install virtualenv
    # Create and activate the virtual environment
    create_and_activate_venv $ENV_NAME
    # sudo yum -y install python3-paramiko python3-devel postgresql-devel python3-psycopg2 python3-pymongo python3-elasticsearch
    pip3.10 install setuptools_rust
    pip3.10 install -r tasks_requirements.txt
else
    echo "Unknown OS"
fi

# Deactivate the virtual environment
deactivate