#!/bin/bash
#
# Postgresql Service

PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
. /etc/federated

config_postgresql() {
  echo -ne "* Configuring postgresql container.."

#  if [[ "${POSTGRESQL_SSL}" = "true" ]]; then
#	POSTGRESQL_SSL="true"
#  elif [[ ! "${POSTGRESQL_SSL}" ]]; then
#	POSTGRESQL_SSL="true"
#  else
#	POSTGRESQL_SSL="false"
#  fi

  if [ ! -d "/federated/apps/postgresql" ]; then
    mkdir -p /federated/apps/postgresql/data/var/lib/postgresql /federated/apps/postgresql/data/docker-entrypoint-initdb.d

    if [[ "${PLUS}" != "true" ]]; then    
      cp /federated/certs/certs/$DOMAIN.crt /federated/apps/postgresql/data/var/lib/postgresql/server.crt
      cp /federated/certs/private/$DOMAIN.key /federated/apps/postgresql/data/var/lib/postgresql/server.key
      chown 999 /federated/apps/postgresql/data/var/lib/postgresql/server.crt /federated/apps/postgresql/data/var/lib/postgresql/server.key
      chmod 600 /federated/apps/postgresql/data/var/lib/postgresql/server.crt /federated/apps/postgresql/data/var/lib/postgresql/server.key
    fi
  fi

if [[ "${PLUS}" != "true" ]]; then    
cat > /federated/apps/postgresql/docker-compose.yml <<EOF
services:
  postgresql:
    image: postgres:\${IMAGE_VERSION}
    container_name: postgresql
    hostname: postgresql.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.14
    volumes:
      - ./data/var/lib/postgresql/server.crt:/var/lib/postgresql/server.crt
      - ./data/var/lib/postgresql/server.key:/var/lib/postgresql/server.key
      - ./data/var/lib/postgresql/data:/var/lib/postgresql/data
      - ./data/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    env_file:
      - ./.env
    secrets:
      - federated_psql_password
    command: >
      -c ssl=on
      -c ssl_cert_file=/var/lib/postgresql/server.crt
      -c ssl_key_file=/var/lib/postgresql/server.key
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

secrets:
  federated_psql_password:
    file: ./.postgresql.secret
networks:
  core:
    external: true
EOF
else
cat > /federated/apps/postgresql/docker-compose.yml <<EOF
services:
  postgresql:
    image: postgres:\${IMAGE_VERSION}
    container_name: postgresql
    hostname: postgresql.$DOMAIN
    restart: always
    networks:
      core:
        ipv4_address: 192.168.0.14
    volumes:
      - ./data/var/lib/postgresql/data:/var/lib/postgresql/data
      - ./data/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
    env_file:
      - ./.env
    secrets:
      - federated_psql_password
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5

secrets:
  federated_psql_password:
    file: ./.postgresql.secret
networks:
  core:
    external: true
EOF
fi

cat > /federated/apps/postgresql/.env <<EOF
IMAGE_VERSION="$(current_version postgresql)"
POSTGRES_DB=postgres
POSTGRES_USER=postgres
POSTGRES_PASSWORD_FILE=/run/secrets/federated_psql_password
POSTGRES_INITDB_ARGS=--encoding='UTF8' --lc-collate='C' --lc-ctype='C'
EOF
chmod 600 /federated/apps/postgresql/.env

PSQL_SECRET=$(create_password)
echo "$PSQL_SECRET" > /federated/apps/postgresql/.postgresql.secret
chmod 600 /federated/apps/postgresql/.postgresql.secret

echo -ne "done.\n"
}
start_postgresql() {
  # Start service with command to make sure it's up before proceeding
  start_service "postgresql" "nc -z 192.168.0.14 5432 &> /dev/null" "18"

  # Tune PostgreSQL
#  if [[ "$BUNDLE" = "starter" ]]; then
#    sed -i "s#shared_buffers =.*#shared_buffers = 50MB#g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
#    sed -i "s#max_connections =.*#max_connections = 400#g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
#    sed -i "s/#work_mem =.*/work_mem = 4MB/g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
#    sed -i "s/#maintenance_work_mem =.*/maintenance_work_mem = 50MB/g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
#  else
    sed -i "s#shared_buffers =.*#shared_buffers = 800MB#g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
    sed -i "s#max_connections =.*#max_connections = 400#g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
    sed -i "s/#work_mem =.*/work_mem = 16MB/g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
    sed -i "s/#maintenance_work_mem =.*/maintenance_work_mem = 128MB/g" /federated/apps/postgresql/data/var/lib/postgresql/data/postgresql.conf
#  fi

  # Restart PostgreSQL
  /federated/bin/stop postgresql &> /dev/null
  /federated/bin/start postgresql &> /dev/null

  echo -ne "done."
}
