#!/usr/bin/env ruby
# We do not want to require ruby193 SCL just to install o-e-release,
# when we use that to bootstrap the channel configuration, including RHSCL.
# So, require and use the system ruby.

#--
# Copyright 2013 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.
#++

# Options parsing...
#
require 'optparse'
options = {
  :wait => 2,
  :verbose => true,
}
optparse = OptionParser.new do |opts|
  opts.banner = <<-"USAGE"
    #{$0}: Upgrade OpenShift Enterprise systems

    Usage: #{$0} [--switches] [status|all|<step>]
    Example: #{$0}
    Example: #{$0} status
    Example: #{$0} all
    Example: #{$0} -q channels

    Switches:
  USAGE

  opts.on('-s','--skip', 'Mark step complete rather than run it') { |o| options[:skip] = o }
  opts.on('-q','--quiet', 'Only display warnings and failures') { |o| options[:verbose] = false }
  opts.on('-d','--debug', 'Enable extra debugging output') { |o| options[:verbose] = options[:debug] = true }
  opts.on('--complete INTEGER', 'Mark upgrade number as complete') { |number| options[:complete] = number.to_i }
  opts.on('-h','--help', 'Print usage') { puts opts; exit 0 }
end

begin
  optparse.parse!
rescue OptionParser::InvalidArgument => e
  puts "\n ##### #{e.message} #####"
  puts optparse.to_s
  puts "\n ##### #{e.message} #####"
  puts
  exit 1
end

#
# execute
#
require 'ose-upgrade/main'
o = OSEUpgrader::Main.new(options)
ARGV.unshift 'status' if ARGV.size == 0
while command = ARGV.shift
  o.set_params(:command => command)
  exitcode = o.run_upgrade
  exit exitcode if exitcode != 0
end
exit 0
