#!/bin/bash

vm=Windows10
echo "Trying to shutdown $vm"
sudo virsh shutdown $vm

i=0

while [ $i -le 30 ]; do
  sleep 1
  sudo virsh list | rg $vm > /dev/null
  if [ $? -ne 1 ]; then
    echo "Still waiting for $vm to terminate..."
    ((i++))
  else
    echo "$vm terminated!"
    exit 0
  fi
done

echo "$vm did not terminate in a timely fashion, destroying instance."
sudo virsh destroy $vm
