#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

###############################################################################
# This script is used to import a file into the marketplace. The source file
# is an opaque representation of an OpenNebula object, like a image file or a
# tar.gz with several vm template or flow disk images
###############################################################################

ONE_LOCATION = ENV['ONE_LOCATION']

if !ONE_LOCATION
    RUBY_LIB_LOCATION = '/usr/lib/one/ruby'
    GEMS_LOCATION     = '/usr/share/one/gems'
else
    RUBY_LIB_LOCATION = ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     = ONE_LOCATION + '/share/gems'
end

UTILS_PATH = File.join(File.dirname(__FILE__), '../../datastore')

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'
$LOAD_PATH << File.dirname(__FILE__)

require 'base64'
require 'rexml/document'
require 'getoptlong'
require 'pp'

require 'S3'

def xpath(xml, xpath)
    xml.elements[xpath].text.to_s rescue nil
end

xml = REXML::Document.new(Base64::decode64(ARGV[0])).root

source = xpath(xml, 'MARKETPLACEAPP/SOURCE')

# required
access_key_id     = xpath(xml, 'MARKETPLACE/TEMPLATE/ACCESS_KEY_ID')
secret_access_key = xpath(xml, 'MARKETPLACE/TEMPLATE/SECRET_ACCESS_KEY')
bucket            = xpath(xml, 'MARKETPLACE/TEMPLATE/BUCKET')
region            = xpath(xml, 'MARKETPLACE/TEMPLATE/REGION')

# optional
signature_version = xpath(xml, 'MARKETPLACE/TEMPLATE/SIGNATURE_VERSION')
endpoint          = xpath(xml, 'MARKETPLACE/TEMPLATE/ENDPOINT')
force_path_style  = xpath(xml, 'MARKETPLACE/TEMPLATE/FORCE_PATH_STYLE')

name = File.basename(source)

if name.empty?
    STDERR.puts "Empty SOURCE."
    exit 1
end

s3_config = {
    :region            => region,
    :access_key_id     => access_key_id,
    :secret_access_key => secret_access_key
}

s3_config[:signature_version] = signature_version if !signature_version.to_s.empty?
s3_config[:endpoint]          = endpoint if !endpoint.to_s.empty?
s3_config[:force_path_style]  = true if force_path_style.to_s.downcase == "yes"

s3 = S3.new(s3_config)

s3.name   = name
s3.bucket = bucket

if !s3.exists?
    STDERR.puts "Object '#{name}' does not exist."
    exit 1
end

s3.delete_object
