module PhusionPassenger::Config::Utils

def self.included(klass)

def self.included(klass)
  # When included into another class, make sure that Utils
  # methods are made private.
  public_instance_methods(false).each do |method_name|
    klass.send(:private, method_name)
  end
end

def complain_that_multiple_passenger_instances_are_running(instances)

def complain_that_multiple_passenger_instances_are_running(instances)
  puts "It appears that multiple #{PROGRAM_NAME} instances are running. Please select"
  puts "a specific one by passing:"
  puts
  puts "  --instance <NAME>"
  puts
  list_all_passenger_instances(instances)
  abort
end

def is_enterprise?

def is_enterprise?
  return defined?(PhusionPassenger::PASSENGER_IS_ENTERPRISE) && PhusionPassenger::PASSENGER_IS_ENTERPRISE
end

def list_all_passenger_instances(instances)

def list_all_passenger_instances(instances)
  puts "The following #{PROGRAM_NAME} instances are running:"
  puts
  printf "%-25s  %s\n", "Name", "Description"
  puts "------------------------------------------------------------------"
  instances.each do |instance|
    printf "%-25s  %s\n", instance.name, instance.server_software
  end
end

def obtain_full_admin_password(instance)

def obtain_full_admin_password(instance)
  begin
    return instance.full_admin_password
  rescue Errno::EACCES
    print_instance_querying_permission_error
    abort
  end
end

def obtain_read_only_admin_password(instance)

def obtain_read_only_admin_password(instance)
  begin
    return instance.read_only_admin_password
  rescue Errno::EACCES
    print_instance_querying_permission_error
    abort
  end
end

def print_instance_querying_permission_error

def print_instance_querying_permission_error
  PhusionPassenger.require_passenger_lib 'platform_info/ruby'
  STDERR.puts "*** ERROR: You are not authorized to query the status for this "
    "#{PROGRAM_NAME} instance. Please try again with " +
    "'#{PhusionPassenger::PlatformInfo.ruby_sudo_command}'."
end

def select_passenger_instance

def select_passenger_instance
  if name = @options[:instance]
    @instance = AdminTools::InstanceRegistry.new.find_by_name_prefix(name)
    if !@instance
      STDERR.puts "*** ERROR: there doesn't seem to be a #{PROGRAM_NAME} instance running with the name '#{name}'."
      list_all_passenger_instances(AdminTools::InstanceRegistry.new.list)
      STDERR.puts
      STDERR.puts "Please pass `--instance <NAME>` to select a specific #{PROGRAM_NAME} instance."
      abort
    elsif @instance == :ambigious
      abort "*** ERROR: there are multiple instances whose name start with '#{name}'. Please specify the full name."
    end
  else
    instances = AdminTools::InstanceRegistry.new.list
    if instances.empty?
      STDERR.puts "*** ERROR: #{PROGRAM_NAME} doesn't seem to be running. If you are sure that it"
      STDERR.puts "is running, then the causes of this problem could be one of:"
      STDERR.puts
      STDERR.puts " 1. You customized the instance registry directory using Apache's"
      STDERR.puts "    PassengerInstanceRegistryDir option, Nginx's"
      STDERR.puts "    passenger_instance_registry_dir option, or #{PROGRAM_NAME} Standalone's"
      STDERR.puts "    --instance-registry-dir command line argument. If so, please set the"
      STDERR.puts "    environment variable PASSENGER_INSTANCE_REGISTRY_DIR to that directory"
      STDERR.puts "    and run this command again."
      STDERR.puts " 2. The instance directory has been removed by an operating system background"
      STDERR.puts "    service. Please set a different instance registry directory using Apache's"
      STDERR.puts "    PassengerInstanceRegistryDir option, Nginx's passenger_instance_registry_dir"
      STDERR.puts "    option, or #{PROGRAM_NAME} Standalone's --instance-registry-dir command"
      STDERR.puts "    line argument."
      abort
    elsif instances.size == 1
      @instance = instances.first
    else
      complain_that_multiple_passenger_instances_are_running(instances)
      abort
    end
  end
end