#!/bin/bash

# Clear the terminal screen
clear

# Function to create an SSH key
create_ssh_key() {
    local hostname=$1
    local username=$2
    local key_name=$3

    # Define the file name for the key
    local file_name="${username}@${hostname}${key_name}"

    # Generate the SSH key
    ssh-keygen -t rsa -b 4096 -C "${username}@${hostname}" -f ~/.ssh/${file_name} -N ""

    # Add a suffix to the .pub file
    local pub_file=~/.ssh/${file_name}.pub
    sed -i '' "s/$/ ${key_name}/" ${pub_file}
    # Display the public key
    echo -e "\033[1;32mPublic key for ${file_name}. This will not be shown again, and the file has been deleted.\033[0m"
    echo -e "\033[1;32mPlace the key in the authorized_keys file\033[0m"
    cat ${pub_file}
    rm ${pub_file}
    echo -e "\033[1;32mSSH key created: ${file_name}\033[0m"
    echo
    
}

# Prompt the user for hostname and username with colors
echo -e "\033[1;34mEnter the hostname:\033[0m"
read hostname
echo -e "\033[1;34mEnter the username:\033[0m"
read username

# Prompt the user for the number of keys to create
echo -e "\033[1;34mEnter the number of keys to create:\033[0m"
read num_keys

# Loop to create the specified number of keys
for ((i=1; i<=num_keys; i++)); do
    echo -e "\033[1;34mEnter the key name for key $i:\033[0m"
    read key_name
    create_ssh_key $hostname $username $key_name
done

echo -e "\033[1;32mAll keys created successfully, Time to add one of those files to the local .ssh/config file. To not do that, type ^C\033[0m"

echo -e "\033[1;34mHostname to add to config\033[0m"
read ssh_host
echo
echo -e "\033[1;34mUser to add to config\033[0m"
read ssh_user
echo
echo -e "\033[1;34mKeyfile name to add to config\033[0m"
read ssh_keyfile
echo
echo "" >> ~/.ssh/config
echo "Host $ssh_host" >> ~/.ssh/config
echo "  User $ssh_user" >> ~/.ssh/config
echo "  IdentityFile ~/.ssh/$ssh_keyfile" >> ~/.ssh/config
echo "" >> ~/.ssh/config

