#!/usr/bin/env ruby
# Copyright 2011 Red Hat, Inc.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

require 'rhc-common'

#
# print help
#
def p_usage
    rhlogin = get_var('default_rhlogin') ? "Default: #{get_var('default_rhlogin')}" : "required"
    puts <<USAGE

Usage: #{$0}
Display information about a user

  -l|--rhlogin   rhlogin    Red Hat login (RHN or OpenShift login with OpenShift Express access) (#{rhlogin})
  -p|--password  password   RHLogin password (optional, will prompt)
  -a|--apps                 List applications for rhlogin
  -i|--info                 Show user info
  -d|--debug                Print Debug info
  -h|--help                 Show Usage info

USAGE
exit 255
end

begin
    opts = GetoptLong.new(
        ["--debug", "-d", GetoptLong::NO_ARGUMENT],
        ["--help",  "-h", GetoptLong::NO_ARGUMENT],
        ["--apps",  "-a", GetoptLong::NO_ARGUMENT],
        ["--info",  "-i", GetoptLong::NO_ARGUMENT],
        ["--rhlogin",  "-l", GetoptLong::REQUIRED_ARGUMENT],
        ["--password",  "-p", GetoptLong::REQUIRED_ARGUMENT]
    )
    opt = {}
    opts.each do |o, a|
        opt[o[2..-1]] = a.to_s
    end
rescue Exception => e
  #puts e.message
    p_usage
end

# Pull in configs from files
libra_server = get_var('libra_server')
debug = get_var('debug') == 'false' ? nil : get_var('debug')

libra_kfile = "#{ENV['HOME']}/.ssh/libra_id_rsa"
libra_kpfile = "#{ENV['HOME']}/.ssh/libra_id_rsa.pub"

if opt["help"]
    p_usage
end

if opt["debug"]
    debug = true
end

opt["rhlogin"] = get_var('default_rhlogin') unless opt["rhlogin"]

if !RHC::check_rhlogin(opt['rhlogin'])
    p_usage
end

password = opt['password']
if !password
  password = RHC::get_password
end

opt['apps'] = true if not opt['info'] and not opt['apps']
user_info = RHC::get_user_info(libra_server, opt['rhlogin'], password, @http, debug, true)

if opt['info']
    puts "User Info"
    puts "========="
    puts "Namespace: #{user_info['user_info']['namespace']}"
    puts "    UUID: #{user_info['user_info']['uuid']}"
    puts "   RHLogin: #{user_info['user_info']['rhlogin']}"
    puts " ssh_key: #{user_info['user_info']['ssh_key']}"
end

if opt['apps']
    puts "\n\n" if opt['info']

    puts "Application Info"
    puts "================"
    user_info['app_info'].each do |key, val|
        puts key
        puts "    Framework: #{val['framework']}"
        puts "     Creation: #{val['creation_time']}"
        puts "         UUID: #{val['uuid']}"
        puts "      Git URL: ssh://#{val['uuid']}@#{key}-#{user_info['user_info']['namespace']}.#{user_info['user_info']['rhc_domain']}/~/git/#{key}.git/"
        puts "   Public URL: http://#{key}-#{user_info['user_info']['namespace']}.#{user_info['user_info']['rhc_domain']}/"
        puts ""
        puts " Embedded: "
        if val['embedded'] && !val['embedded'].empty? 
            val['embedded'].each do |embed_key, embed_val|
                puts "      #{embed_key} - #{embed_val['info']}"
            end
        else
            puts "      None"
        end
        puts ""
    end

end

exit 0
