#  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{
    CacheLocationConfig.c is automatically generated from CacheLocationConfig.c.cxxcodebuilder,
    using definitions from src/ruby_supportlib/phusion_passenger/nginx/config_options.rb.
    Edits to CacheLocationConfig.c will be lost.

    To update CacheLocationConfig.c:
      rake nginx

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

  comment '0: NGX_ERROR, 1: OK'
  function('int generated_cache_location_part(ngx_conf_t *cf, passenger_loc_conf_t *conf)') do
    add_code %q{
      size_t len = 0;
      u_char int_buf[32], *end, *buf, *pos;
    }

    separator
    comment 'Calculate lengths'
    separator

    filter_eligible_options(LOCATION_CONFIGURATION_OPTIONS).each do |option|
      if option[:type] == :string
        add_code %Q{
          if (conf->#{struct_field_for(option)}.data != NULL) {
            len += sizeof("!~#{header_name_for(option)}: ") - 1;
            len += conf->#{struct_field_for(option)}.len;
            len += sizeof("\\r\\n") - 1;
          }
        }
      elsif option[:type] == :integer
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET) {
            end = ngx_snprintf(int_buf,
              sizeof(int_buf) - 1,
              "%d",
              conf->#{struct_field_for(option)});
            len += sizeof("!~#{header_name_for(option)}: ") - 1;
            len += end - int_buf;
            len += sizeof("\\r\\n") - 1;
          }
        }
      elsif option[:type] == :uinteger
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET_UINT) {
            end = ngx_snprintf(int_buf,
              sizeof(int_buf) - 1,
              "%ui",
              conf->#{struct_field_for(option)});
            len += sizeof("!~#{header_name_for(option)}: ") - 1;
            len += end - int_buf;
            len += sizeof("\\r\\n") - 1;
          }
        }
      elsif option[:type] == :flag
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET) {
            len += sizeof("!~#{header_name_for(option)}: ") - 1;
            len += conf->#{struct_field_for(option)}
              ? sizeof("t\\r\\n") - 1
              : sizeof("f\\r\\n") - 1;
          }
        }
      else
        raise "Unknown option type #{option[:type].inspect} for option #{option[:name]}"
      end

      separator
    end

    separator

    add_code %q{
      /* Create string */
      buf = pos = ngx_pnalloc(cf->pool, len);
      if (buf == NULL) {
        return 0;
      }
    }

    separator

    filter_eligible_options(LOCATION_CONFIGURATION_OPTIONS).each do |option|
      if option[:type] == :string
        add_code %Q{
          if (conf->#{struct_field_for(option)}.data != NULL) {
            pos = ngx_copy(pos,
              "!~#{header_name_for(option)}: ",
              sizeof("!~#{header_name_for(option)}: ") - 1);
            pos = ngx_copy(pos,
              conf->#{struct_field_for(option)}.data,
              conf->#{struct_field_for(option)}.len);
            pos = ngx_copy(pos, (const u_char *) "\\r\\n", sizeof("\\r\\n") - 1);
          }
        }
      elsif option[:type] == :integer
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET) {
            pos = ngx_copy(pos,
              "!~#{header_name_for(option)}: ",
              sizeof("!~#{header_name_for(option)}: ") - 1);
            end = ngx_snprintf(int_buf,
              sizeof(int_buf) - 1,
              "%d",
              conf->#{struct_field_for(option)});
            pos = ngx_copy(pos, int_buf, end - int_buf);
            pos = ngx_copy(pos, (const u_char *) "\\r\\n", sizeof("\\r\\n") - 1);
          }
        }
      elsif option[:type] == :uinteger
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET_UINT) {
            pos = ngx_copy(pos,
              "!~#{header_name_for(option)}: ",
              sizeof("!~#{header_name_for(option)}: ") - 1);
            end = ngx_snprintf(int_buf,
              sizeof(int_buf) - 1,
              "%ui",
              conf->#{struct_field_for(option)});
            pos = ngx_copy(pos, int_buf, end - int_buf);
            pos = ngx_copy(pos, (const u_char *) "\\r\\n", sizeof("\\r\\n") - 1);
          }
        }
      elsif option[:type] == :flag
        add_code %Q{
          if (conf->#{struct_field_for(option)} != NGX_CONF_UNSET) {
            pos = ngx_copy(pos,
              "!~#{header_name_for(option)}: ",
              sizeof("!~#{header_name_for(option)}: ") - 1);
            if (conf->#{struct_field_for(option)}) {
              pos = ngx_copy(pos, "t\\r\\n", sizeof("t\\r\\n") - 1);
            } else {
              pos = ngx_copy(pos, "f\\r\\n", sizeof("f\\r\\n") - 1);
            }
          }
        }

        separator
      else
        raise "Unknown option type #{option[:type].inspect} for option #{option[:name]}"
      end
    end

    separator

    add_code %q{
      conf->options_cache.data = buf;
      conf->options_cache.len = pos - buf;

      return 1;
    }
  end
end

def filter_eligible_options(options)
  options.reject do |option|
    option[:alias_for] ||
      option.fetch(:field, true).nil? ||
      option[:field].to_s =~ /\./ ||
      !option.fetch(:header, true)
  end
end

def struct_field_for(option)
  if option.has_key?(:field)
    option[:field]
  else
    option[:name].sub(/^passenger_/, '')
  end
end

def header_name_for(option)
  option[:header] || option[:name].upcase
end

main
