#!/bin/bash -e

LOG_DIR=/var/vcap/sys/log/gorouter
source /var/vcap/packages/routing_utils/pid_utils.sh
source /var/vcap/packages/routing_utils/syslog_utils.sh

tee_output_to_sys_log "${LOG_DIR}" "pre-start" <%= p("router.logging.format.timestamp") %>

<%

    # The following ports are manually specified for one of the following
    # reasons:
    # * Not available via bosh links and fragile.
    # * From bosh add-ons, so bosh links don't work.
    ports = [
        2822, # Monit
        2825, # Bosh Agent
        3457, 14829, # UDP Forwarder
        3458, 14823, # Forwarder Agent
        3459, 14824, # Loggregator Agent
        3460, 14822, # Syslog Agent
        3461, 14726, 14727, # Metrics Agent - grpc ingress, prometheus endpoint, own metrics
        8853, 53080, # Bosh DNS
        9100, 14830, # OTel Collector
        14821, # Prom Scraper
        15821, # Metrics Discovery Registrar
        53035, 14922 # System Metrics Agent
    ]

    # the following ports are set in this release
    ports.append(p("router.port")) # has default. will always exist.
    ports.append(p("router.status.port")) # has default. will always exist.
    ports.append(p("router.status.routes.port")) # has default. will always exist.
    ports.append(p("router.tls_port")) # has default. will always exist.

    route_services_internal_server_port = p('router.route_services_internal_server_port')
    if route_services_internal_server_port != 0
        ports.append(route_services_internal_server_port)
    end

    if_p('router.status.tls.port') do |port|
        ports.append(port)
    end
    if_p('router.prometheus.port') do |port|
        ports.append(port)
    end

    # ipv4 only. This makes me sad. I couldn't find a ruby package that takes an IP
    # with a port and returns just the port.
    debug_address = p("router.debug_address").split(":").last.to_i
    if debug_address > 0
        ports.append(debug_address)
    end

    ports.sort!
%>

<% if p('router.set_kernel_parameters') %>
    # Ephemeral port range
    echo "<%= p("router.ip_local_port_range") %>" > /proc/sys/net/ipv4/ip_local_port_range

    # Reserved Ports, not ephemeral
    echo "<%= ports.join(",") %>" > /proc/sys/net/ipv4/ip_local_reserved_ports

    # TCP_FIN_TIMEOUT
    # This setting determines the time that must elapse before TCP/IP can release a closed connection and reuse
    # its resources. During this TIME_WAIT state, reopening the connection to the client costs less than establishing
    # a new connection. By reducing the value of this entry, TCP/IP can release closed connections faster, making more
    # resources available for new connections. Adjust this in the presence of many connections sitting in the
    # TIME_WAIT state:

    echo 5 > /proc/sys/net/ipv4/tcp_fin_timeout

    # TCP_TW_REUSE
    # This allows reusing sockets in TIME_WAIT state for new connections when it is safe from protocol viewpoint.
    # Default value is 0 (disabled). It is generally a safer alternative to tcp_tw_recycle

    echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse

    # Allow a few more queued connections than are allowed by default

    echo 1024 > /proc/sys/net/core/somaxconn
<% else %>
    echo "Not setting /proc/sys/net/ipv4 parameters, since I'm running inside a linux container"
<% end %>

# Allowed number of open file descriptors
ulimit -n <%= p("router.max_open_files") %>

# Add jq to path
cp /var/vcap/jobs/gorouter/bin/setup-jq /etc/profile.d/jq.sh
chown root:vcap /etc/profile.d/jq.sh
