/*
 *  Phusion Passenger - https://www.phusionpassenger.com/
 *  Copyright (c) 2010-2014 Phusion
 *
 *  "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
 *
 *  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.
 */

/*
 * ConfigurationCommands.c is automatically generated from ConfigurationCommands.c.erb,
 * using definitions from lib/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 ext/nginx/ConfigurationCommands.c
 *   rake ext/nginx/ConfigurationCommands.c
 */

<%
require 'phusion_passenger/nginx/config_options'

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]
		return the_alias
	else
		return option
	end
end

def name_for(option)
	return (option[:aliased_for] || option[:name]).inspect
end

def context_for(option)
	context = option.fetch(:context, [:main, :srv, :loc, :lif])
	return 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
		return "NGX_CONF_TAKE1"
	when :flag
		return "NGX_CONF_FLAG"
	when :string_keyval
		return "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
		return "ngx_conf_set_str_slot"
	when :integer, :uinteger
		return "ngx_conf_set_num_slot"
	when :flag
		return "ngx_conf_set_flag_slot"
	when :string_array
		return "ngx_conf_set_str_array_slot"
	when :string_keyval
		return "ngx_conf_set_keyval_slot"
	when :path
		return "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"
		return "passenger_loc_conf_t"
	when "NGX_HTTP_MAIN_CONF_OFFSET"
		return "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]
			return "offsetof(#{struct_type_for(option)}, #{field})"
		else
			return "0"
		end
	else
		field = option[:name].sub(/^passenger_/, '')
		return "offsetof(#{struct_type_for(option)}, #{field})"
	end
end

def post_for(option)
	return option[:post] if option[:post]
	return "NULL"
end
%>

<% for option in LOCATION_CONFIGURATION_OPTIONS %>
{
	<% option = resolve_possible_alias(option) %>
	ngx_string(<%= name_for(option) %>),
	<%= context_for(option) %> | <%= take_type_for(option) %>,
	<%= setter_function_for(option) %>,
	<%= struct_for(option) %>,
	<%= struct_field_for(option) %>,
	<%= post_for(option) %>
},
<% end %>
