#!/bin/bash
################################################################################
# Copyright (c) 2016 VMware, Inc. All rights reserved.
################################################################################
# Fetch some status data from PostgreSQL instance. This can be run as root
# or dedicated Postgres user.

ENCODING=UTF8
THIS_DIR=`dirname $0`

if [ -z $VMWARE_POSTGRES_BASE ]; then
   echo "VMWARE_POSTGRES_BASE is not set."
   echo "Check your installation."
   exit 1
fi
SANITY_FILE=$VMWARE_POSTGRES_BASE/scripts/vpostgres_sanity_checks
if [ -f $SANITY_FILE ]; then
   source $SANITY_FILE
else
   echo "Sanity check file for environment variables of VMware Postgres"
   echo "is not available. Check your installation."
   exit 1
fi

# Show utility help
show_help()
{
   ERROR_NUM=$1
   echo "Usage: `basename $0`"
   echo "Example: `basename $0`"
   exit $ERROR_NUM
}

EXPECTED_ARGS=0
if [ $# -ne $EXPECTED_ARGS ]; then
   # Leave with an error code
   show_help 1
fi

# Grab status of server via pg_controldata.
# There is no need to check for the status of the command here, just
# output the result. The data generated here is no more than a couple of
# kilo bytes, so appending it should not matter much.
CONTROL_LOG=${VMWARE_POSTGRES_LOG}/pg_controldata_pre_start.txt
cat <<EOT >> ${CONTROL_LOG}
######################################################
# Date: $(date)
EOT
chown ${VMWARE_POSTGRES_OS_ADMIN}:${VMWARE_POSTGRES_OS_GROUP} ${CONTROL_LOG}
${VMWARE_POSTGRES_BIN}/pg_controldata ${VMWARE_POSTGRES_DATA} 2>&1 >> \
    $CONTROL_LOG
