#!/usr/bin/env bash

DIR=$(dirname "$(readlink -f "$0")")

KEYFILE="$DIR/../ssh-key/homeassistant-wallboard-hdmi-sshkey"
TARGET="~/.ssh/authorized_keys"

if [ $# == 0 ] ; then
    echo "Usage: install-wallboard-hdmi-sshkey.sh user@wallboard-ip"
    exit 1;
fi

if [ ! -f $KEYFILE ]; then
	echo "You need to create the SSH-Key-File first: \"$KEYFILE\""
	echo "Use: \"ssh-keygen -t ed25519 -C homeassistant-wallboard-hdmi-sshkey -f $KEYFILE\""
	exit 1
fi

SSHKEY=$(<"$KEYFILE.pub")
echo "command=\"vcgencmd \$SSH_ORIGINAL_COMMAND\" $SSHKEY" | ssh $1 '\
	mkdir -p .ssh && \
	chmod 700 .ssh && \
	touch .ssh/authorized_keys && \
	chmod 600 .ssh/authorized_keys && \
	tee -a .ssh/authorized_keys \
' >/dev/null

echo "Installed the ssh key into \"$1:$TARGET\"."
