#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

function set_if_not_set() {
    property="$1"
    value="$2"
    if [ "`/opt/dosis2/bin/conf-edit /etc/sysctl.conf --get .${property} | cut -d= -f2`" = "" ]; then
        /opt/dosis2/bin/conf-edit "/etc/sysctl.conf" --output "/etc/sysctl.conf" --set ".${property}=${value}"
    fi
}

if [ "`getos`" = "CentOS" ]; then
    set_if_not_set 'net.ipv4.tcp_rmem' "'4096 65536 8388608'"
    set_if_not_set 'net.ipv4.ip_forward' '0'
    set_if_not_set 'net.ipv4.tcp_syncookies' '1'
    set_if_not_set 'net.ipv4.conf.default.rp_filter' '1'
    set_if_not_set 'net.ipv4.conf.default.accept_source_route' '0'
    set_if_not_set 'net.bridge.bridge-nf-call-ip6tables' '0'
    set_if_not_set 'net.bridge.bridge-nf-call-iptables' '0'
    set_if_not_set 'net.bridge.bridge-nf-call-arptables' '0'
    set_if_not_set 'net.core.rmem_max' '8388608'
    set_if_not_set 'net.core.rmem_default' '65536'
    set_if_not_set 'kernel.sysrq' '0'
    set_if_not_set 'kernel.core_uses_pid' '1'
    set_if_not_set 'kernel.msgmnb' '65536'
    set_if_not_set 'kernel.msgmax' '65536'
    set_if_not_set 'kernel.shmmax' '536870912'
    set_if_not_set 'kernel.shmall' '2097152'
    set_if_not_set 'kernel.core_pattern' '/opt/dosis2/tmp.out/core'
else
    abort 1 "$0 is only supported on CentOS, you are running: `getos`"
fi