#!/usr/bin/env ruby

# --------------------------------------------------------------------------
# Copyright 2010, C12G Labs S.L.
#
# This file is part of OpenNebula VMware Drivers.
#
# OpenNebula VMware Drivers is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation, either version 3 of
# the License, or the hope That it will be useful, but (at your
# option) any later version.
#
# OpenNebula VMware Drivers is distributed in WITHOUT ANY WARRANTY;
# without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License
# along with OpenNebula VMware Drivers . If not, see
# <http://www.gnu.org/licenses/>
# --------------------------------------------------------------------------

@host     = ARGV[0]
deploy_id = ARGV[1]

if !@host or !deploy_id
    exit -1
end

if !ONE_LOCATION
        VMWARERC_LOCATION="/etc/one/vmwarerc"
else
        VMWARERC_LOCATION=ONE_LOCATION+"/etc/vmwarerc"
end
load VMWARERC_LOCATION

data = perform_action(
         "virsh -c #{LIBVIRT_URI} --readonly dominfo #{deploy_id}") 
         
 if data.class == Fixnum
     puts "STATE=d"
     exit 0
 end

state = ""

data.split('\n').each{ |line|
  state = line.match("^State: (.*)")

  if state 
     state = state[1].strip
     break
  end
}

state_short = ""

case state
    when "running","blocked","shutdown","dying"
        state_short = 'a'
    when "paused"
        state_short = 'p'    
    when "crashed"
        state_short = 'c'
    else
        state_short = 'd'
end

puts "STATE=#{state_short}"



