#!/bin/bash
################################################################################
# Copyright (c) 2013-2016 VMware, Inc. All rights reserved.
################################################################################
# Sanity checks for environments using VMware Postgres
#
# This script should be called at the beginning of a script using environment
# variables related to VMware Postgres to ensure that the environment used
# is not broken and to ensure that the script will not be broken.
# When adding new environemnt variables, new checks should be added here for
# safety.
#
# Note that not all environments may care about setting some of those values.
# They are still checked for safety but if any of those variables are not used
# just be sure to set a dummy value.

CHECK_STATUS=0

if [ -z $VMWARE_POSTGRES_BASE ]; then
   echo "VMWARE_POSTGRES_BASE is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_BIN ]; then
   echo "VMWARE_POSTGRES_BIN is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_ETC ]; then
   echo "VMWARE_POSTGRES_ETC is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_SCRIPTS ]; then
   echo "VMWARE_POSTGRES_SCRIPTS is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_DB_ADMIN ]; then
   echo "VMWARE_POSTGRES_DB_ADMIN is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_OS_ADMIN ]; then
   echo "VMWARE_POSTGRES_OS_ADMIN is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_OS_GROUP ]; then
   echo "VMWARE_POSTGRES_OS_GROUP is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_DATA ]; then
   echo "VMWARE_POSTGRES_DATA is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_MOUNT_XLOG ]; then
   echo "VMWARE_POSTGRES_MOUNT_XLOG is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_MOUNT_DATA ]; then
   echo "VMWARE_POSTGRES_MOUNT_DATA is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_MOUNT_ARCHIVE ]; then
   echo "VMWARE_POSTGRES_MOUNT_ARCHIVE is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_XLOG ]; then
   echo "VMWARE_POSTGRES_XLOG is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_ARCHIVE ]; then
   echo "VMWARE_POSTGRES_ARCHIVE is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_BACKUP ]; then
   echo "VMWARE_POSTGRES_BACKUP is not set"
   CHECK_STATUS=1
fi
if [ -z $VMWARE_POSTGRES_LOG ]; then
   echo "VMWARE_POSTGRES_LOG is not set"
   CHECK_STATUS=1
fi

if [ $CHECK_STATUS == '1' ]; then
   echo "One or more environment variables are not set. Check your"
   echo "installation."
   exit 1
fi
