#!/bin/bash

SRV="${1:?Need a fqdn}"
SSHKEY_NAME="${2:-vpsbot}"

sshpubkey=$( ./getSSHKeys "${SSHKEY_NAME}" | jq -r '.key' )
current_image_id=$( ./getImageDetails "${SRV}" | jq -r '.id' )

IMAGE_ID="${3:-${current_image_id}}"
HTTP_PAYLOAD='{ "doNotSendPassword": true, "publicSshKey": "'${sshpubkey}'", "imageId": "'${IMAGE_ID}'" }'

./ovhcli post "vps/${SRV}/rebuild" "${HTTP_PAYLOAD}"

exit $?