#!/bin/bash
# This script will start/stop/status VMware machine
# Written by Ez-Aton
# http://www.tournament.org.il/run

# Hardcoded. Change to match your own settings!
VMWARE="/export/vmware/hosts/Windows_XP_Professional/Windows XP Professional.vmx"
VMRUN="/usr/bin/vmrun"
TIMEOUT=60

function status () {
	# This function will return success if the VM is up
	$VMRUN list | grep "$VMWARE" &>/dev/null
	if [[ "$?" -eq "0" ]]
	then
		echo "VM is up"
		return 0
	else
		echo "VM is down"
		return 1
	fi
}

function start () {
	# This function will start the VM
	$VMRUN start "$VMWARE"
	if [[ "$?" -eq "0" ]]
        then
                echo "VM is starting"
                return 0
        else
                echo "VM failed"
                return 1
        fi
}

function stop () {
	# This function will stop the VM
	$VMRUN suspend "$VMWARE" 
	for i in `seq 1 $TIMEOUT`
	do
		if status
		then
			echo 
		else
			echo "VM Stopped"
			return 0
		fi
		sleep 1
	done
	$VMRUN stop "$VMWARE" soft
}

case "$1" in
	start) 	start
		;;
	stop)	stop
		;;
	status)	status
		;;
esac
RET=$?

exit $RET
