#!/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/>
# --------------------------------------------------------------------------

ONE_LOCATION=ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION="/usr/lib/one/ruby"
else
    RUBY_LIB_LOCATION=ONE_LOCATION+"/lib/ruby"
end

$: << RUBY_LIB_LOCATION


require 'OpenNebula'
require 'OpenNebula/ImageRepositoryVMware'
require 'CommandManager'
require 'client_utilities'
require 'command_parse'

##########################
## COMMAND LINE PARSING ##
##########################

class OneVMwareParse < CommandParse

    COMMANDS_HELP=<<-EOT
Commands:

* register (Registers an image, copying it to the repository if it applies)
    onevmware register --disk-vmdk <path> --disk-flat <path1,path2,.> <tmplt>

    tmplt is a template filename where the Image description is located
EOT

    def text_commands
        COMMANDS_HELP
    end

    def text_command_name
        "onevmware"
    end

    def list_options
        table=ShowTable.new(ShowTableImage)
        table.print_help
    end

    def special_options(opts, options)
        opts.on_tail("-k path", "--disk-vmdk path", String, "Path to vmdk "<<
                "descriptor file") do |o|
            options[:vmdk]=o
        end
        opts.on_tail("-f path", "--disk-flat path", String, "Path to flat "<<
                "vmdk disk file(s), separated by commas.") do |o|
            options[:flat]=o
        end
    end
end


def get_template(template_path)
    begin
        template = File.read(ARGV[0])
    rescue
        result = OpenNebula::Error.new("Can not read template: #{ARGV[0]}")
    end

    if !is_successful?(result)
        puts result.message
        exit -1
    end

    return template
end

onevmware_opts=OneVMwareParse.new([])
onevmware_opts.parse(ARGV)
ops=onevmware_opts.options

result=[false, "Unknown error"]

command=ARGV.shift

img_repo = OpenNebula::ImageRepositoryVMware.new

case command
when "register", "create", "add"
    check_parameters("register", 1)
    template = get_template(ARGV[0])

    image = OpenNebula::Image.new(OpenNebula::Image.build_xml, get_one_client)

    if ops[:vmdk] and ops[:flat]
        result = img_repo.create_vmware(image, template, ops[:vmdk], ops[:flat])
    else
        result = OpenNebula::Error.new("Files not found.")
    end

    if is_successful?(result)
        puts "ID: " + image.id.to_s if ops[:verbose]
    end

else
    onevmware_opts.print_help
    exit -1
end

if OpenNebula.is_error?(result)
    puts "Error: " + result.message
    exit -1
end


