#!/bin/bash
#INFO Ensure that ssh IP and port are matching with each vagrant machine
#NOTE You can check VM-INFO in logs after a vagrant up
#NOTE SSH IP is the IP required, you can retrieve it on each machine via: SSH_IP=$(hostname -I | awk '{print $1}')

#TOTEST Dynamic ssh - IP assignation
#TOHAVE Dynamic port assignation
# Configuration file to have root access of the VM on VScode 
# Add each server and agent analogically.

# HACK for debug
cd /home/koraty/Documenti/master_thesis/vagrant

# DRAFT Dynamic config generation#
source logging.sh
source variables.sh

# TIMESTAMP=$(date +%Y%m%d_%H%M%S)          # Timestamp for uniqueness
# BACKUP_FILE="$PATH_TO_BACKUP_FOLDER/config_backup_$TIMESTAMP"

# # Create backup folder if it doesn't exist
# if [ -d "$PATH_TO_BACKUP_FOLDER" ]; then

#   # Backup the SSH configuration file
#   if [ -f "$SSH_CONFIG_FILE" ]; then
#     cp "$SSH_CONFIG_FILE" "$BACKUP_FILE"
#     chmod 600 "$BACKUP_FILE"  # Set permissions for the backup file
#     log $DEBUG_LEVEL $VERBOSE_INFO "Backup of SSH configuration file created at $BACKUP_FILE"
#   else
#     log $DEBUG_LEVEL $VERBOSE_WARNING "SSH configuration file not found. No backup created."
#   fi
# else
#   log $DEBUG_LEVEL $VERBOSE_ERROR " No Backup folder found - ensure to create a backup folder here : $PATH_TO_BACKUP_FOLDER"
# fi

# # Display current SSH configuration
# if [ -f "$SSH_CONFIG_FILE" ]; then
#   log $DEBUG_LEVEL $VERBOSE_INFO "Current SSH configuration:"
#   cat "$SSH_CONFIG_FILE"
# else
#   log $DEBUG_LEVEL $VERBOSE_WARNING "SSH configuration file does not exist."
# fi

# # Function to update or add host configuration
# update_or_add_host() {
#   local host_name=$1
#   local ip_address=$2
#   local port=$3

#   local config_block=$(cat <<EOF
# Host $host_name
#   HostName $ip_address
#   User root
#   Port $port
#   UserKnownHostsFile /dev/null
#   StrictHostKeyChecking no
#   PasswordAuthentication no
#   IdentityFile ~/.ssh/id_ed25519
#   IdentitiesOnly yes
#   LogLevel FATAL
#   PubkeyAcceptedKeyTypes +ssh-rsa
#   HostKeyAlgorithms +ssh-rsa
# EOF
# )

#   # Check if the host already exists in the config file
#   if grep -q "Host $host_name" "$SSH_CONFIG_FILE"; then
#     # Remove the existing host configuration block completely
#     sed -i "/Host $host_name/,/^Host /d" "$SSH_CONFIG_FILE"
#     log $DEBUG_LEVEL $VERBOSE_INFO "Updated $host_name configuration."
#   else
#     log $DEBUG_LEVEL $VERBOSE_INFO "Added new $host_name configuration."
#   fi

#   # Append the new configuration to the file
#   echo "$config_block" >> "$SSH_CONFIG_FILE"
# }

# # Retrieve IP addresses from the credentials file
# MANAGER_SSH_IP=$(grep -oP "Node: $MANAGER_NAME - IP: [\d\.]+ - SSH_IP:\K[\d\.]+" "$PATH_TO_CREDENTIALS")
# AGENT_COUNT=0

# # Dynamically retrieve IP addresses for all wazagent nodes
# for i in {1..10}; do  # Adjust the range as needed
#   AGENT_NAME="wazagent$i"
#   AGENT_SSH_IP=$(grep -oP "Node: $AGENT_NAME - IP: [\d\.]+ - SSH_IP:\K[\d\.]+" "$PATH_TO_CREDENTIALS")
#   if [ -n "$AGENT_SSH_IP" ]; then
#     AGENT_SSH_PORT=$((2200 + i - 1))  # Dynamically calculate the SSH port
#     update_or_add_host "$AGENT_NAME" "$AGENT_SSH_IP" "$AGENT_SSH_PORT"
#     AGENT_COUNT=$((AGENT_COUNT + 1))
#     log $DEBUG_LEVEL $VERBOSE_INFO "Updated or added host: $AGENT_NAME with IP: $AGENT_SSH_IP and port: $AGENT_SSH_PORT"
#   fi
# done

# # Calculate the port for the manager
# MANAGER_PORT=2222
# # HACK MANAGER_SSH_IP , ce n'est peut être pas ça , à verifier : ....
# MANAGER_SSH_IP = 127.0.0.1
# # Call the function for each host with the corresponding IP and port
# update_or_add_host "$MANAGER_NAME" "$MANAGER_SSH_IP" "$MANAGER_PORT"
# log $DEBUG_LEVEL $VERBOSE_INFO "Updated or added host: $MANAGER_NAME with IP: $MANAGER_SSH_IP and port: $MANAGER_PORT"

# # Set the correct permissions for the SSH configuration file
# chmod 600 "$SSH_CONFIG_FILE"

# # Display updated SSH configuration
# log $DEBUG_LEVEL $VERBOSE_INFO "Updated SSH configuration:"
# cat "$SSH_CONFIG_FILE"


# HACK COPY a simple ssh config file
cp $PATH_TO_SSH_CONFIG $SSH_CONFIG_FILE
log $DEBUG_LEVEL $VERBOSE_INFO "SSH configuration file updated at $SSH_CONFIG_FILE"
