#!/usr/bin/env bash

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

systemtype="$1"
OS="`getos`"


if [ "${OS}" = "Ubuntu" ]; then
    CONFFILE="/opt/dosis2/etc/postgresql/10/90-personality.conf"
elif [ "${OS}" = "CentOS" ]; then
    CONFFILE="/opt/dosis2/etc/postgresql/9.2/custom-postgresql.conf"
else
    abort 2 "Unknown OS: ${OS}"
fi

if [ "${systemtype}" = "site-server" ]; then
    max_connections="300"
    shared_buffers="1024MB"
    work_mem="50MB"
    effective_cache_size="4GB"
else
    max_connections="100"
    shared_buffers="256MB"
    work_mem="50MB"
    effective_cache_size="2GB"
fi


if ! [ -f "${CONFFILE}" ]; then
    touch "${CONFFILE}"
fi


echo "Adjusting max_connections=${max_connections}"
/opt/dosis2/bin/conf-edit "${CONFFILE}" \
    --output "${CONFFILE}" \
    --filetype properties \
    --set .max_connections="${max_connections}"

echo "Adjusting shared_buffers=${shared_buffers}"
/opt/dosis2/bin/conf-edit "${CONFFILE}" \
    --output "${CONFFILE}" \
    --filetype properties \
    --set .shared_buffers="${shared_buffers}"

echo "Adjusting work_mem=${work_mem}"
/opt/dosis2/bin/conf-edit "${CONFFILE}" \
    --output "${CONFFILE}" \
    --filetype properties \
    --set .work_mem="${work_mem}"

echo "Adjusting effective_cache_size=${effective_cache_size}"
/opt/dosis2/bin/conf-edit "${CONFFILE}" \
    --output "${CONFFILE}" \
    --filetype properties \
    --set .effective_cache_size="${effective_cache_size}"
