#!/bin/bash
DIR="$(realpath "$(dirname "${BASH_SOURCE[0]}")")"
cd "$DIR/.."
set -x
SSH_KEY=~/.ssh/id_ed25519

# Create server.

gcloud compute instances create shishua-intel \
  --machine-type=n2-standard-2 \
  --maintenance-policy=TERMINATE \
  --zone=us-central1-f \
  --image-project=ubuntu-os-cloud --image-family=ubuntu-2004-lts
tar cJf shishua.tar.xz $(git ls-files)
while ! gcloud compute ssh shishua-intel --ssh-key-file="$SSH_KEY" \
    --zone=us-central1-f -- 'echo sshd started.'; do
  echo Awaiting sshd…
  sleep 10
done

# Run benchmark

gcloud compute scp ./shishua.tar.xz shishua-intel:~ --ssh-key-file="$SSH_KEY" --zone=us-central1-f
gcloud compute ssh shishua-intel --ssh-key-file="$SSH_KEY" --zone=us-central1-f -- 'tar xJf shishua.tar.xz && ./bin/perf-cloud-ubuntu-gcc.sh'
gcloud compute scp shishua-intel:~/test/benchmark-perf test/benchmark-perf-intel --ssh-key-file="$SSH_KEY" --zone=us-central1-f

# Clean up.

rm shishua.tar.xz
gcloud compute instances delete shishua-intel --zone=us-central1-f