#!/bin/bash

# Variables
WEB_SERVER_URL="https://files.arrdnd.link/keys/lk.pub"  # Change this to your web server's URL
LOCAL_KEY_PATH="$HOME/.ssh/authorized_keys"  # Change this if necessary

# Fetch the public key from the web server
curl -o my_public_key.pub "${WEB_SERVER_URL}"

# Check if the command was successful
if [ $? -ne 0 ]; then
    echo "Failed to retrieve the public key."
    exit 1
fi

# Check if the key is already in authorized_keys
if grep -q "$(cat my_public_key.pub)" "$LOCAL_KEY_PATH"; then
    echo "Public key is already installed."
else
    # Install the public key to the authorized_keys file
    cat my_public_key.pub >> "$LOCAL_KEY_PATH"

    # Ensure the correct permissions are set for the authorized_keys file
    chmod 600 "$LOCAL_KEY_PATH"

    echo "Public key installed successfully."
fi

# Clean up the downloaded key file
rm my_public_key.pub