#!/usr/bin/env oo-ruby
# -*- 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.
#++

# CAVEAT: if the policy is changed, must run these by hand (release ticket)

require 'rubygems'
require 'openshift-origin-node/utils/tc'

tc = ::OpenShift::Runtime::Utils::TC.new

begin
  case ARGV[0]
  when "start"
    tc.start

  when "stop"
    tc.stop

  when "restart"
    tc.restart

  when "reload"
    tc.restart

  when "startuser"
    tc.startuser(ARGV[1])

  when "stopuser"
    tc.stopuser(ARGV[1])

  when "restartuser"
    tc.restartuser(ARGV[1])

  when "throttleuser"
    tc.throttleuser(ARGV[1])

  when "nothrottleuser"
    tc.nothrottleuser(ARGV[1])

  when "deluser"
    tc.deluser(ARGV[1])

  when "status"
    tc.status(ARGV[1])

  when "show"
    tc.show(ARGV[1])

  else
    $stderr.puts "Usage: #{__FILE__} {start|stop|restart|status [username]|startuser <username>|stopuser <username>|restartuser <username>|throttleuser <username>|nothrottleuser <username>|deluser <username>}"
    exit(1)
  end

rescue RuntimeError, ArgumentError => e
  puts "#{tc.output.join("\n")} [FAIL]"
  puts e
  exit(1)
end

puts "#{tc.output.join("\n")} [OK]"
exit(0)
