#!/usr/bin/env ruby
#
# print help
#
def p_usage(exit_code = 255)
    puts <<USAGE

Usage: rhc (<resource> | --help) [<command>] [<args>]
Command line tool for performing operations related to your rhcloud account.

List of resources
  domain             Manage the namespace for the registered rhcloud user.
  app                Manage applications within the rhcloud account.
  sshkey             Manage multiple keys for the registered rhcloud user.
  port-forward       Forward remote ports to the workstation

See 'rhc <resource> --help' for more applicable commands and argumments on a specific resource.

USAGE
exit exit_code
end


def get_args
  ARGV.shift
  args = ""
  ARGV.each do|a|
    if ( a.to_s.strip.length == 0 || a.to_s.strip.match(/\s/) ); a = "'#{a}'" end    
    args += " #{a}"
  end
  args
end

case ARGV[0]
when "domain"
  system("rhc-domain #{get_args} 2>&1")
  retcode = $?.exitstatus
when "app"
  system("rhc-app #{get_args} 2>&1")
  retcode = $?.exitstatus
when "sshkey"
  system("rhc-sshkey #{get_args} 2>&1")
  retcode = $?.exitstatus
when "port-forward"
  system("rhc-port-forward #{get_args} 2>&1")
  retcode = $?.exitstatus
when "-h", "--help", "help", nil
  p_usage 0
else
  puts "Invalid rhc command: #{ARGV[0]}"
  p_usage
end

exit retcode
