#!/bin/bash

. ../_local_config.bash

if [ ! -f $gsvm_project--$gsvm_subproj ] ; then
    echo ""
    echo "----"
    echo "- Generating RSA key-pair for ssh access"
    echo "- [Press enter twice to set with an empty passphrase]"
    ssh-keygen -t rsa -f $gsvm_project--$gsvm_subproj -C $gsvm_project--$gsvm_subproj
    echo "----"
fi

ssh_rsa_pubkey=$(cat $gsvm_project--$gsvm_subproj.pub | awk '{ print $2 }')


echo ""
echo "####"
echo "# Assuming that the Google Cloud SDK has aready been initializied with:"
echo "#    gcloud init"
echo "# or on a headless server:"
echo "#   gcloud auth login --no-launch-browser"
echo "####"
echo ""


echo "Using gcloud to create a base Debian VM instance: $GSVM_INSTANCE"
echo "[OK to ignore the warning about the set '40 GB' disk size being larger than the default '10 GB']"

gcloud compute instances create $GSVM_INSTANCE \
    --project=$gsvm_project \
    --zone=$gsvm_zone \
    --machine-type=e2-medium \
    --network-interface=network-tier=STANDARD,stack-type=IPV4_ONLY,subnet=default,no-address \
    --metadata="^%^ssh-keys=$GSVM_USER:ssh-rsa $ssh_rsa_pubkey ssh-$gsvm_project--$gsvm_subproj {\"userName\":\"$GSVM_USER@gmail.com\"}" \
    --maintenance-policy=MIGRATE \
    --provisioning-model=STANDARD \
    --scopes=https://www.googleapis.com/auth/devstorage.read_only,https://www.googleapis.com/auth/logging.write,https://www.googleapis.com/auth/monitoring.write,https://www.googleapis.com/auth/servicecontrol,https://www.googleapis.com/auth/service.management.readonly,https://www.googleapis.com/auth/trace.append \
    --tags=http-server,https-server \
    --create-disk=auto-delete=yes,boot=yes,device-name=$gsvm_instance_disk,image=projects/debian-cloud/global/images/debian-11-bullseye-v20240110,mode=rw,size=40,type=projects/$gsvm_project/zones/$gsvm_zone/diskTypes/pd-balanced \
    --no-shielded-secure-boot \
    --shielded-vtpm \
    --shielded-integrity-monitoring \
    --labels=goog-ec-src=vm_add-gcloud \
    --reservation-affinity=any

if [ $? != 0 ] ; then
    echo "" 2>&1
    echo "Warning: Failed to run gcloud command to create instance" >&2
    # exit 1
fi
echo ""

echo "Using gcloud to see if statically reserved external IP for network interface already exists for: $gsvm_instance_nic"
external_static_ip=$(gcloud compute addresses --project=$gsvm_project  describe $gsvm_instance_nic --region=$gsvm_region --format='value(address)' 2>/dev/null)

if [ "x$external_static_ip" != "x" ] ; then
    echo "  Network address exists: $external_static_ip"
else
    echo "Using gcloud to create statically reserved external IP for network interface: $gsvm_instance_nic"

    gcloud compute addresses create $gsvm_instance_nic --project=$gsvm_project --network-tier=STANDARD --region=$gsvm_region
    if [ $? != 0 ] ; then
	echo "Warning: Failed to run gcloud command to create network interface with statically allocated IP" >&2
	exit 1
    fi
fi

echo ""
echo "Using gcloud to retrieve allocated IP for network interface: $gsvm_instance_nic"
external_static_ip=$(gcloud compute addresses --project=$gsvm_project  describe $gsvm_instance_nic --region=$gsvm_region --format='value(address)')

if [ $? != 0 ] ; then
    echo "Error: Failed to run gcloud command to retrieve allocated network interface for $gsvm_instance_nic" >&2
    exit 1
fi

echo ""
echo "Using gcloud to attach network interface '$gsvm_instance_nic' [$external_static_ip] to '$GSVM_INSTANCE'"
gcloud compute instances add-access-config $GSVM_INSTANCE --project=atea-storage --zone=australia-southeast1-b --address=$external_static_ip --network-tier=STANDARD
if [ $? != 0 ] ; then
    echo "Error: Failed to run gcloud command to attached network interface $gsvm_instance_nic to VM instance $GSVM_INSTANCE" >&2
    echo "" >&2
    exit 1
fi

echo ""
echo "Removing any existing ssh keys for $GSVM_FQDM from known_hosts"
ssh-keygen -f "$HOME/.ssh/known_hosts" -R "$GSVM_FQDN" >/dev/null

echo ""
echo "----"
echo "For your Greenstone3 VM to be publically accessible over the web, ensure the following"
echo "DNS record has been create through your Domain Hosting provider (e.g., Freeparking):"
echo "  A-Record: $GSVM_FQDN -> $external_static_ip"
echo "----"

echo ""

echo "----"
echo "To provision your VM to run Greenstone3 over https, run:"
echo "    ./ssh-provision-vm.sh"
echo "----"
echo ""


