#!/usr/bin/env bash

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

KEYFILE="$DIR/../ssh-key/homeassistant-wakeonlan-sshkey"
TARGET="$HOME/.ssh/authorized_keys"

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

if ! which wakeonlan > /dev/null; then
	echo "You need to install \"wakeonlan\" first. Otherwise the ssh-key will be useless."
	exit 1
fi

SSHKEY=$(<"$KEYFILE.pub")
echo "command=\"wakeonlan \$SSH_ORIGINAL_COMMAND\" $SSHKEY" >> $TARGET

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