#!/usr/bin/env oo-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

#{$0}: Deletes an application container.
  This command must be run as root.

== Usage

#{$0} --with-container-uuid UUID \\
               --with-container-name NAME \\
               --with-app-uuid APP_UUID \\
               --with-app-name APP_NAME \\
               --with-namespace NAMESPACE

== List of arguments
  -s|--with-namespace       namespace   Namespace of the application (required)
  -a|--with-app-uuid        app_uuid    Unique application identifier (required)
  -A|--with-app-name        name        Name of the application (required)
  -c|--with-container-uuid  gear_uuid   Unique identifier for the gear(required)
  -C|--with-container-name  gear_name   Name of the gear (defaults to app name)
  -H|--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
  exit 255
end

require 'rubygems'
require 'getoptlong'
opts = GetoptLong.new(
    ["--with-namespace",      "-s", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-app-uuid",       "-a", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-app-name",       "-A", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-container-uuid", "-c", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-container-name", "-C", GetoptLong::OPTIONAL_ARGUMENT],
    ["--dry-run",             "-n", GetoptLong::NO_ARGUMENT],
    ["--porcelain",           "-q", GetoptLong::NO_ARGUMENT],
    ["--skip-hooks",          "-H", GetoptLong::NO_ARGUMENT],
    ["--debug",               "-d", GetoptLong::NO_ARGUMENT],
    ["--help",                "-h", GetoptLong::NO_ARGUMENT]
)

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

if args["--help"]
  usage
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
end

require 'openshift-origin-node'

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
