<% unless p("cf_networking.disable") %>
<%
    def db_param_path(name)
      "cf_networking.policy_server.database.#{name}"
    end

    def db_param(name)
      if_p(db_param_path name) do |param|
        return param
      end.else do
        return nil
      end
    end

    def require_db_param(name)
      param = db_param name
      return param if !param.nil?

      raise "#{db_param_path name} must be specified"
    end

    def db_host
      from_property = db_param 'host'
      return from_property if !from_property.nil?

      from_link = link("database").instances[0].address
      return from_link if !from_link.nil?

      raise "must provide database link or set #{db_param_path 'host'}"
    end

    def cleanup_interval_in_seconds
      minutes = p("cf_networking.policy_cleanup_interval")
      if minutes < 1
        raise "'cf_networking.policy_cleanup_interval' must be at least 1 minute"
      end
      minutes * 60
    end
%>

<%=
    require 'json'

    user = require_db_param 'username'
    password = require_db_param 'password'
    port = require_db_param 'port'
    name = require_db_param 'name'
    driver = require_db_param 'type'

    unless driver == 'mysql' || driver == 'postgres'
      raise "unknown driver  #{driver}"
    end

    toRender = {
      "listen_host" => p("cf_networking.policy_server.listen_ip"),
      "listen_port" => p("cf_networking.policy_server.listen_port"),
      "log_prefix" => "cfnetworking",
      "debug_server_host" => "127.0.0.1",
      "debug_server_port" => p("cf_networking.policy_server.debug_port"),
      "internal_listen_port" => p("cf_networking.policy_server.internal_listen_port"),
      "uaa_client" => p("cf_networking.policy_server.uaa_client"),
      "uaa_client_secret" => p("cf_networking.policy_server.uaa_client_secret"),
      "uaa_url" => "https://#{p("cf_networking.policy_server.uaa_hostname")}",
      "uaa_port" => p("cf_networking.policy_server.uaa_port"),
      "cc_url" => "http://#{p("cf_networking.policy_server.cc_hostname")}:#{p("cf_networking.policy_server.cc_port")}",
      "skip_ssl_validation" => p("cf_networking.policy_server.skip_ssl_validation"),
      "database" => {
        "type" => driver,
        "user" => user,
        "password" => password,
        "host" => db_host,
        "port" => port,
        "timeout" => p("cf_networking.policy_server.connect_timeout_seconds"),
        "database_name" => name,
      },
      "tag_length" => p("cf_networking.policy_server.tag_length"),
      "metron_address" => "127.0.0.1:#{p("cf_networking.policy_server.metron_port")}",
      "log_level" => p("cf_networking.policy_server.log_level"),
      "cleanup_interval" => cleanup_interval_in_seconds,
      "max_policies" => p("cf_networking.max_policies_per_app_source"),
      "enable_space_developer_self_service" => p("cf_networking.enable_space_developer_self_service"),

      # hard-coded values, not exposed as bosh spec properties
      "ca_cert_file" => "/var/vcap/jobs/policy-server/config/certs/ca.crt",
      "server_cert_file" => "/var/vcap/jobs/policy-server/config/certs/server.crt",
      "server_key_file" => "/var/vcap/jobs/policy-server/config/certs/server.key",
      "uaa_ca" => "/var/vcap/jobs/policy-server/config/certs/uaa_ca.crt",
      "request_timeout" => 5,
    }

    JSON.pretty_generate(toRender)
%>
<% end %>
