#!/bin/sh

# This script configures PostgreSQL clusters for usage with Elephant Shed.

# Usage: es_ctlcluster <cluster> {enable-powa|enable-powa-addons}

set -eu

if [ -z "${2:-}" ]; then
  echo "Usage: $0 <version> <cluster> {enable-backup|enable-powa|enable-powa-addons}"
  exit 1
fi

case $1 in
  *-*)
    VERSION="${1%%-*}" CLUSTER="${1#*-}" ACTION="$2" ;;
  */*)
    VERSION="${1%%/*}" CLUSTER="${1#*/}" ACTION="$2" ;;
  *)
    VERSION="$1" CLUSTER="$2" ACTION="$3" ;;
esac

pg_lsclusters -h "$VERSION/$CLUSTER" > /dev/null
if ! pg_lsclusters -h "$VERSION/$CLUSTER" | grep -q online; then
  echo "Cluster $VERSION $CLUSTER is not running"
  exit 1
fi

CONFTOOL="pg_conftool -s $VERSION $CLUSTER"

enable_backup()
{
  # check whether archiving is activated already
  archive_command=$($CONFTOOL show archive_command || :)
  case $archive_command in
    *true*)
      systemctl start pgbackrest-toggle-archiving@$VERSION-$CLUSTER ;;
    *pgbackrest*)
      echo "archive_command already configured" ;;
  esac
  systemctl enable pgbackrest@$VERSION-$CLUSTER.timer
  systemctl start pgbackrest@$VERSION-$CLUSTER.timer
  systemctl enable pgbackrest-incr@$VERSION-$CLUSTER.timer
  systemctl start pgbackrest-incr@$VERSION-$CLUSTER.timer
  echo "backup for cluster $VERSION-$CLUSTER enabled"
}

shared_preload ()
{
  local extension="$1"
  shared_preload_libraries=$($CONFTOOL show shared_preload_libraries || :)
  case $shared_preload_libraries in
    *$extension*)
      return 1 ;; # no restart required
  esac
  echo "Adding $extension to shared_preload_libraries ..."
  ( set -x
    $CONFTOOL set shared_preload_libraries "${shared_preload_libraries:+$shared_preload_libraries, }$extension"
  )
  return 0
}

enable_powa ()
{
  if [ -x /usr/bin/dpkg ]; then
    POWA_PKG="postgresql-$VERSION-powa"
    if ! dpkg -l "$POWA_PKG" | grep -q ^ii; then
      echo "Installing $POWA_PKG ..."
      apt-get install -y "$POWA_PKG"
    fi
  elif [ -x /usr/bin/rpm ]; then
    CONTRIB_PKG="postgresql$VERSION-contrib"
    if ! rpm -q "$CONTRIB_PKG" > /dev/null; then
      echo "Installing $CONTRIB_PKG ..."
      yum install -y "$CONTRIB_PKG"
    fi
    POWA_PKG="powa_$VERSION"
    if ! rpm -q "$POWA_PKG" > /dev/null; then
      echo "Installing $POWA_PKG ..."
      yum install -y "$POWA_PKG"
    fi
  fi

  shared_preload pg_stat_statements && restart=yes
  shared_preload powa && restart=yes
  if [ "${restart:-}" ]; then
    echo "Restarting cluster ..."
    pg_ctlcluster "$VERSION" "$CLUSTER" restart
  fi

  echo "Setting up powa user and database ..."
  su postgres <<EOF
    set -eu
    [ \$(psql -X --cluster "$VERSION/$CLUSTER" -Atc "select datname from pg_database where datname = 'powa'") ] ||
    createdb --cluster "$VERSION/$CLUSTER" --echo powa
    psql -X --cluster "$VERSION/$CLUSTER" -c "create extension if not exists powa cascade" powa
EOF

  echo "Updating powa-web configuration ..."
  update-powa-web-config
}

enable_powa_addons ()
{
  if [ -x /usr/bin/dpkg ]; then
    EXTENSIONS="pg-qualstats pg-wait-sampling pg-stat-kcache pg-track-settings hypopg"
    for EXTENSION in $EXTENSIONS; do
      PKG="postgresql-$VERSION-$EXTENSION"
      if ! dpkg -l "$PKG" | grep -q ^ii; then
        echo "Installing $PKG ..."
        apt-get install -y "$PKG"
      fi
    done
  elif [ -x /usr/bin/rpm ]; then
    EXTENSIONS="pg_qualstats pg-stat-kcache pg_track_settings hypogpg"
    for EXTENSION in $EXTENSIONS; do
      PKG="$EXTENSION_$VERSION"
      if ! rpm -q "$PKG" > /dev/null; then
        echo "Installing $PKG ..."
        yum install -y "$PKG"
      fi
    done
  fi

  for EXTENSION in $EXTENSIONS; do
    if [ "$EXTENSION" = "pg-track-settings" -o "$EXTENSION" = "pg_track_settings" ]; then
      continue
    fi
    shared_preload $(echo $EXTENSION | tr '-' '_') && restart=yes
  done
  if [ "${restart:-}" ]; then
    echo "Restarting cluster ..."
    pg_ctlcluster "$VERSION" "$CLUSTER" restart
  fi
  for EXTENSION in $EXTENSIONS; do
    su postgres <<EOF
      set -eu
      psql -X --cluster "$VERSION/$CLUSTER" -c "create extension if not exists $(echo $EXTENSION | tr '-' '_') cascade" powa
EOF
  done
}

case $ACTION in
  enable-backup)
    enable_backup ;;
  enable-powa)
    enable_powa ;;
  enable-powa-addons)
    enable_powa_addons ;;
  *)
    echo "Error: unknown action '$ACTION'"
    exit 1 ;;
esac
