#  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/nginx/config_options'

def main
  set_indent_string '    '
  comment copyright_header_for(__FILE__)

  separator

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

    To update ConfigurationCommands.c:
      rake nginx

    To force regeneration of ConfigurationCommands.c:
      rm -f src/nginx_module/ConfigurationCommands.c
      rake src/nginx_module/ConfigurationCommands.c
  }

  separator

  ctx = self
  LOCATION_CONFIGURATION_OPTIONS.each do |option|
    option = resolve_possible_alias(option)

    struct_initializer do
      element "ngx_string(#{ctx.name_for(option)})"
      element "#{ctx.context_for(option)} | #{ctx.take_type_for(option)}"
      element ctx.setter_function_for(option)
      element ctx.struct_for(option)
      element ctx.struct_field_for(option)
      element ctx.post_for(option)
    end

    add_code ','
  end
end

def resolve_possible_alias(option)
  if option[:alias_for]
    the_alias = LOCATION_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 name_for(option)
  (option[:aliased_for] || option[:name]).inspect
end

def context_for(option)
  context = option.fetch(:context, [:main, :srv, :loc, :lif])
  context.map{ |c| "NGX_HTTP_#{c.to_s.upcase}_CONF" }.join(" | ")
end

def take_type_for(option)
  return option[:take] if option[:take]

  case option[:type]
  when :string, :integer, :uinteger, :string_array
    "NGX_CONF_TAKE1"
  when :flag
    "NGX_CONF_FLAG"
  when :string_keyval
    "NGX_CONF_TAKE2"
  else
    raise "Unknown type #{option[:type].inspect} for option #{option[:name]}"
  end
end

def setter_function_for(option)
  return option[:function] if option[:function]

  case option[:type]
  when :string
    "ngx_conf_set_str_slot"
  when :integer, :uinteger
    "ngx_conf_set_num_slot"
  when :flag
    "ngx_conf_set_flag_slot"
  when :string_array
    "ngx_conf_set_str_array_slot"
  when :string_keyval
    "ngx_conf_set_keyval_slot"
  when :path
    "ngx_conf_set_path_slot"
  else
    raise "Unknown type #{option[:type].inspect} for option #{option[:name]}"
  end
end

def struct_for(option)
  option[:struct] || "NGX_HTTP_LOC_CONF_OFFSET"
end

def struct_type_for(option)
  case struct_for(option)
  when "NGX_HTTP_LOC_CONF_OFFSET"
    "passenger_loc_conf_t"
  when "NGX_HTTP_MAIN_CONF_OFFSET"
    "passenger_main_conf_t"
  else
    raise "Unknown struct #{struct_for(option).inspect}"
  end
end

def struct_field_for(option)
  if option.has_key?(:field)
    if field = option[:field]
      "offsetof(#{struct_type_for(option)}, #{field})"
    else
      "0"
    end
  else
    field = option[:name].sub(/^passenger_/, '')
    "offsetof(#{struct_type_for(option)}, #{field})"
  end
end

def post_for(option)
  option[:post] || 'NULL'
end

main
