#!/usr/bin/env bash

# Base params
BASE_URI_START="https://glb.tyleo.dev/admin/keys/tyleo-phoenix-"
BASE_URI_ENDING=".pub"

# Ask for the Key Code to correspond to PUB files on https://glb.tyleo.dev/admin/keys static system
read -p "Key Code : " keycode

# Modelization of the Key URI, based on params + key code provided before
KEY_URI=$BASE_URI_START$keycode$BASE_URI_ENDING

# Get the Key data
KEY_DATA=$(curl $KEY_URI)

# Authorized Key File Structure Autorisation AutoFix (0700 is mandatory for .ssh, so we enforce it. And we enforce the user to be the owner.)
mkdir ~/.ssh
chmod 0700 -Rv ~/.ssh
chown $USER:$USER -Rv ~/.ssh

# Write the Key
echo $KEY_DATA >> ~/.ssh/authorized_keys

# Write Sources
echo $KEY_URI >> ~/.ssh/authorized_keys_sources