#!/usr/bin/env ruby
#--
# Copyright 2010 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
    puts <<USAGE
== Synopsis

oo-app-destroy: Deletes an application container.
  This command must be run as root.

== Usage

oo-app-destroy --with-container-uuid UUID \\
               --with-container-name NAME \\
               --with-app-uuid APP_UUID \\
               --with-app-name APP_NAME \\
               --with-namespace NAMESPACE

== List of arguments
  -a|--with-app-uuid        app_uuid    Unique application identifier (required)
  -c|--with-container-uuid  gear_uuid   Unique identifier for the gear(required)
    |--with-app-name        name        Name of the application (required)
    |--with-namespace       namespace   Namespace of the application (required)
    |--with-container-name  gear_name   Name of the gear
    |--skip-hooks                       Skip running pre and post hooks for cartridge destroy
  -n|--dry-run                          Don't make changes, just do a dry run.
  -q|--porcelain                        TODO: what does this do?
  -d|--debug                            Enable debug mode
  -h|--help                             Print this message

USAGE
end

require 'rubygems'
require 'openshift-origin-node'
opts = GetoptLong.new(
    ["--with-app-uuid",       "-a", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-container-uuid", "-c", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-app-name",             GetoptLong::REQUIRED_ARGUMENT],
    ["--with-namespace",            GetoptLong::REQUIRED_ARGUMENT],
    ["--with-container-name",       GetoptLong::OPTIONAL_ARGUMENT],
    ["--dry-run",             "-n", GetoptLong::NO_ARGUMENT],
    ["--porcelain",           "-q", GetoptLong::NO_ARGUMENT],
    ["--skip-hooks",                GetoptLong::NO_ARGUMENT],
    ["--debug",               "-d", GetoptLong::NO_ARGUMENT],
    ["--help",                "-?", GetoptLong::NO_ARGUMENT]
)

args = {}
begin
    opts.each{ |k,v| args[k]=v }
rescue GetoptLong::Error => e
    usage
    exit -100
end

if args["--help"]
  usage
  exit -1
end

app_uuid = args['--with-app-uuid']
app_name = args['--with-app-name']
container_uuid = args['--with-container-uuid']
container_name = args['--with-container-name'] || args['--with-app-name']
namespace = args['--with-namespace']
skip_hooks = true if args['--skip-hooks']
$dry_run = true if args['--dry-run']
$oo_debug = true if args['--debug']
$porcelain = args['--porcelain'] ? true : false

unless container_uuid
  usage
  exit -100
end

begin
  container = OpenShift::ApplicationContainer.new(app_uuid, container_uuid, nil, app_name,
                                                   container_name, namespace, nil, nil)
  out, err, rc = container.destroy(skip_hooks)
rescue Exception => e
  $stderr.puts(e.message)
  exit -1
else
  $stdout.puts(out) if out != ""
  $stderr.puts(err) if err != ""
  exit rc
end
