#!/bin/bash
#
# - Sets up the following postgres server logging parameters:
#
#	log_min_error_statement = error      (all postgres versions)
#	log_line_prefix = '%d %m'            (postgres v8+ only)
#
# - Sets up logrotate(8) instructions    (postgres v7.* only)
#
# Notes - overwrites any pre-existing values, if set
#
# Usage: sxpsql-set-logging [-n pg-major-version-number ]
#  e.g.:
#        sxpsql-set-logging -n 7   # major-version-number = 7
#        sxpsql-set-logging        # assumes major-version-number > 7
#

ME=$(basename $0)

usage() {
	echo "Usage: $ME [-n pg-major-version-number ]"
	exit 1
}

#------------------------------------------------------------------------------
# deployLogrotate                                                             #
#------------------------------------------------------------------------------
# Postgres 7.x only - Install logrotate(8) instructions to manage 
#                     'pgactivity.log' file
#
deployLogrotate()
{
    LR_FILE="/etc/logrotate.d/scalix-postgres"
    echo "Installing logrotate(8) instructions for managing 'pgactivity.log'.."
    if [ -f "$LR_FILE" ]; then
        BU_LR="/var/tmp/scalix-postgres_logrotate_saved.$(date +'%F_%T')"
        echo -n "Warning: a version of \"$LR_FILE\" already exists."
        echo "It has been saved to ${BU_LR}."
        cp --archive $LR_FILE $BU_LR
    fi
    cat > $LR_FILE << _EOI_EOI_
# scalix-postgres
# Logrotate configuration for Postgres 7.4+ logfile management.
# Files are kept for a week.

/var/opt/scalix/??/postgres/pgactivity.log {
    dateext
    missingok
    compress
    copytruncate
    daily
    rotate 7
}
_EOI_EOI_
}


# Main
#
if [ $(whoami) != "root" ]; then
	echo "Please run $ME as root."
	exit 1
fi

echo "SCALIX Postgres logging reconfiguration started."

if [ $# -eq 0 ]; then
	echo "$ME called without -n - assuming Postgres v8 as a minimum."
	PG_MAJOR_VER=8
else
	[ $# -ne 2 ] && usage
	[ "$1" != "-n" ] && usage
	PG_MAJOR_VER=$2
fi

# Pull in common routines
#
. /opt/scalix-postgres/bin/common-conf-file.sh

# Deploy logrotate(8) config instructions (Postgres 7.x only)
#
[ "$PG_MAJOR_VER" = "7" ] && deployLogrotate

/etc/init.d/scalix-postgres status >/dev/null 2>/dev/null
POSTGRES_RUNNING=$?

GETINSTDIR="/opt/scalix-postgres/bin/sxpsql-get-inst-dir"
CONF=$($GETINSTDIR)/data/postgresql.conf

# Back up config file
#
BACKUP_CONF=$(backupConfigFile $CONF)
if [ $? -eq 0 ]; then
	echo "$(basename $CONF) backed up to $(basename $BACKUP_CONF)"
else
	echo "Error - unable to back up $CONF : ${BACKUP_CONF}. Stop."
	exit 1
fi

[ $POSTGRES_RUNNING -eq 0 ] && /etc/init.d/scalix-postgres stop

ERRS=0
( ! setConfParam "log_min_error_statement" "error"   $CONF ) && let ERRS=ERRS+1
if [ $PG_MAJOR_VER -gt 7 ]; then
	( ! setConfParam "log_line_prefix"         "'%d %m'" $CONF ) && \
		let ERRS=ERRS+1
fi

if [ $ERRS -eq 0 ]; then
	echo "ScalixDB logging enabled."
else
	cat $BACKUP_CONF > $CONF   # preserve owner/group/perms
	echo "Error encountered - original config file restored."
fi

[ $POSTGRES_RUNNING -eq 0 ] && /etc/init.d/scalix-postgres start

echo "SCALIX Postgres logging reconfiguration completed."
exit $ERRS
