#!/usr/bin/env bash

mkdir -p /config/ssh/

SSH_KEY=/config/ssh/id_rsa

echo "Documentation for this script: https://docs.olivetin.app/action-ssh-easy.html"
echo ""

if test -f "$SSH_KEY"; then
	echo "Using existing SSH key at $SSH_KEY, not generating a new one"
else
	ssh-keygen -f $SSH_KEY -N ''
	echo "Your new SSH key has been generated and saved to $SSH_KEY"
fi

echo "To use this SSH key with OliveTin, format your action line like this:"
echo ""
echo "actions:"
echo "  - name: My SSH Action"
echo "    shell: ssh -F /config/ssh/easy.cfg user@host \"echo 'Hello, world!'\""
echo ""
echo "Note: This config has ssh key checking turned off so you don't have to "
echo "keep approving host keys inside the OliveTin container. This should only"
echo "be used in environments where checking SSH host keys is not important."
echo ""
echo "If validating SSH host keys is important, you should not use this config."
echo ""

cat <<EOF > /config/ssh/easy.cfg
Host *
	StrictHostKeyChecking no
	UserKnownHostsFile=/dev/null
	IdentityFile /config/ssh/id_rsa
EOF

echo "Copy this line into your ~/.ssh/authorized_keys file to allow OliveTin to connect to your server:"
cat /config/ssh/id_rsa.pub
