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

require 'rubygems'
require 'openshift-origin-node'
require 'openshift-origin-node/utils/shell_exec'

def usage
    puts <<USAGE
== Synopsis

oo-env-var-remove: Removes an env var from the app.
  This command must be run as root.

== Usage

oo-env-var-remove  --with-app-uuid APP_UUID \\
                    --with-container-uuid UUID \\
                    --with-key KEY

Options:
-h|--help:
   Prints this message

UUID: Unique identifier for the application
NAME: The name of the application to create

USAGE
end

opts = GetoptLong.new(
    ['--with-app-uuid',       '-a', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-container-uuid', '-c', GetoptLong::REQUIRED_ARGUMENT],
    ['--with-key',            '-k', GetoptLong::REQUIRED_ARGUMENT]
)

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

uuid = args['--with-container-uuid']
key = args['--with-key']

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

unless uuid and key
  usage
  exit -100
end

begin
  container = OpenShift::ApplicationContainer.new(uuid, uuid)
  container.user.remove_env_var(key)
rescue Exception => e
  $stderr.puts(e.message)
  exit -1
else
  exit 0
end
