# This script is hosted on https://amogusreal.tech for https://github.com/amogusreal69/ssh. It is also hosted at https://ssh.amogus.works/script.sh. Read more at the repository link.

#!/bin/bash

# Lumi's SSH key manager
#
# This script is used as a way to help users as fast as possible by instead of sharing their server's password, they add my SSH key, and I have access to their server.
# Idea inspired by Virtfusion, GPT-4o helped with this lol
# This is the new version of the script, accessible via:
#   $ wget -qO- https://ssh.amogus.works/script.sh | bash
#
# We also have a legacy version of the script, which is no longer maintained, accessible via:
#   $ wget -qO- https://ssh.amogus.works/old.sh | bash
# 
# Have fun! Made with ❤️ by Lumi

help() {
    echo -e "\033[92m● Usage:\e[0m"
    echo -e "  script.sh [add|remove|check|help]"
    echo ""
    echo -e "\033[92m● Options:\e[0m"
    echo -e "  \e[33m○ add\e[0m     - Installs the SSH key."
    echo -e "  \e[33m○ remove\e[0m  - Removes the SSH key."
    echo -e "  \e[33m○ check\e[0m   - Checks if the SSH key is installed."
    echo -e "  \e[33m○ help\e[0m    - Displays this help message."
    echo ""
    echo -e "\033[92m● GitHub Repository:\e[0m"
    echo -e "  \e[33m○ https://github.com/heyitslumi/ssh\e[0m"
    exit 0
}

add() {
    set +H
    echo -e "\e[30;44;1m\e[1;37m[INFO]\e[0;38;5;250m hey! incase you still use this script, i now have a SSH.id key, which in my opinion is better for me since well... i don't know but if you can use it please-\e[0m"
    echo -e "\e[30;44;1m\e[1;37m[INFO]\e[0;38;5;250m you can download my SSH.id keys at: https://sshid.io/lumi\e[0m"
    echo -e "\e[30;44;1m\e[1;37m[INFO]\e[0;38;5;250m or put it directly in your .ssh/authorized_keys file using this lil command: curl -fs https://sshid.io/lumi >> ~/.ssh/authorized_keys\e[0m"
    echo -e "\e[30;44;1m\e[1;37m[INFO]\e[0;38;5;250m there will be a timeout of 15 seconds just for you to give the time to read this\e[0m"
    echo -e "\e[30;44;1m\e[1;37m[INFO]\e[0;38;5;250m thanks and have a nice day! :3\e[0m"
    sleep 15

    mkdir -p ~/.ssh
    chmod 700 ~/.ssh
    touch ~/.ssh/authorized_keys

    grep -F -e "lumi@votion.dev" -e "lumi's support key, check next_steps.txt in root directory for more" ~/.ssh/authorized_keys >/dev/null 2>&1

    if [ $? -eq 0 ]; then
        echo -e "\033[93m● Support SSH key is already installed!\e[0m"
    else
        echo -e "\e[36m○ Downloading support SSH key...\e[0m"
        wget https://ssh.amogus.works/keys/ssh_key.pub -O "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi___key" >/dev/null 2>&1

        echo -e "\e[36m○ Downloading support SSH key checksum...\e[0m"
        wget https://ssh.amogus.works/keys/checksum/ssh_key.checksum -O "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi__key___check" >/dev/null 2>&1

        echo -e "\e[36m○ Verifying SSH key with SHA1...\e[0m"
        CHECKSUM=$(awk '{ print $1 }' "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi__key___check")
        KEYSUM=$(sha1sum "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi___key" | awk '{ print $1 }')

        if [ "${CHECKSUM}" == "${KEYSUM}" ]; then
            echo -e "\033[92m● SSH key verification successful!\e[0m"
            echo -e "\e[36m○ Installing SSH key...\e[0m"
            SSH_KEY=$(cat "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi___key")
            echo "${SSH_KEY}" >> ~/.ssh/authorized_keys
            rm -f "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi___key"
            rm -f "${TMP_DOWNLOAD_LOCATION}/__tmp___lumi__key___check"
            echo -e "\033[92m● Support SSH key installed successfully!\e[0m"
            echo ""
            next_steps
            echo ""
            save_next_steps
        else
            echo -e "\e[1;31m● Support SSH key verification failed! SSH Key didn't match checksum!\e[0m"
        fi
    fi

    echo -e "\e[36m○ Setting permissions...\e[0m"
    chmod 600 ~/.ssh/authorized_keys
}

