#!/bin/bash
################################################################################
# Copyright (c) 2017 VMware, Inc. All rights reserved.
################################################################################
# Wrapper script aimed at creating a temporary folder for temporary statistics
# This needs to be part of vCenter pre-start phase for the service of
# PostgreSQL.

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

if [ -z $VMWARE_CIS_HOME ]; then
   echo "VMWARE_CIS_HOME is not set."
   echo "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

PG_TEMP_STAT_PATH=/dev/shm/postgres_stats

if [ ! -d $PG_TEMP_STAT_PATH ]; then
   mkdir -p $PG_TEMP_STAT_PATH
fi
chmod 700 $PG_TEMP_STAT_PATH
chown ${VMWARE_POSTGRES_OS_ADMIN}:${VMWARE_POSTGRES_OS_GROUP} \
      $PG_TEMP_STAT_PATH

exit 0
