#!/bin/sh
# file      : etc/pgctl
# copyright : Copyright (c) 2014-2015 Code Synthesis Ltd
# license   : MIT; see accompanying LICENSE file
#
# Designed to simplify controlling brep PostgreSQL server instance.

. `dirname $0`/config

CMD="$1"
shift

SOCKET_DIR="$PG_WORKSPACE_DIR"
OUT_FILE="$PG_WORKSPACE_DIR/out"

# Print usage description and exit.
#
case $CMD in
  init|start|stop|status|connect) ;;
  *)
    echo "Usage: pgctl (init|start|stop|status|connect)"
    exit 1
    ;;
esac

ERROR=0

# Initialization includes creating PostgreSQL DB cluster, creating brep DB
# and schema.
#
if test "$CMD" = "init"; then
  if test -d "$PG_DATA_DIR"; then
    echo "PostgreSQL DB cluster directory $PG_DATA_DIR already exist"
  else
    echo "creating PostgreSQL DB cluster ..."
    pg_ctl initdb -D "$PG_DATA_DIR"
    ERROR=$?

    if test $ERROR -eq 0; then
      echo "cluster created"
    else
      echo "cluster creating failed"
      exit $ERROR;
    fi
  fi
fi

case $CMD in
  start|init)
    # Start DB server if not running yet.
    #
    pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1

    if test $? -eq 0; then
      echo "PostgreSQL server already running"
    else
      if test ! -d "$PG_DATA_DIR"; then
        echo "PostgreSQL DB cluster not exist; run '$0 init' first"
	exit 1
      fi

      echo "PostgreSQL server starting ..."

      mkdir -p `dirname $OUT_FILE`
      mkdir -p "$PG_LOG_DIR"
      mkdir -p "$SOCKET_DIR"

      pg_ctl start -D "$PG_DATA_DIR" -w -o \
        "-c port=$PG_PORT -c unix_socket_directories=$SOCKET_DIR \
        -c logging_collector=on -c log_directory=$PG_LOG_DIR" \
	1>"$OUT_FILE" 2>&1

      ERROR=$?

      if test $ERROR -eq 0; then
        echo "server started"
      else
        cat "$OUT_FILE" 1>&2
        echo "server starting failed"
        exit $ERROR
      fi
    fi
    ;;

  status)
    echo "checking PostgreSQL server status ..."
    pg_ctl status -D "$PG_DATA_DIR"
    ;;

  stop)
    pg_ctl status -D "$PG_DATA_DIR" 1>/dev/null 2>&1

    if test $? -eq 0; then
      echo "PostgreSQL server stopping ..."
      pg_ctl stop -D "$PG_DATA_DIR" "$@"
      ERROR=$?

      if test $ERROR -eq 0; then
        : # pg_ctl prints "server stopped" on success
      else
        echo "server stopping failed"
        exit $ERROR
      fi
    else
      echo "PostgreSQL server not running"
    fi
    ;;

  connect)
    echo "connecting to PostgreSQL server ..."
    psql --host=$SOCKET_DIR --port=$PG_PORT brep
    ;;
esac

if test "$CMD" = "init"; then
  # Create brep DB if not exist.
  #
  psql --host=$SOCKET_DIR --port=$PG_PORT -c "" brep 1>/dev/null 2>&1

  ERROR=$?

  if test $ERROR -eq 0; then
    echo "brep DB already exist"
  else
    if test $ERROR -eq 2; then
      echo "creating brep DB ..."
      createdb --host=$SOCKET_DIR --port=$PG_PORT brep
      ERROR=$?

      if test $ERROR -eq 0; then
        echo "brep DB created"
      else
        echo "brep DB creating failed"
        exit $ERROR;
      fi
    else
      echo "brep DB existence checking failed"
      exit $ERROR;
    fi
  fi

  # Create brep DB schema if not exist.
  #
  psql --host=$SOCKET_DIR --port=$PG_PORT -c "select count(1) from package" \
       brep 1>/dev/null 2>&1

  ERROR=$?

  if test $ERROR -eq 0; then
    echo "brep DB schema already exist"
  else
    echo "creating brep DB schema ..."

    psql -v ON_ERROR_STOP=1 --host=$SOCKET_DIR --port=$PG_PORT \
	 --file="$PG_SCHEMA_DIR/package.sql" brep 1>"$OUT_FILE" 2>&1

    ERROR=$?

    if test $ERROR -eq 0; then
      echo "brep DB schema created"
    else
      cat "$OUT_FILE" 1>&2
      echo "brep DB schema creating failed"
      exit $ERROR;
    fi
  fi
fi