#!/bin/bash

# PURPOSE: Script to start a virtual machine (VM) on Ubuntu Server.
#
# AUTHOR: feedback[AT]HaveTheKnowHow[DOT]com
#
# Expects one argument which is the name of the VM you wish to start

#give server 3 minutes to start up
sleep 180

for i in 1 2 3
do
  str=$(/usr/bin/virsh --connect qemu:///system list --all | grep " $1" | awk '{print $3}')

  echo 'NOTE: Current Status of '$1 'is' ${str}

  if [ ${str} = 'shut' ]
  then
    echo $1 'is currently shutdown so starting it'
    /usr/bin/virsh --connect qemu:///system start $1
    sleep 30
  else
    echo $1 'is already running'
    exit
  fi
done

echo 'ERROR: Server could not be started'
/usr/sbin/ssmtp myemail@myaddress.com </home/htkh/MyScripts/msg.txt
echo 'Email Sent.....'