#  Phusion Passenger - https://www.phusionpassenger.com/
#  Copyright (c) 2017 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/cxxcodebuilder

require 'phusion_passenger/nginx/config_options'

def main
  set_indent_string '    '
  comment copyright_header_for(__FILE__), 1

  separator

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

    To update ConfigGeneral/AutoGeneratedManifestDefaultsInitialization.c:
      rake nginx

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

  separator

  add_code %Q{
    #include "ManifestGeneration.h"
  }

  separator

  function('static void set_manifest_autogenerated_global_conf_defaults(manifest_gen_ctx_t *ctx)') do
    filter_eligible_options(NGINX_CONFIGURATION_OPTIONS, :global).each do |option|
      emit_manifest_config_entry_default_adding_code(option, 'ctx->global_config_container')
      separator
    end
  end

  function('static void set_manifest_autogenerated_app_conf_defaults(' \
    'manifest_gen_ctx_t *ctx, PsgJsonValue *options_container)') \
  do
    filter_eligible_options(NGINX_CONFIGURATION_OPTIONS, :application).each do |option|
      emit_manifest_config_entry_default_adding_code(option, 'options_container')
      separator
    end
  end

  function('static void set_manifest_autogenerated_loc_conf_defaults(' \
    'manifest_gen_ctx_t *ctx, PsgJsonValue *options_container)') \
  do
    filter_eligible_options(NGINX_CONFIGURATION_OPTIONS, :location).each do |option|
      emit_manifest_config_entry_default_adding_code(option, 'options_container')
      separator
    end
  end
end

def filter_eligible_options(options, scope)
  options.reject do |option|
    option[:alias_for] ||
      option.fetch(:field, true).nil? ||
      (option[:default].nil? && option[:dynamic_default].nil?) ||
      option[:scope] != scope
  end
end

def emit_manifest_config_entry_default_adding_code(option, var_name)
  if option[:dynamic_default]
    add_code %Q{
      add_manifest_options_container_dynamic_default(ctx,
        #{var_name},
        #{option[:name].inspect},
        sizeof(#{option[:name].inspect}) - 1,
        #{option[:dynamic_default].inspect},
        sizeof(#{option[:dynamic_default].inspect}) - 1);
    }
  else
    case option[:type]
    when :string
      add_code %Q{
        add_manifest_options_container_static_default_str(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default].inspect},
          sizeof(#{option[:default].inspect}) - 1);
      }
    when :integer
      add_code %Q{
        add_manifest_options_container_static_default_int(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default]});
      }
    when :uinteger, :msec
      add_code %Q{
        add_manifest_options_container_static_default_uint(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default]});
      }
    when :flag
      add_code %Q{
        add_manifest_options_container_static_default_bool(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default] ? 1 : 0});
      }
    when :string_array
      add_code %Q{
        add_manifest_options_container_static_default_str_array(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default]});
      }
    when :string_keyval
      add_code %Q{
        add_manifest_options_container_static_default_str_keyval(ctx,
          #{var_name},
          #{option[:name].inspect},
          sizeof(#{option[:name].inspect}) - 1,
          #{option[:default]});
      }
    else
      raise "Unknown option type #{option[:type].inspect} for option #{option[:name]}"
    end
  end
end

main