remove_ssh_key() {
    grep -F -e "lumi@votion.dev" -e "lumi's support key, check next_steps.txt in root directory for more" ~/.ssh/authorized_keys >/dev/null 2>&1

    if [ $? -ne 0 ]; then
        echo -e "\e[1;31m● Support SSH key not found!\e[0m"
    else
        sed -i '/lumi@votion.dev/d' ~/.ssh/authorized_keys
        sed -i "/lumi's support key, check next_steps.txt in root directory for more/d" ~/.ssh/authorized_keys
        echo -e "\033[92m● Support SSH key removed successfully!\e[0m"
    fi
}

check_ssh_key() {
    grep -F -e "lumi@votion.dev" -e "lumi's support key, check next_steps.txt in root directory for more" ~/.ssh/authorized_keys >/dev/null 2>&1

    if [ $? -eq 0 ]; then
        echo -e "\033[92m● Support SSH key IS installed.\e[0m"
        echo -e "\033[92m○ To remove my support SSH key, run the following command:\e[0m"
        echo -e "  \e[33mwget -qO- https://ssh.amogus.works/script.sh | bash -s -- remove\e[0m"
    else
        echo -e "\033[93m● Support SSH key is NOT installed.\e[0m"
        echo -e "\033[92m○ To add my support SSH key, run the following command:\e[0m"
        echo -e "  \e[33mwget -qO- https://ssh.amogus.works/script.sh | bash -s -- add\e[0m"
    fi
}

next_steps() {
    echo -e "\e[36m=========================================\e[0m"
    echo -e "\033[92m            Next Steps                  \e[0m"
    echo -e "\e[36m=========================================\e[0m"
    echo -e "\033[92m● What you can do next:\e[0m"
    echo -e "  \e[33m○ Share your server's IP Address:\e[0m If you need support and I asked you to run this script, please share your server's IP address, as well as your SSH port."
    echo -e "    \e[1;31m○ Please, do not share your server's password, as I will not use it. If you are still using passwords, I recommend migrating to SSH Keys. It's safer.\e[0m"
    echo -e "  \e[33m○ Remove the SSH key:\e[0m Use \`wget -qO- https://ssh.amogus.works/script.sh | bash -s -- remove\` if you no longer need the key."
    echo -e "  \e[33m○ Check key status:\e[0m Use \`wget -qO- https://ssh.amogus.works/script.sh | bash -s -- check\` to confirm if the key is installed."
    echo -e "  \e[33m○ Visit the repository:\e[0m Check out the GitHub repo at \e[33mhttps://github.com/heyitslumi/ssh\e[0m for updates."
    echo -e "\e[36m=========================================\e[0m"
}

save_next_steps() {
    cat < ~/next_steps.txt
=========================================
            Next Steps                  
=========================================
● What you can do next:
  ○ Share your server's IP Address:
    If you need support and I asked you to run this script, please share your server's IP address, as well as your SSH port.
    ○ Please, do not share your server's password, as I will not use it. If you are still using passwords, I recommend migrating to SSH Keys. It's safer.
  ○ Remove the SSH key:
    Use \`wget -qO- https://ssh.amogus.works/script.sh | bash -s -- remove\` if you no longer need the key.
  ○ Check key status:
    Use \`wget -qO- https://ssh.amogus.works/script.sh | bash -s -- check\` to confirm if the key is installed.
  ○ Visit the repository:
    Check out the GitHub repo at https://github.com/heyitslumi/ssh for updates.
=========================================
EOF
    echo -e "\033[92m● This has also been saved to next_steps.txt.\e[0m"
}

SSH_KEY=''
TMP_DOWNLOAD_LOCATION=$(mktemp -d)
OPTION=$1

case "$OPTION" in
    add)
        add
        ;;
    remove)
        remove_ssh_key
        ;;
    check)
        check_ssh_key
        ;;
    --help|help|-h)
        help
        ;;
    *)
        echo -e "\e[1;31m● Invalid command or argument! Run the same script with the --help argument for the command list.\e[0m"
        ;;
esac