#!/bin/sh

help()
{
    echo -h: help
    echo -s IP: board ip-address
    exit
}

while getopts hs: flag
do
    case "${flag}" in
        h) help;;
        s) BOARDIP=$OPTARG;;
    esac
done

echo Board-IP: $BOARDIP

if [ -z "$BOARDIP" ]
then
	BOARDIP=$(awk 'BEGIN{FS="="}/^ *BOARDIP/{gsub(" ", "", $2); gsub("\r", "", $2); gsub("\n", "", $2); print($2);}' $1)
fi
printf "BOARDIP: '%s'\n" "$BOARDIP"

if [ ! -f "/home/$USER/.ssh/id_rsa" ]
then
    mkdir -p "/home/$USER/.ssh"
    ssh root@"$BOARDIP" "exit"
    ssh-keygen
fi

ssh-keygen -f "/home/$USER/.ssh/known_hosts" -R "$BOARDIP"
ssh-copy-id root@"$BOARDIP"
