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

require 'rubygems'
require 'openshift-origin-node'
require 'openshift-origin-common'

module OpenShift
  module NodeLogger
    def self.logger
       unless @logger
          @logger = Logger.new(File.open(File::NULL, "w"))
       end
       @logger
    end

    def self.trace_logger
       unless @trace_logger
         @trace_logger = Logger.new(File.open(File::NULL, "w"))
         @trace_logger.level = Logger::Severity::ERROR
       end
       @trace_logger
    end
  end
end

def usage
  puts <<USAGE
== Synopsis

#{$0}: Lists all cartridges installed on the node

== Usage

#{$0} --with-descriptors [--porcelain]

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

USAGE
  exit 255
end

opts = GetoptLong.new(
    ['--with-descriptors',  '-a', GetoptLong::NO_ARGUMENT],
    ['--porcelain',         '-q', 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
end

if args["--help"]
  usage
end

list_descriptors = true if args['--with-descriptors']
$oo_debug = true if args['--debug']
$porcelain = args['--porcelain'] ? true : false

begin
  list = OpenShift::Node.get_cartridge_list(list_descriptors, $porcelain, $oo_debug)
  puts list
rescue Exception => e
  $stderr.puts(e.message)
  exit -1
else
  exit 0
end
