#  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2010-2016 Phusion Holding B.V.
#
#  "Passenger", "Phusion Passenger" and "Union Station" are registered
#  trademarks of Phusion Holding B.V.
#
#  Permission is hereby granted, free of charge, to any person obtaining a copy
#  of this software and associated documentation files (the "Software"), to deal
#  in the Software without restriction, including without limitation the rights
#  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#  copies of the Software, and to permit persons to whom the Software is
#  furnished to do so, subject to the following conditions:
#
#  The above copyright notice and this permission notice shall be included in
#  all copies or substantial portions of the Software.
#
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#  THE SOFTWARE.

# This file uses the cxxcodebuilder API. Learn more at:
# https://github.com/phusion/passenger/cxxcodebuilder

require 'phusion_passenger/apache2/config_options'

def main
  comment copyright_header_for(__FILE__)

  separator

  comment %q{
    ConfigurationCommands.cpp is automatically generated from ConfigurationCommands.cpp.cxxcodebuilder,
    using definitions from src/ruby_supportlib/phusion_passenger/apache2/config_options.rb.
    Edits to ConfigurationCommands.cpp will be lost.

    To update ConfigurationCommands.cpp:
      rake apache2

    To force regeneration of ConfigurationCommands.cpp:
      rm -f src/apache2_module/ConfigurationCommands.cpp
      rake src/apache2_module/ConfigurationCommands.cpp
  }

  separator

  APACHE2_DIRECTORY_CONFIGURATION_OPTIONS.each do |option|
    option = resolve_possible_alias(option)
    format =
      "%s(%s,\n" \
      "\t(%s) %s,\n" \
      "\tNULL,\n" \
      "\t%s,\n" \
      "\t%s),\n"
    code = sprintf(format,
      macro_for(option),
      name_for(option),
      function_type_for(option),
      function_for(option),
      context_for(option),
      description_for(option))
    add_code(code)
  end
end

def resolve_possible_alias(option)
  if option[:alias_for]
    the_alias = APACHE2_DIRECTORY_CONFIGURATION_OPTIONS.find do |o|
      o[:name] == option[:alias_for]
    end.dup
    the_alias[:aliased_for] = option[:name]
    the_alias
  else
    option
  end
end

def macro_for(option)
  case option[:type]
  when :string, :integer
    "AP_INIT_TAKE1"
  when :flag
    "AP_INIT_FLAG"
  else
    raise "Unknown type #{option[:type].inspect} for option #{option[:name].inspect}"
  end
end

def name_for(option)
  str_val(option[:aliased_for] || option[:name])
end

def function_type_for(option)
  case option[:type]
  when :string, :integer
    "Take1Func"
  when :flag
    "FlagFunc"
  else
    raise "Unknown type #{option[:type].inspect}"
  end
end

def function_for(option)
  if option[:function]
    option[:function]
  else
    function_name = option[:name].gsub(/[A-Z]/) do |letter|
      "_" + letter[0..0].downcase
    end
    "cmd#{function_name}"
  end
end

def context_for(option)
  context = option[:context] || ["OR_OPTIONS", "ACCESS_CONF", "RSRC_CONF"]
  context.join(" | ")
end

def description_for(option)
  option[:desc].inspect
end

main
