#!/usr/bin/env ruby
#
# print help
#
require 'rhc/coverage_helper'

# Require rhc-common for wizard invocation
require 'rhc-common'

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
  server             Display information about the status of the service.
  setup              Run the setup wizard to configure your account.

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

def run_setup_wizard_if_needed
  if not (RHC::Config.has_local_config? or
          RHC::Config.has_opts_config? or
          ARGV.include?('--noprompt') or
          ARGV.include?('--help') or
          ARGV.include?('-h'))
    RHC::Wizard.new(RHC::Config).run
  end
  ARGV.delete('--noprompt')
end

begin
  case ARGV[0]
  when "app"
    run_setup_wizard_if_needed
    system("rhc-app #{get_args} 2>&1")
    retcode = $?.exitstatus
  else
    begin
      require 'rhc/cli'
      RHC::CLI.set_terminal
      RHC::CLI.start(ARGV)
      retcode = 0
    rescue SystemExit => e
      retcode = e.status
    end
  end

  if retcode == nil
    retcode = 1

    # return codes for uncaught signals are 128 + the signal code
    retcode = 128 + $?.termsig if $?.signaled? and !$?.termsig.nil?
  end

  exit retcode
rescue Interrupt
  exit 128 + 2 #INT
end
