#!/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
  puts <<USAGE
== Synopsis

#{$0}: Deletes endpoints for a cartridge within a gear.
  This command must be run as root.

== Usage

#{$0} --with-container-uuid UUID \\
               --with-app-uuid APP_UUID \\
               --cart-name CART_NAME

== List of arguments
  -c|--with-container-uuid  gear_uuid   Unique identifier for the gear (required)
  -a|--with-app-uuid        app_uuid    Unique identifier for the application (required)
  -n|--cart-name            cart_name   Name of the cart within the gear (required)
  -h|--help                             Print this message

USAGE
  exit 255
end

require 'rubygems'
require 'openshift-origin-node'
opts = GetoptLong.new(
    ["--with-app-uuid",       "-a", GetoptLong::REQUIRED_ARGUMENT],
    ["--with-container-uuid", "-c", GetoptLong::REQUIRED_ARGUMENT],
    ["--cart-name",           "-n", GetoptLong::REQUIRED_ARGUMENT],
    ["--help",                "-?", GetoptLong::NO_ARGUMENT]
)

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

if args["--help"]
  usage
end

container_uuid = args['--with-container-uuid']
app_uuid = args['--with-app-uuid']
cart_name = args['--cart-name']

unless container_uuid && app_uuid && cart_name
  usage
end

begin
  container = OpenShift::ApplicationContainer.new(app_uuid, container_uuid)
  container.delete_public_endpoints(cart_name)
rescue Exception => e
  $stderr.puts(e.message)
  $stderr.puts(e.backtrace)
  exit -1
else
  exit 0
end
