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

ONE_LOCATION = ENV['ONE_LOCATION']

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

require 'load_opennebula_paths'

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << RUBY_LIB_LOCATION + '/cli'

USER_AGENT = 'CLI'

require 'command_parser'
require 'one_helper'
require 'opennebula/oneform_client'
require 'tempfile'

require 'one_helper/oneform_helper'

CommandParser::CmdParser.new(ARGV) do
    FORMAT = [OpenNebulaHelper::JSON, OpenNebulaHelper::YAML]

    usage '`oneform` <command> [<args>] [<options>]'
    version OpenNebulaHelper::ONE_VERSION

    set :option, OneForm::Client::DEFAULT_OPTIONS
    set :option, CommandParser::VERSION
    set :option, CommandParser::HELP

    ENABLED = {
        :name => 'enabled',
        :short => '-e',
        :large => '--enabled',
        :description => 'Show only enabled providers',
        :default => false
    }

    helper = OneFormHelper.new

    ########################################################################
    # Commands for interacting with oneform drivers
    ########################################################################

    list_desc = <<-EOT.unindent
        List all drivers installed on the system.
    EOT

    command :list,
            list_desc,
            :options => FORMAT + CLIHelper::OPTIONS + [OpenNebulaHelper::DESCRIBE, ENABLED] do
        enabled = options[:enabled] || false
        helper.list_driver_pool(helper.client(options), options, :enabled => enabled)
    end

    top_desc = <<-EOT.unindent
        List the available drivers continuously.
    EOT

    command :top,
            top_desc,
            :options => FORMAT + [CLIHelper::DELAY, ENABLED] do
        Signal.trap('INT') { exit(-1) }
        enabled = options[:enabled] || false
        helper.top_driver_pool(helper.client(options), options, :enabled => enabled)

        0
    end

    show_desc = <<-EOT.unindent
        Show details of a specific driver.
    EOT

    command :show, show_desc, :driver_name, :options => FORMAT do
        driver_name = args[0]

        helper.format_resource(helper.client(options), driver_name, options)
    end

    sync_desc = <<-EOT.unindent
        Synchronize OneForm drivers, refreshing and update available drivers
    EOT

    command :sync, sync_desc do
        client      = helper.client(options)

        response = client.sync_drivers

        return [response[:err_code], response[:message]] \
            if CloudClient.is_error?(response)

        0
    end

    enable_desc = <<-EOT.unindent
        Enable a driver in OneForm to permit the creation of new resources based on it.
    EOT

    command :enable, enable_desc, :driver_name do
        client      = helper.client(options)
        driver_name = args[0]

        response = client.enable_driver(driver_name)

        return [response[:err_code], response[:message]] \
            if CloudClient.is_error?(response)

        0
    end

    disable_desc = <<-EOT.unindent
        Disables a driver in OneForm, avoiding creation of new resources based on it.
    EOT

    command :disable, disable_desc, :driver_name do
        client   = helper.client(options)
        driver_name = args[0]

        response = client.disable_driver(driver_name)

        return [response[:err_code], response[:message]] \
            if CloudClient.is_error?(response)

        0
    end
end
