#!/usr/bin/env oo-ruby
#--
# Copyright 2013 Red Hat, Inc.
#
# 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.
#++

def usage
  $stderr.puts <<USAGE
== Synopsis

#{$0}: Perform action (CRUD) on given gear using named cartridge

== Usage

#{$0} --with-container-uuid 1c3a5f7a92e111e188d800262df50034 \\
            --action start --with-cartridge-name mock

== List of arguments
       -a|--action                             One of <add|delete>
       -c|--with-container-uuid     gear_uuid  Unique identifier for the gear
       -n|--with-cartridge-name     name       Unique identifier for the cartridge
       -n|--with-cartridge-version  version    cartridge version to install
       -p|--with-software-version   version    packaged software version to install (default: latest)
       -u|--with-template-url                  override cartridge's default application
       -d|--debug                              enable additional output
       -h|--help                               Print this message
USAGE
  exit 255
end

require "rubygems"
require 'openshift-origin-node'
require 'openshift-origin-node/utils/environ'
require "etc"

opts = GetoptLong.new(
    ['--action', '-a', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-container-uuid', '-c', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-cartridge-name', '-n', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-software-version', '-p', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-template-url', '-u', GetoptLong::REQUIRED_ARGUMENT],
    ['--debug', '-d', GetoptLong::NO_ARGUMENT],
    ['--help', '-h', GetoptLong::NO_ARGUMENT],
)

action            = nil
uuid              = nil
cartridge_name    = nil
cartridge_version = nil
software_version  = nil
template_url      = nil

opts.each do |opt, arg|
  case opt
    when '--help'
      usage
    when '--with-container-uuid'
      uuid = arg
    when '--action'
      action = arg
    when '--with-cartridge-name'
      cartridge_name = arg
    when '--with-cartridge-version'
      raise NotImplementedError.new('--with-cartridge-version')
    when '--with-software-version'
      software_version = arg
    when '--with-template-url'
      template_url = arg
    when '--debug'
      $DEBUG = true
  end
end

unless action && uuid && cartridge_name && 0 == ARGV.length
  $stderr.puts "Invalid arguments\n"
  usage
end

begin
  # TODO; Will need to be refactored when broker gives separate pieces
  c = cartridge_name.to_s
  c << "-#{software_version}" if software_version

  container = OpenShift::ApplicationContainer.new(uuid, uuid)
  case action
    when 'add'
      container.configure(c, template_url)
    when 'delete'
      container.deconfigure(c)
    else
      usage
      raise NotImplementedError.new(action)
  end
rescue OpenShift::Utils::ShellExecutionException => e
  $stderr.puts "#{e.message}: rc(#{e.rc}"

  if $DEBUG
    $stderr.puts "stdout ==>\n#{e.stdout}\nstderr ==>\n#{e.stderr}"
    $stderr.puts e.backtrace.join("\n")
  end

  exit -1
rescue Exception => e
  $stderr.puts e.message
  $stderr.puts e.backtrace.join("\n") if $DEBUG
  exit -1
else
  exit 0
end
