#!/bin/bash
# bash  <(curl -s https://files.ratchfordconsulting.com/software/software_install/linux/fetch_github_public_keys_cli.sh)

while getopts u: flag
do
    case "${flag}" in
        u) username=${OPTARG};;
    esac
done

AUTHORIZED_KEYS_PATH="${HOME}/.ssh/authorized_keys"
URL="https://github.com/${username}.keys"
curl $URL >> "${AUTHORIZED_KEYS_PATH}.new" 2>/dev/null

if [[ -f $AUTHORIZED_KEYS_PATH ]]; then
    cp "${AUTHORIZED_KEYS_PATH}" "${AUTHORIZED_KEYS_PATH}.old"
else
    touch "${AUTHORIZED_KEYS_PATH}"
fi

cat "${AUTHORIZED_KEYS_PATH}.new" >> "${AUTHORIZED_KEYS_PATH}"
rm "${AUTHORIZED_KEYS_PATH}.new"